Centos 7 Postfix使用第三方的smtp

参见前一篇文章,阿里云的ecs全部关闭了25端口,因此我们只能使用postfix来通过第三方的smtp服务发送一些系统邮件,比如说报警邮件.

这些第三方服务商一般提供25,587端口的smtp的服务,这里我们选择使用587端口

假设你的服务器的hostname是beta.iamhippo.com, 在/etc/aliases 中设定

root    [email protected]

表示所有发向root的邮件都转发到[email protected], 下面是详细的步骤:

1) 安装必须的library

centos/redhat/fedora:

yum install cyrus-sasl-plain

debian/ubuntu:

apt-get install libsasl2-modules

如果你不安装cyrus或者libsasl库,你会得到“no mechanism found error”

2) 设置smtp的用户名,密码

postfix的smtp的用户名和密码位于/etc/postfix/sasl_password

vi /etc/postfix/sasl_password

按照下面的格式添加验证信息:

[mail.isp.example]:587 username:password

我们需要hash一些这个文件来给postfix使用

postmap /etc/postfix/sasl_passwd

一切都正常的情况下,你会在这个目录/etc/postfix目录下看到sasl_passwd.db文件

必要的安全设置:

chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db

3) 设置generic map

如果我们是通过配置csf,让系统自动发送报警邮件,那么一般是来自于[email protected], 我们想在邮件中替换这个邮件的话,可以使用generic的功能,比如说我们想把[email protected] 改成[email protected], 那么可以这么写:

[email protected] [email protected]

然后和sasl_password 一样,我们需要hash 一下

postmap /etc/postfix/generic
chown root:root /etc/postfix/generic /etc/postfix/generic.db
chmod 0600 /etc/postfix/generic /etc/postfix/generic.db

4) 配置reply server

这个配置起来就比较简单了,直接打开/etc/postfix/main.cf文件,在最末尾添加:

# specify SMTP relay host
relayhost = [mail.isp.example]:587


# enable SASL authentication
smtp_sasl_auth_enable = yes
# disallow methods that allow anonymous authentication.
smtp_sasl_security_options = noanonymous
# where to find sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# Enable STARTTLS encryption
smtp_use_tls = yes
# where to find CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# where to find generic
smtp_generic_maps = hash:/etc/postfix/generic

保存配置,然后重启postfix

systemctl restart postfix

当然了,建议在重启之前先把postfix 中的queue mails 都删掉。。要不然积累了大量的邮件会一次性发出的

postsuper -d ALL

或者

postfix -f

或者

postfix flush

查看mail queue:

mailq

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.