OpenBSD as a mail server(三)postfix的安装与配置

1.安装 postfix-2.9.6-pgsql,默认带dovecot的认证方式(SMTP)

server:/home/xisxy$ sudo pkg_add postfix-2.9.6-pgsql.tgz
-> Creating /etc/mailer.conf.postfix
-> Creating Postfix spool directory and chroot area under /var/spool/postfix
-> Creating Postfix data directory under /var/postfix
+---------------
| Configuration files have been installed in /etc/postfix.
| Please update these files to meet your needs.
+---------------
postfix-2.9.6-pgsql: ok
The following new rcscripts were installed: /etc/rc.d/postfix
See rc.d(8) for details.
--- +postfix-2.9.6-pgsql -------------------
Postfix can be set up to replace sendmail entirely. Please read the
documentation at file:/usr/local/share/doc/postfix/html/index.html or
http://www.postfix.org/ carefully before you decide to do this!

To replace sendmail with Postfix you have to install a new mailer.conf
using the following command:

    /usr/local/sbin/postfix-enable

If you want to restore sendmail, this is done using the following command:

    /usr/local/sbin/postfix-disable
   
server:/home/xisxy$ sudo /usr/local/sbin/postfix-enable
old /etc/mailer.conf saved as /etc/mailer.conf.pre-postfix
postfix /etc/mailer.conf enabled

NOTE: do not forget to add sendmail_flags=NO to
      /etc/rc.conf.local to disable sendmail.

NOTE: do not forget to add "-a /var/spool/postfix/dev/log" to
      syslogd_flags in /etc/rc.conf.local and restart syslogd.

NOTE: do not forget to add postfix to pkg_scripts in
      /etc/rc.conf.local to start postfix automatically.

NOTE: do not forget to remove the "sendmail clientmqueue runner"
      from root's crontab.

开启postfix替换到自带的sendmail
server:/home/xisxy$ sudo /usr/local/sbin/postfix-enable

最终的/etc/rc.conf.local,内容如下:
sendmail_flags=NO
syslogd_flags="-a /var/spool/postfix/dev/log"
nginx_flags=""
pkg_scripts="postgresql php_fpm postfix "

server:/home/xisxy$ sudo crontab -e
# sendmail clientmqueue runner
#*/30   *       *       *       *       /usr/sbin/sendmail -L sm-msp-queue -Ac -q

重启系统,让postfix完全替换掉sendmail
server:/home/xisxy$ sudo reboot


2.配置 postfix

准备工作
新建一个不能登录系统的帐户vmail,作为postfix虚拟用户在系统中的身份
server:/home/xisxy$ sudo useradd -m -d /var/vmail/ -s /sbin/nologin -u 3000 -g=uid vmail
用postfixadmin生成测试域名和测试用户
test.com
[email protected]


生成postfix信息在postgresql数据库中的查询表,只建了三个常用的映射表,如要用到更多功能可再手动加
简单的脚本如下:

server:/home/xisxy$vi create_pgsql_maps.sh
#!/bin/sh

cd /etc/postfix
PGPW="Your_DB_password"
PGUSER="postfixdbm"
PGDB="postfixdb"
HOST="127.0.0.1"

cat -user = $PGUSER
password = $PGPW
hosts = $HOST
dbname = $PGDB
query = Select goto From alias Where address='%s' and active =true
EOF

cat -user = $PGUSER
password = $PGPW
hosts = $HOST
dbname = $PGDB
query = Select domain from domain where domain='%s' and backupmx = false and active=true
EOF

cat -user = $PGUSER
password = $PGPW
hosts = $HOST
dbname = $PGDB
query = Select maildir from mailbox where username='%s' and active=true
EOF

server:/home/xisxy$ chmod +x create_pgsql_maps.sh
server:/home/xisxy$ sudo create_pgsql_maps.sh
查看效果
server:/home/xisxy$ cd /etc/postfix
server:/etc/postfix$ ls |grep maps                                                                                                                          
pgsql_virtual_alias_maps.cf
pgsql_virtual_domains_maps.cf
pgsql_virtual_mailbox_maps.cf


验证能否查询到正确结果:
#postmap -q [email protected]  pgsql:/etc/postfix/pgsq_virtual_alias_maps.cf
[email protected]

server:/etc/postfix$ sudo vi main.cn
在最后添加如下类似内容
myhostname = mail.test.com
mydomain = test.com
myorigin = $mydomain
inet_interfaces = all
mynetworks = 127.0.0.0/8
alias_maps = hash:/etc/postfix/aliases
home_mailbox = Maildir/
mailbox_size_limit = 51200000
virtual_mailbox_limit = 51200000
message_size_limit = 10240000
virtual_uid_maps = static:3000
virtual_gid_maps = static:3000
virtual_mailbox_base = /var/vmail/
virtual_alias_maps = $alias_maps,proxy:pgsql:/etc/postfix/pgsql_virtual_alias_maps.cf
virtual_mailbox_maps = proxy:pgsql:/etc/postfix/pgsql_virtual_mailbox_maps.cf
virtual_mailbox_domains = proxy:pgsql:/etc/postfix/pgsql_virtual_domains_maps.cf


国加入了alias_maps = hash:/etc/postfix/aliases,使aliases列表生效
server:/etc/postfix$ sudo postalias /etc/postfix/aliases

posfix生效新配置文件
server:/etc/postfix$ sudo postfix reload

发测试信
server:/etc/postfix$ mail [email protected]
Subject: test
Just do a test.
1234567.
.
EOT

查看邮件日志
server:/root# tail /var/log/maillog                                                                                                                         
May 30 09:00:01 server newsyslog[14975]: logfile turned over
May 30 15:12:19 server postfix/pickup[21097]: 6586218952D: uid=1000 from=
May 30 15:12:19 server postfix/cleanup[16873]: 6586218952D: message-id=
May 30 15:12:19 server postfix/qmgr[32399]: 6586218952D: from=, size=324, nrcpt=1 (queue active)
May 30 15:12:19 server postfix/virtual[9834]: 6586218952D: to=, relay=virtual, delay=0.16, delays=0.07/0.03/0/0.07, dsn=2.0.0, status=sent (delivered to maildir)
May 30 15:12:19 server postfix/qmgr[32399]: 6586218952D: removed

查看用户邮件,是不是刚开发的那一封
server:/root# cd /var/vmail/test.com/test/new/                                                                                                                                                                                                                
server:/var/vmail/test.com/test/new# ls
1369897939.V404I156908M491905.server.domain.org
server:/var/vmail/test.com/test/new# tail 1369897939.V404I156908M491905.server.domain.org  
Received: by mail.test.com (Postfix, from userid 1000)
        id 6586218952D; Thu, 30 May 2013 15:12:19 +0800 (CST)
To: [email protected]
Subject: test
Message-Id:
Date: Thu, 30 May 2013 15:12:19 +0800 (CST)
From: [email protected] (xisxy)

Just do a test.
1234567.
server:/var/vmail/test.com/test/new#

设置成功,下面开始设置SMTP认证-dovecot
12-08 10:13