CentOS7安装配置sendmail汇总——仍然没有配置成功

1. CentOS 7 默认安装了sendmail,直接命令行可以运行。

which sendmail,如下:
/usr/sbin/sendmail
关闭IPV6(默认是开启的,IPV4和IPv6的地址不一样,关闭后记得检查IP地址)
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1

2. 运行mailq

查看mail队列,或者tail /var/log/maillog,会发现:
Jun 13 08:28:26 deaboway postfix/smtp[866]: connect to 163mx02.mxmail.netease.com[220.181.14.146]:25: Connection timed out
Jun 13 08:28:26 deaboway postfix/smtp[863]: connect to 163mx03.mxmail.netease.com[220.181.14.159]:25: Connection timed out
Jun 13 08:28:56 deaboway postfix/smtp[865]: connect to 163mx01.mxmail.netease.com[220.181.14.143]:25: Connection timed out
Jun 13 08:28:56 deaboway postfix/smtp[864]: connect to 163mx02.mxmail.netease.com[220.181.14.150]:25: Connection timed out
Jun 13 08:28:56 deaboway postfix/smtp[865]: 400DE6B47: to=<[email protected]>, relay=none, delay=721, delays=571/0.02/150/0, dsn=4.4.1, status=deferred (connect to 163mx01.mxmail.netease.com[220.181.14.143]:25: Connection timed out)
Jun 13 08:28:56 deaboway postfix/smtp[864]: B35EB6B45: to=<[email protected]>, relay=none, delay=797, delays=646/0.02/150/0, dsn=4.4.1, status=deferred (connect to 163mx02.mxmail.netease.com[220.181.14.150]:25: Connection timed out)
Jun 13 08:28:56 deaboway postfix/smtp[866]: connect to 163mx01.mxmail.netease.com[220.181.14.139]:25: Connection timed out
Jun 13 08:28:56 deaboway postfix/smtp[866]: 1D5BD6B44: to=<[email protected]>, relay=none, delay=808, delays=657/0.03/151/0, dsn=4.4.1, status=deferred (connect to 163mx01.mxmail.netease.com[220.181.14.139]:25: Connection timed out)
Jun 13 08:28:56 deaboway postfix/smtp[863]: connect to 163mx03.mxmail.netease.com[220.181.14.161]:25: Connection timed out
Jun 13 08:28:56 deaboway postfix/smtp[863]: B9E536B46: to=<[email protected]>, relay=none, delay=726, delays=575/0.02/151/0, dsn=4.4.1, status=deferred (connect to 163mx03.mxmail.netease.com[220.181.14.161]:25: Connection timed out)

所以说:sendmail是Linux下优秀的邮件系统。在不做任何设定的情况下,sendmail发出邮件的邮箱源地址形如[email protected],这种地址几乎会被所有的邮箱认定为SPAM(垃圾邮件)或直接拒收。

3. 保险起见,重新安装sendmail
yum install -y sendmail
yum install -y sendmail-cf

4. Senmail的SMTP认证配置(不需要认证的可忽略此步)
首先确认saslauthd服务是否安装或启动。
安装saslauthd服务:# yum install -y saslauthd
启动saslauthd服务:# service saslauthd start
(1) 配置Senmail的SMTP认证
# vim /etc/mail/sendmail.mc
dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN’)dnl
dnl define(`confAUTH_MECHANISMS’, `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN’)dnl
将上面两行的dnl去掉。在sendmail文件中,dnl表示该行为注释行,是无效的,因此通过去除行首的dnl字符串可以开启相应的设置行。
(2) 设置Sendmail服务的网络访问权限
# vim /etc/mail/sendmail.mc
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA’)dnl
将127.0.0.1改为0.0.0.0,意思是任何主机都可以访问Sendmail服务。如果仅让某一个网段能够访问到Sendmail服务,将127.0.0.1改为形如192.168.1.0/24的一个特定网段地址。

5. 生成Sendmail配置文件
Sendmail的配置文件由m4来生成,m4工具在sendmail-cf包中。如果系统无法识别m4命令,说明sendmail-cf软件包没有安装。
生成Sendmail的配置文件:
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
需要重启Sendmail才能使配置文件生效。

6. Sendmail邮件域名配置
配置sendmail使用合法的域名分两步,修改sendmail的配置文件和添加域名MX记录。
sendmail可以直接使用 “-r [email protected]” 参数来以任意源地址发送邮件,但目前主流的邮箱都会将源地址和反向解析IP进行比较,如果解析不到或是解析的IP不匹配,轻则将邮件直接归为SPAM,严重的就直接拒绝接收。

