容器化邮件转发

一直使用的docker容器突然不能启动了,只好重做一个,顺便翻出2014年底写的草稿,改一改发出来。

公司的邮件系统是MS Exchange + MS Outlook,我的工作环境是Ubuntu + Emacs,任务管理使用org-mode。这里有一点不和谐之处——不能从Emacs里面直接读取公司邮件。

之前的解决方案是在Ubuntu上架一个courier-imap服务器,然后在Outlook里创建规则把邮件复制过去。

但是这个方案有个致命缺陷,当Outlook无法连接IMAP服务器,复制规则就会出错然后被禁用。导致我老是要在笔记本离开公司内部网络时先关掉Outlook。最常见的情况就是去开会,这时通常使用无线网络,但是无线网络不属于公司内网,尽管Outlook能够继续收邮件,但是没法连接到Ubuntu机器上的imap服务器。

于是我就在笔记本电脑上安装了boot2docker,运行一个docker容器,容器里面运行一个用于缓存邮件的IMAP服务器,并定时运行imapsync同步邮件。如果笔记本离开公司内网,imapsync就会同步失败,这时邮件缓存在笔记本电脑里,直到连上公司内网。

上次创建的时候完全是手工操作,这次重新开始,boot2docker已经更名为Docker for Windows,我也能够写出一个Dockerfile自动化构建容器。

FROM debian:latest

RUN echo "root:Docker!" | chpasswd
RUN apt-get -y update -qq && \
  echo 'courier-base courier-base/webadmin-configmode boolean false' | debconf-set-selections && \
  apt-get -y install gamin courier-imap courier-imap-ssl unzip wget \
    libauthen-ntlm-perl \
    libcrypt-ssleay-perl \
    libdigest-hmac-perl \
    libfile-copy-recursive-perl \
    libio-compress-perl \
    libio-socket-inet6-perl \
    libio-socket-ssl-perl \
    libio-tee-perl \
    libmodule-scandeps-perl \
    libnet-ssleay-perl \
    libpar-packer-perl \
    libreadonly-perl \
    libterm-readkey-perl \
    libtest-pod-perl \
    libtest-simple-perl \
    libunicode-string-perl \
    liburi-perl \
    cpanminus make
RUN cpanm Data::Uniqid Mail::IMAPClient
RUN mkdir ~/imapsync && cd ~/imapsync && \
  wget https://github.com/imapsync/imapsync/archive/master.zip && \
  unzip *.zip && maildirmake ~/Maildir

EXPOSE 143 993

CMD /etc/init.d/courier-authdaemon start; \
  /etc/init.d/courier-imap start; \
  while true; \
  do ~/imapsync/imapsync-master/imapsync \
       --host1 localhost --user1 root --password1 "Docker!" --delete \
       --host2 example.com --user2 me --password2 "blahblahblah" -ssl2 >/dev/null 2>&1 ; \
     sleep 300; \
  done

构建命令

docker build -t emailfw .

运行时需要映射端口

 docker run -d -p 6666:143 -p 6667:993 --name emailfw emailfw:latest

然后在Outlook里面设置邮件服务器地址为192.168.99.100,端口为6666,用户名root,密码Docker!即可。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据