|
Postfix是linux明星邮件服务器之一,基本可以满足当前系统的MTA要求(暂时不需要接收邮件功能),接下来将讲解邮件服务器的配置、帐号设置和在ZF中的测试代码。
一、安装软件包
可以到官网下载postfi的tar包自行编译安装,也可以直接用yum安装这里不多说了。
二、设置用户帐号
groupadd -g 3001 postfix
useradd -g postfix -g 3001 postfix
为了使smtpd进程能顺利运行,还要用以下命令创建/etc/aliases.db文件
newaliases
三、启动并检查进程和端口,并增加防火墙规则
postfix start (/usr/sbin/postfix start)
ps -eaf | grep postfix
netstat -anp | grep :25
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
四、Postfix服务器基本配置
邮件服务器安装好后,打开/etc/postfix下的main.cf主配置文件,注意配置以下主要参数
1、myhostname和mydomain:本机的主机名和域名,主机名可以用hostname查看,如需更改可以修改/etc/hosts文件和/etc/sysconfig/network文件,这里测试环境下设置域名steve.net,主机名mail.steve.net;
2、myorigin是向收件人标示本地提交的邮件的来源,默认$myhostname
3、mydestination是发往那些域的邮件将分发给本地用户,默认情况下是$myhostname和localhost.$mydomain
4、设置mynetwork_style=subnet,表示授权网络范围是Postfix服务器主机所在的子网
5、设置relay_domains=$mydestination,表示授权网络范围外的客户机利用Postfix转发邮件时,其目的主机只能是mydestination指定的域,如果要增加可以往下添加一行relay_domain=qq.com
重启服务器测试:postfix stop => postfix start 或直接postfix reload
用telnet测试邮件发送
[iyunv@kevin kristoffer]# telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.steve.com ESMTP Postfix
ehlo localhost
250-mail.steve.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:<kevin@steve.com>
250 2.1.0 Ok
rcpt to:<kristoffer3@163.com>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
This is a test mail!
.
250 2.0.0 Ok: queued as CCD1A24CB2
quit
221 2.0.0 Bye
Connection closed by foreign host.
查看邮件队列
[iyunv@kevin kristoffer]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
CCD1A24CB2 357 Sun Oct 19 09:53:08 kevin@hello.com
(host 163mx01.mxmail.netease.com[220.181.14.143] said: 451 DT:SPM mx16, QsCowEAJU0a3GUNUPQEoCg--.2428S2, please try again 1413683639 http://mail.163.com/help/help_spam_16.htm?ip=115.214.239.239&hostid=mx16&time=1413683639 (in reply to end of DATA command))
kristoffer3@163.com
-- 0 Kbytes in 1 Request.
五、配置SMTP认证
smtp认证机制通过Cyrus SASL包实现
检测安装包:rpm -qa | grep cyrus-sasl
如果没有安装到光盘里找安装包
安装完成后,会在/usr/sbin/saslauthd运行文件,为了使用/etc/passwd文件认证系统用户,需要修改/etc/sysconfig/saslauthd文件,把其中的MECH=pam改为MECH=shadow,然后运行saslauthd脚本
[iyunv@kevin kristoffer]# saslauthd -m /var/run/saslauthd -a shadow
[iyunv@kevin kristoffer]# ps -eaf | grep sasl
root 3307 1 0 10:06 ? 00:00:00 saslauthd -m /var/run/saslauthd -a shadow
root 3308 3307 0 10:06 ? 00:00:00 saslauthd -m /var/run/saslauthd -a shadow
root 3309 3307 0 10:06 ? 00:00:00 saslauthd -m /var/run/saslauthd -a shadow
root 3310 3307 0 10:06 ? 00:00:00 saslauthd -m /var/run/saslauthd -a shadow
root 3311 3307 0 10:06 ? 00:00:00 saslauthd -m /var/run/saslauthd -a shadow
root 3313 2334 0 10:06 pts/0 00:00:00 grep sasl
测试SASL安全认证是否已经正常工作
[iyunv@kevin kristoffer]# testsaslauthd -u kristoffer -p XXXXXXXX
0: OK "Success."
接下来在main.cf中启用SMTP认证功能
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous
修改上述文件,重启服务前还要检查Postfix是否已经支持了SASL认证
输入postconf -a,如果只输出dovecot,没有cyrus,表明未被支持,需要重新编译安装(具体看书)
此外需要在/etc/sasl2/smtpd.conf中确保pwcheck_method:saslauthd
全部就位重启服务器
在用telnet测试之前需要获取加密的用户名和密码
printf "用户名" | openssl base64
printf "密码" | openssl base64
开始测试,从网上拖下来的
注意:在登录过程中可能会报错Error: authentication failed: generic failure,这个是因为SElinux在搞鬼,运行sestatus -v,显示SELinux status:enabled,运行getenforce,显示Enforcing,将其临时关闭setenforce 0,将原来sasl进程kill掉,重启saslauthd和postfix,结果测试可以通过了。
也可以尝试运行sestatus -b | grep sasl 将allow_saslauthd_read_shadow设置为on,setsebool allow_saslauthd_read_shadow 1,getsebool allow_saslauthd_read_shadow检查。将postfix服务stop-start(不能reload)后再测试,如果还是不可以,尝试chmod a+rx /var/run/saslauthd。
最后是在PHP端测试邮件发送,代码如下
/**
* 邮件测试
*/
public function testMailAction(){
$this->_helper->viewRenderer->setNoRender ( true );
header ( 'content-type:text/html;charset=utf-8' );
try{
$mail = new Zend_Mail('UTF-8');
//邮件服务器配置
$config = array('auth' => 'login', 'username' => 'kristoffer', 'password' => 'XXXXXXXXX');
$transport=new Zend_Mail_Transport_Smtp('localhost',$config);
$mail->setDefaultTransport($transport);
$mail->setBodyHtml('<b>Hello World</b> Are U OK!');
$mail->setFrom('tony@binbin.net', 'tony');
$mail->addTo('kristoffer3@163.com', 'kristoffer');
$mail->setSubject('test mail');
$mail->setBodyText('hello world this is a test mail!');
$mail->send();
echo 'success';
}catch(Exception $e){
echo $e->getMessage();
}
} |
|