Postfix 邮件服务器 - postfix服务
postfix 邮件服务 也是基于sendmail (需要关闭或卸载sendmail)
postfix 是一个电子邮件服务器,它为了改良sendmail邮件服务器而产生的,并且它的配置文件比sendmail简单得多,配置相当容易。
postfix 作为发送邮件服务器
1、安装
yum install postfix
postfix 检测版本;
postconf -d | grep mail_version
postconf 还有其他命令可以了解一下。
2、创建邮件专用账户 ( 为下面配置需要使用 );
groupadd -g 5000 wmail
useradd -g wmail -u 5000 -s /sbin/nologin wmail
/etc/passwd 查看:
3、创建 postfix 库
新安装mysql默认没有root密码 直接登录;
为 postfix 创建 email 库,设置任意远程账户或者限制为本机localhost访问;
便于实验使用 库名账户密码都为 postfix
mysql -u root -p mysql> create database postfix default character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec) mysql> grant all on postfix.* to 'postfix'@'%' identified by 'postfix'; Query OK, 0 rows affected (0.01 sec) mysql> FLUSH PRIVILEGES;
4、文件配置
postfix 的配置文件是 main.cf
配置示例参考: [root@mail ~]# cat /etc/postfix/main.cf queue_directory = /var/spool/postfix command_directory = /usr/sbin daemon_directory = /usr/libexec/postfix data_directory = /var/lib/postfix mail_owner = postfix inet_interfaces = all inet_protocols = all myhostname = mail.wangxiaojing.com mydomain = wangxiaojing.com myorigin = $mydomain # mydestination = $myhostname, localhost.$mydomain, localhost mydestination = mynetworks = 0.0.0.0/0
# 可以设置允许的网段访问
local_recipient_maps = unknown_local_recipient_reject_code = 550 alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases debug_peer_level = 2 debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5 sendmail_path = /usr/sbin/sendmail.postfix newaliases_path = /usr/bin/newaliases.postfix mailq_path = /usr/bin/mailq.postfix setgid_group = postdrop html_directory = no manpage_directory = /usr/share/man sample_directory = /usr/share/doc/postfix-2.6.6/samples readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES #启用SMTP认证 smtpd_sasl_auth_enable = yes smtpd_sasl_local_domain = $myhostname smtpd_sasl_type = dovecot smtpd_sasl_security_options = noanonymous smtpd_sasl_path = /var/spool/postfix/private/auth smtpd_banner = $myhostname ESMTP $mail_name smtpd_sender_restrictions = permit_mynetworks, reject_sender_login_mismatch, reject_authenticated_sender_login_mismatch, reject_unauthenticated_sender_login_mismatch smtpd_sasl_application_name = smtpd broken_sasl_auth_clients = yes smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination smtpd_sasl_security_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination smtpd_client_restrictions = permit_mynetworks,permit_sasl_authenticated #smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache #smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem #smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key #smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache #smtpd_use_tls = yes #启用虚拟用户 virtual_mailbox_base = /home/wmail/
# 前面创建的邮件专用账户
virtual_mailbox_domains = proxy:mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps virtual_alias_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf virtual_mailbox_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf virtual_uid_maps = static:5000 virtual_gid_maps = static:5000 virtual_transport = dovecot dovecot_destination_recipient_limit = 1
master.cf 配置参考:
在 master.cf 文件最后添加: dovecot unix - n n - - pipe flags=DRhu user=wmail:wmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}
# 注意空格 不要顶行 否则会报错。
5、启动检测:
/etc/init.d/postfix start chkconfig postfix on ss -tln (检测 SMTP 25 端口)
6、创建 数据库连接相关文件
mkdir /etc/postfix/sql/
在 sql 目录下创建以下连接文件
# cat mysql_virtual_alias_domain_catchall_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1' # cat mysql_virtual_alias_domain_mailbox_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u','@',alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1' # cat mysql_virtual_alias_domain_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1' # cat mysql_virtual_alias_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT goto FROM alias WHERE address='%s' AND active = '1' # cat mysql_virtual_domains_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT domain FROM domain WHERE domain='%s' AND active = '1' # cat mysql_virtual_mailbox_limit_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1' # cat mysql_virtual_mailbox_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
7、测试 sasl 与 postfix 集成链接;
telnet mail.wangxiaojing.com 25
测试输入 ehlo wangxiaojing.com 结果如下: 表示连接状态和服务状态正确
[root@mail~ ]# telnet mail.wangxiaojing.com 25 Trying 172.16.2.18... Connected to mail.wangxiaojing.com. Escape character is '^]'. 220 mail.wangxiaojing.com ESMTP Postfix ehlo wangxiaojing.com 250-mail.wangxiaojing.com 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-AUTH PLAIN LOGIN CRAM-MD5 250-AUTH=PLAIN LOGIN CRAM-MD5 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN quit 221 2.0.0 Bye Connection closed by foreign host.
8、邮件发送测试;(qq邮箱或者其他公网邮箱服务有可能会被当垃圾邮件屏蔽)
可以看 /var/log/maillog 日志信息查看。
发送邮件测试(格式) 链接 smtp 25 端口 发件服务 随机 admin@wangxiaojing.com
from 发送邮件 rcpt 方式发送 data 内容格式
. 邮件编辑结束
telnet mail.wangxiaojing.com 25 mail from:admin@wangxiaojing.com rcpt to:123456@qq.com data note. . quit
测试发送
QQ 邮箱上查看: