最近在实施服务端日志监控脚本,需要对异常情况发送邮件通知相关责任人,记录下centos通过sendmail发送邮件的配置过程。
一、安装sendmai
|
1
2
3
4
5
6
7
|
安装sendmail#安装yum -y install sendmail#启动/etc/init.d/sendmail start<br>systemctl start sendmail |
二、设置发件人信息
上述发送邮件默认会使用linux当前登录用户,通常会被当成垃圾邮件,指定发件人邮箱信息命令:vim /etc/mail.rc,或者执行如下:
|
1
2
3
4
5
6
7
8
9
10
|
cat >/etc/mail.rc<<EOFset from=邮箱账号
set smtp=smtp.163.com
set smtp-auth-user=邮箱账号
set smtp-auth-password=邮箱授权码
set smtp-auth=login
EOF重启sendmail |
|
1
|
systemctl restart sendmail |
三、发送邮件
1.通过文件内容发送
发送命令:
|
1
|
mail -s \'mail test\' xxx@yyy.com < con.txt ("mail test"为邮件主题,xxx@yyy.com为收件人邮箱,con.txt保存邮件内容)
|
例子:
|
1
|
mail -s \'backup over\' 13161168831@163.com</tmp/mail.txt >/dev/null 2>&1
|
2.通过管道符直接发送
发送命令:
|
1
|
echo "this is my test mail" | mail -s \'mail test\' xxx@yyy.com >/dev/null 2>&1
|