系统中使用脚本监控内存和磁盘使用情况,需要用到smtp来发送通知邮件。mailx无法在ubuntu系统上使用,经过尝试可以安装ssmtp来使用SMTP服务向外发送邮件。
1、安装好的Ubuntu20.04中可能已经自带了邮件系统,先删除它们
- sudo apt autoremove postfix
- sudo apt autoremove sendmail
2、安装我们需要的ssmtp和mailutils
sudo apt install ssmtp mailutils
3、编辑ssmtp的配置文件:
vim /etc/ssmtp/ssmtp.conf
3.1非TLS
我这里是不需要身份验证,使用默认使用25端口,配置如下:
只更新了mailhub=你的SFTP服务器
- #
- # Config file for sSMTP sendmail
- #
- # The person who gets all mail for userids < 1000
- # Make this empty to disable rewriting.
- root=poster
-
- # The place where the mail goes. The actual machine name is required no
- # MX records are consulted. Commonly mailhosts are named mail.domain.com
- mailhub=smtp.exmail.qq.com
-
- # Where will the mail seem to come from?
- #rewriteDomain=
-
- # The full hostname
- hostname=服务名
-
- # Are users allowed to set their own From: address?
- # YES - Allow the user to specify their own From: address
- # NO - Use the system generated From: address
- #FromLineOverride=YES
-
如果需要身份认证,需要添加如下的配置
- AuthUser=address@mail.com
- AuthPass=邮箱密码
3.2 开启了TLS
如果开启了TLS发送邮件,需要修改配置:
-
-
- #
- # Config file for sSMTP sendmail
- #
- # The person who gets all mail for userids < 1000
- # Make this empty to disable rewriting.
- root=poster
-
- # The place where the mail goes. The actual machine name is required no
- # MX records are consulted. Commonly mailhosts are named mail.domain.com
- mailhub=smtpdm.aliyun.com:465
-
- # Where will the mail seem to come from?
- #rewriteDomain=
-
- # The full hostname
- hostname=服务名
-
- # Are users allowed to set their own From: address?
- # YES - Allow the user to specify their own From: address
- # NO - Use the system generated From: address
- #FromLineOverride=YES
-
- AuthUser=address@mail.com
- AuthPass=邮箱密码
- UseTLS=Yes
- AuthMethod=LOGIN
4、发送邮件测试
- # 不带附件发邮件:
- echo "This is a email content." | mail -s "Email Title" to@mail.com
- # 带附件发送邮件(注意在Ubuntu下附件使用附件参数-A,与Centos中的-a不同,注意大小写):
- echo "This is a email content." | mail -s "Email Title" -A /path/to/test.gz to@mail.com
- # 要显示回复邮箱地址,加上-r参数,该邮箱可以为任意指定邮箱:
- echo "This is a email content." | mail -s "Email Title" -r from@mail.com to@mail.com
- # 指定发件人名字和发件人邮箱地址(与配置中的邮箱地址一致),避免以root@hostname的来显示发件人名字
- echo "This is a email content." | mail -s "Email Title" -a "From: Someone
" to@mail.com
5、问题处理和日志检查
查看发送邮件的日志和记录
tail -n 10 /var/log/mail.log
查看错误日志
tail -n 10 /var/log/mail.err