MX记录(Mail Exchanger Record)主要是接收邮件时使用,即当投递一封新邮件时,会查询收件人域名的MX记录,然后通过MX记录得到的IP地址进行投递。同时邮件厂商在接收邮件的时候也会将源地址和MX记录进行比较,作为垃圾邮件的判断标准之一。

第一步:MX记录
A mail 107.191.44.43 1 Hour
MX @ mail.deaboway.com (Priority: 10) 1 Hour

第二步:添加域名
(1) 将域名加入到local-host-names文件
# vim /etc/mail/local-host-names,添加
deaboway.com
(2) 修改submit.cf文件
# vim /etc/mail/submit.cf,找到行 #Dj$w.Foo.COM,修改为
Dj$w.com 或者 Dj$deaboway.com

sendmail邮件命名配置完毕,重启sendmail使配置生效。 命令:
service sendmail restart
/bin/systemctl restart sendmail.service

第三步:本机IP映射
编辑/etc/hosts文件做一个本机ip和 mail.deaboway.com的映射
127.0.0.1 deaboway mail.deaboway.com.
107.191.44.43 deaboway mail.deaboway.com.

127.0.0.1 localhost localhost
::1 localhost localhost

万一不行,CentOS修改主机名(hostname),参考:
需要修改两处:一处是/etc/sysconfig/network,另一处是/etc/hosts,只修改任一处会导致系统启动异常。首先切换到root用户。

/etc/sysconfig/network
用任一款你喜爱的编辑器打开该文件,里面有一行 HOSTNAME=localhost.localdomain (如果是默认的话),修改 localhost.localdomain 为你的主机名 deaboway。

/etc/hosts
打开该文件,会有一行 127.0.0.1 localhost.localdomain localhost 。
其中 127.0.0.1 是本地环路地址, localhost.localdomain 是主机名(deaboway),也就是你待修改的。localhost 是主机名的别名(alias),它会出现在Konsole的提示符下。将第二项修改为你的主机名,第三项可选。

将上面两个文件修改完后,并不能立刻生效。如果要立刻生效的话,可以用 hostname your-hostname 作临时修改,它只是临时地修改主机名,系统重启后会恢复原样的。但修改上面两个文件是永久的,重启系统会得到新的主机名。

最后,重启后查看主机名 uname -n 。

7. 邮件队列
(1)查看邮件队列:mailq
(2)强制送信:/usr/sbin/sendmail -q -v # 对MTA队列强制送信
(3)清空队列: /var/spool/mqueue (删除里面的文件)

参考:
1. http://blog.sina.com.cn/s/blog_7cc54c730101mf21.html
http://www.bkjia.com/Linuxjc/928972.html
http://www.cnblogs.com/kerrycode/p/4171661.html
大多数Linux系统都默认自带了sendmail,最近想通过sendmail将一些服务器信息直接发送到自己的邮箱里面,结果发送之后一直没什么反应,最后发现是因为我的服务器上没有合法的域名和MX解析记录,而sendmail默认会进行反向解析,所以就一直堆在队列里面发布出去。
解决办法很简单,直接关掉sendmail的反向解析就可以了,具体配置如下:

我发送邮件的命令为:
# mail -s “Daily report” [email protected] < /var/log/report.txt

查看电子邮件队列信息:
# sendmail -bp
—————————-
/var/spool/mqueue (1 requests)
—–Q-ID—– –Size– —–Q-Time—– ————Sender/Recipient———–
p6C71EPC020359 10 Tue Mar 19 14:02
(host map: lookup (gmail.com): deferred)

Total requests: 1
—————————-
出现了”host map: lookup (domain): deferred”错误,意思就是说邮件的发送被延迟了。

解决办法很简单:
# vim /etc/mail/sendmail.cf
找到 #O ResolverOptions=+AAONLY 这一行信息,并把它的将注释取消掉。

# /etc/init.d/sendmail restart
重启sendmail以后即可成功发送邮件。

不过,即使邮件能够发送出去了,由于没有合法的域名和MX解析记录,稍微好点的邮箱都会把它丢到垃圾邮件中去,所以建议如果有自己的域名的话,可以考虑分配一个子域名和MX解析。

