【小编推荐】CentOS sendmail更改为postfix默认发信的设置

2014-10-08   |   发布者:梁国芳   |   查看:3320次

linux
 一、配置Postfix并启用




postfix是CentOS默认安装的邮件服务器软件。以下配置示例假设要配置的域名是0fafa.com,邮件服务器主机名是mail.0fafa.com。

1. 打开postfix的配置文件(基本上修改的是将这些行前面有#的需要删除)

vi /etc/postfix/main.cf

2. :75 光标移至第75行,修改myhostname

myhostname = email.aliyun.com

3. :83 光标移至第83行,修改mydomain

mydomain = aliyun.com

4. :99 光标移至第99行,修改myorigin

myorigin = $mydomain

5. :116光标移至第116行,修改inet_interfaces

inet_interfaces = all

6. :119光标移至第119行,修改inet_protocols

inet_protocols = ipv4

7. :164光标移至第164行,添加$mydomain

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, aliyun.com

# 最后一个参数为指定发给本地邮件的域名

8. :264光标移至第264行,修改mynetworks

mynetworks = 168.100.189.0/28, 127.0.0.0/8

#指定内网和本地的IP地址范围,按照情况修改,一般无需变动

9. :296光标移至第296行,修改relay_domains

relay_domains =

10. :419光标移至第419行,设置home_mailbox

home_mailbox = Maildir/

11. :425 光标移至第425行,设置mail_spool_directory,/data是之前挂载的数据盘,mail目录需要通过mkdir命令创建

mail_spool_directory = /data/mail

12. 重启postfix使设置生效

service postfix restart

# 这里,如果重启出现如下情况也不要惊讶,因为你没启动过Postfix因而关闭肯定failed。第一次启动可能时间长一点。
 

二、停止sendmail服务及卸载,并设置Postfix为默认邮件程序

如果你之前开了sendmail,首先请停用并卸载。因为在用Postfix作为SMTP服务器的前提下,我们不准备再用sendmail。所以一不做二不休,将sendmail服务关掉。

/etc/rc.d/init.d/sendmail stop

之后会显示如下信息: (或者不显示)

Shutting down sendmail:         [ OK ]

Shutting down sm-client:         [ OK ]

之后再输入如下命令:

chkconfig sendmail off

# 关闭sendmail自启动

chkconfig –list sendmail

# 确认sendmail自启动已被关闭(都为off就OK)

之后会显示如下信息:

sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off

然后再将默认的MTA设置为Postfix:

alternatives –config mta

# 设置默认MTA

之后会显示如下信息:

There are 2 programs which provide ‘mta’.

Selection Command

—————————————————

1           /usr/sbin/sendmail.postfix

*+ 2           /usr/sbin/sendmail.sendmai

# 当前状态:sendmail为默认MTA

Enter to keep the current selection[+], or type selection number: 1

# 在这里输入1,使Postfix成为默认MTA。如果你的Postfix那行前面的Selection那列为2,则输入2

然后就是最后一步了,卸载掉sendmail:

yum remove sendmail

三、启动相应服务

最后,启动Postfix服务,并设置相应服务为自启动。

chkconfig postfix on

# 将Postfix设置为自启动

chkconfig –list postfix

# 确认Postfix服务状态

之后会显示如下信息:

postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off

# 确认2~5为on的状态就OK

/etc/rc.d/init.d/postfix start

# 启动Postfix

之后会显示如下信息:(如果启动过请重启) (或者不显示)

Starting postfix:            [ OK ]

四、测试及其他详细设置

测试telnet测试邮件服务器是否正常

1. 在邮件服务器上安装telnet

yum install telnet

2. 连接服务器SMTP端口

telnet 127.0.0.1 25

3. 填写发件人

MAIL FROM:

回车

4. 填写收件人

RCPT TO:contact@163.com

回车

5. 撰写邮件内容

5.1 开始写邮件内容

DATA

回车

5.2 输入标题

Subject: test message

回车

5.3 输入内容,并以.号结束

test body

.

如果显示下面的信息,说明邮件进入发送队列

250 2.0.0 Ok: queued as 88D6D32A94

测试函数调用发信:

将一下文本复制到新建PHP文件中,如1.php,上传到网站跟目录并打开测试是否能够收到信件:

$send = mail(‘你的邮箱地址’, ‘My Subject’, ‘The test mail’);
if($send){echo ‘发送成功';}else{echo ‘发送失败';}
?>

其他详细设置:

上述测试函数调用发信问题

如果不能发送成功,检查php.ini的710行的 sendmail_path 并且去掉前面的分号,最后修改成:

sendmail_path = /usr/sbin/sendmail -t –i

授权其他服务器通过该邮件服务器发送邮件

未授权的情况下,如果在其他服务器telnet这台服务器,在输入收件人之后会出现Relay access denied错误

RCPT TO:contact@163.com

554 5.7.1

解决方法:

vi /etc/postfix/main.cf,:264将光标移至第264行,在mynetworks的值之后加这台的服务器IP地址,比如:

mynetworks = 127.0.0.0/8 58.33.14.124

service postfix restart之后,问题解决。

其他问题

收到上面telnet发出的邮件时,你会发现收件人信息显示的是undisclosed recipients,解决方法是在DATA命令之后,输入Subject之前,分别输入:

FROM:test@aliyun.com

TO:contact@163.com
==================================================================

postfix   常用命令。

常用操作:
postqueue -p 列出队列里的所有邮件
postsuper -d ID 删除一封邮件
postsuper -d ALL 删除所有邮件
postsuper –h ID 保留邮件
postsuper -r ALL 重新排例
postcat -q ID   显示邮件内容
postqueue -f   清空邮件

如果 显示发送成功,但是一直收不到邮件。


日志显示(或者post queue -p)

 Host or domain name not found. Name service error for name=qq.com type=MX: Host not found, try again


可能是dns 的问题。修改下dns 8.8.8.8  。