debian+postfix+dovecot 建立一个小型虚拟域邮件系统

如果你的公司只有几号人或到十多号人,或者你的邮件只是建立在vps 上面,或者你只是希望拥有一个小型的邮件系统,那么本文将教你以最快的速度建一个小型邮件系统满足公司日常需要。

假定我的域名是maildir.cn,我已经设置好了mx记录,那么我们将开始这个奇妙的旅程。

1、安装postfix,安装时选择 移除 exaim 和选择”internet site “

aptitude install postfix

修改/etc/postfix/main.cf ,更改下面的参数为

myhostname = mail.maildir.cn #实际的hostname
mydestination =  mail.maildir.cc, localhost.maildir.cc, localhost  #要去除虚拟域名的域名

增加下面内容

virtual_transport = lmtp:unix:private/dovecot-lmtp
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
#smtpd_tls_auth_only = yes

virtual_mailbox_domains = /etc/postfix/virtual_mailbox_domains

创建/etc/postfix/virtual_mailbox_domains 添加下面的内容

maildir.cn  OK

生成db文件,注意每次修改这个文件都需要执行一次

shell# postmap /etc/postfix/virtual_mailbox_domains

修改 /etc/postfix/master.cf 找到下面的行,去掉注释

submission inet n       -       -       -       -       smtpd
smtps     inet  n       -       -       -       -       smtpd

重启postfix

service postfix restart

2、安装dovecot

shell#apt-get install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd

修改 /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:/var/mail/vhosts/%d/%n

创建邮件存储目录,执行下面命令

mkdir -p /var/mail/vhosts/maildir.cn
groupadd -g 5000 vmail
useradd -r -g vmail -u 5000 vmail -d /var/mail/vhosts -c "virtual mail user"
chown -R vmail:vmail /var/mail/vhosts/

修改/etc/dovecot/conf.d/10-master.conf,将下条目改为:

service imap-login {
  inet_listener imap {
    port = 143
  }
  inet_listener imaps {
    port = 993
    ssl = yes
  }
}

service pop3-login {
  inet_listener pop3 {
    port = 110
  }
  inet_listener pop3s {
    port = 995
    ssl = yes
  }
}

service lmtp {
 unix_listener /var/spool/postfix/private/dovecot-lmtp {
    mode = 0600
        user = postfix
        group = postfix

  }
}

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
        user = postfix
        group = postfix
  }
}

修改 /etc/dovecot/conf.d/10-auth.conf ,找到下面行,去掉注释改为

disable_plaintext_auth = no
auth_mechanisms = plain login

找到最后几行,改为下面(去掉!include auth-passwdfile.conf.ext注释)

#!include auth-system.conf.ext
#!include auth-sql.conf.ext   
#!include auth-ldap.conf.ext
!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
#!include auth-static.conf.ext

编辑/etc/dovecot/conf.d/auth-passwdfile.conf.ext,修改为

passdb {
  driver = passwd-file
	args = scheme=PLAIN username_format=%u /etc/dovecot/dovecot-users
}

userdb {
  driver = passwd-file
  args = username_format=%u /etc/dovecot/dovecot-users
}

创建用户文件/etc/dovecot/dovecot-users,输入下面的内容,以下为明文密码

#cat /etc/dovecot/dovecot-users
fy@maildir.cn:{PLAIN}abc123:5000:5000

重启dovecot

service dovecot restart

测试

发邮件

# sendmail fy@maildir.cn
test
.

日志

Sep 4 13:03:37 mail postfix/pickup[4876]: 0A70CA0F1: uid=0 from=<root>
Sep 4 13:03:37 mail postfix/cleanup[8923]: 0A70CA0F1: message-id=<20150904050337.0A70CA0F1@mail.maildir.cc>
Sep 4 13:03:37 mail postfix/qmgr[4877]: 0A70CA0F1: from=<root@mail.maildir.cccn>, size=255, nrcpt=1 (queue active)
Sep 4 13:03:37 mail dovecot: lmtp(8926): Connect from local
Sep 4 13:03:37 mail dovecot: lmtp(8926, fy@maildir.cn): cLPLCykm6VXeIgAA/cW88w: msgid=<20150904050337.0A70CA0F1@mail.maildir.cc>: saved mail to INBOX
Sep 4 13:03:37 mail postfix/lmtp[8925]: 0A70CA0F1: to=<fy@maildir.cn>, relay=mail.maildir.cc[private/dovecot-lmtp], delay=3.3, delays=2.9/0.02/0.02/0.4, dsn=2.0.0, status=sent (250 2.0.0 <fy@maildir.cn> cLPLCykm6VXeIgAA/cW88w Saved)
Sep 4 13:03:37 mail dovecot: lmtp(8926): Disconnect from local: Client quit (in reset)
Sep 4 13:03:37 mail postfix/qmgr[4877]: 0A70CA0F1: removed

收邮件

# telnet localhost 110
Trying ::1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
user fy@maildir.cn
+OK
pass abc123
+OK Logged in.
list
1 479
.
quit
+OK Logging out.
Connection closed by foreign host.

一切正常

参考

http://wiki2.dovecot.org/HowTo/PostfixDovecotLMTP

https://help.ubuntu.com/community/PostfixVirtualMailBoxClamSmtpHowto

http://www.binarytides.com/install-postfix-dovecot-debian/

http://wiki2.dovecot.org/AuthDatabase/PasswdFile

http://wiki2.dovecot.org/Variables