2. http://blog.csdn.net/fairytall/article/details/10002757
今天搭Hadoop完全分布式集群的时候,启动Centos时,总是停在starting sendmail上很长时间,接下来的starting sm-client也是异常的慢。后来查资料,才知道原来是和更改hostname有关。回想一下,自己曾经确实是改了hostname。但是只是修改了配置文件(/etc/sysconfig/network)。网上有说“这是因为 sendmail 的 server name (预设为 hostname) 不能为gethostbyaddr 所解析所致,不能解析时会等 timeout, 所以等了那麼久。 所以,重點在 /etc/hosts 有沒有對應或 dns 有沒有記錄”。
所以,解决这个问题的方法是,通过修改配置文件更改了主机名后,还要在/etc/hosts文件中将主机名添加进去(把主机名添加到127.0.0.1后面)。当然,也可直接停sendmail服务。
其实更改主机名要做三件事:
1)修改/etc/sysconfig/network配置文件,HOSTNAME=“主机名”;
2)将主机名添加到/etc/hosts文件中。使主机名能够被解析。
/etc/hosts:
192.168.1.119 server.localdomain server
/etc/sysconfig/netwrok:
HOSTNAME=server.localdomain
P.S. /etc/hosts 文件的第一栏是网络地址,第二栏是主机名,第三栏是主机别名

3)这时,主机名已经修改成功了,但shell提示符还是显示原来的主机名,需重启才能更改。
利用命令; hostname 主机名 这样,主机名就被完全修改了。

3. http://blog.sina.com.cn/s/blog_4f9fc6e10100udpz.html
这几天做邮件促销,公司又不提供专门的SMTP账号,就只有用PHP的mail发了,PHP的mail函数是使用的linux的sendmail组件。但是在发的时候,遇到了奇怪的问题,163的能收到,但是qq的却收不到,以为是程序问题,找了很久也没有发现程序有问题。然后无意中发现时被QQ拦截了,linux下使用sendmail发邮件,部分邮箱收到不到的解决办法
相当的郁闷,那怎么解决呢?

然后又上发邮件的服务器,查看邮件日志(/var/log/maillog),发现里面发不出的邮件都有DSN:SERVICE UNAVAIIZE(该单词拼写错误)之类的提示,大致意思就是说服务不存在。发送成功了的就不会有这样的日志。找了很久,最好终于找到解决办法了。居然和linux的hosts文件有关。

修改/etc/hosts文件,将 x.x.x.x www.xxx.com放到该文件的最前面,x.x.x.x代表当前linux的IP,如果没有动态IP,就写127.0.0.1,最好的是写2条这样的语句,而且都放到最前面,2条分别向这样:
127.0.0.1 test.mydomain.com
x.x.x.x.x test.mydomain.com(外网IP)
后面的是域名,不一定要是你本机的域名,只要是外部能够ping通的域名就可以了,在发邮件QQ就能收到了。

4. http://www.mamicode.com/info-detail-1069126.html

 

补充阅读:

禁止sendmail服务:
1. 结束sendmail 服务:# killall sendmail
2. 在自动启动中删除sendmail:#chkconfig –del sendmail
3. 禁用sendmail:#chkconfig sendmail off

查看sendmail是否监听:netstat -tuln | grep 25

重启centos:shutdown -r now

之后,需要重启nginx和php-fpm
nginx
/usr/sbin/php-fpm
记得把两个加入到centos自启动服务中

查看nginx状态,systemctl status nginx
设为开机启动。systemctl enable nginx.service
systemctl status php-fpm
systemctl enable php-fpm.service

关闭sendmail服务
(1)[root@sample ~]# /etc/rc.d/init.d/sendmail stop  ← 关闭sendmail服务
或者[root@sample ~]# service sendmail stop  ← 关闭sendmail服务
Shutting down sendmail: [ OK ]
Shutting down sm-client: [ OK ]
(2)[root@sample ~]# chkconfig sendmail off  ← 关闭sendmail自启动
(3)[root@sample ~]# chkconfig –list sendmail  ← 确认sendmail自启动已被关闭(都为off就OK)
sendmail 0:off 1:off 2:off 3:off 4:off 5:off 6:off

欢迎关注我的微信公众号:

 

如无特殊说明,文章均为本站原创,转载请注明出处!

《CentOS7安装配置sendmail汇总——仍然没有配置成功》有5个想法

  1. 使用:https://wp-ecommerce.net/easy-wordpress-smtp-send-emails-from-your-wordpress-site-using-a-smtp-server-2197 实现第三方的SMTP服务器发送邮件

发表回复

您的电子邮箱地址不会被公开。