1.系统环境。

[root@web02 ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)
[root@web02 ~]# uname -r
2.6.32-642.el6.x86_64
[root@web02 ~]# uname -m
x86_64


2.通过修改配置文件/etc/mail.rc可以使用外部SMTP服务器,轻松实现linux发邮件功能。

[root@web02 ~]# tail /etc/mail.rc
# For Linux andBSD, this should be set.
set bsdcompat
 
# sendmail config
set from=user@foxmail.com
set smtp=smtp.qq.com
set smtp-auth-user=user@foxmail.com
set smtp-auth-password=xxxxxxxxxxxxxxx
set smtp-auth=login

注意:目前大部分的外部邮件服务使用第三方客户端时,都需要使用授权码,上面的smtp-auth-password使用的就是授权码,而不是邮件帐号的密码。


wKiom1jCmM6wBMOyAApdHEeKbQI528.jpg-wh_50

3.相关命令和选项的作用。

-s<邮件主题>:指定邮件的主题;
-c<地址>:添加邮件抄送人,多个人时用逗号隔开;
-b<地址>:添加邮件暗送人;
-a <附件>: 添加附件Attachthe given file to the message.


(1)   方法一:正文内容重定向输入。

[root@web02 ~]# mail -s "标题" -a /etc/hosts -c user1@163.com,user2@163.com user@foxmail.com </etc/hosts
#当主送和抄送有多个人时,请使用逗号隔开。

2)方法二:正文内容通过echo命令输入

[root@web02 ~]# echo "正文"|mail -s "标题" -a /etc/hosts -c user1@163.com,user2@163.com user@foxmail.com

4.启动postfix服务

[root@web02 ~]# /etc/init.d/postfix start
Starting postfix:                                          [  OK  ]
[root@mysql01 ~]# chkconfig --level 3 postfix on
[root@mysql01 ~]# chkconfig|grep postfix
postfix            0:off    1:off    2:off    3:on    4:off    5:off    6:off

到此为止,发邮件配置完毕。可以正常测试。