织辉科技»论坛 技术科创 技术运维 查看内容

0 评论

2108 收藏

分享

ubuntu24中使用ssmtp mailutils实现邮件发送,结合sh可做到监控警报功能

 

织辉科技温馨提醒:把部分功能需要登陆才能操作……

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 科技江湖 于 2025-1-31 14:27 编辑

首先安装组件
apt install ssmtp mailutils

image.png
代码跑完没报错,说明已成功安装
image.png
接下来进行配置vi /etc/ssmtp/ssmtp.conf
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=******@qq.com

# 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.qq.com:465

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
rewriteDomain=
hostname=smtp.qq.com:465
# 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: addres
UseTLS=Yes
root=******@qq.com
AuthUser=******@qq.com
AuthPass=******

image.png
通过ssh发送邮件测试:
# 不带附件发邮件:
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 <from@mail.com>" to@mail.com

问题处理:
若发送邮件有如下提示:
image.png
查看错误日志,cat /var/log/mail.err,也有如下信息:
sSMTP[1234]: 501 mail from address must be same as auth orization user

image.png
则需要到/etc/ssmtp/revaliases配置文件中对发件人、服务器地址和端口进行指明,添加如下部分:
xuruijie:******@qq.com:smtp.qq.com:465
root:******@qq.com:smtp.qq.com:465
www:******@qq.com:smtp.qq.com:465

image.png
若/var/log/mail.log出现550错误
2024-09-29T08:25:04.966671+00:00 xuruijie sSMTP[385957]: 501 Mail from address must be same as authorization user.
2024-09-29T08:36:16.803026+00:00 xuruijie sSMTP[386057]: 501 Mail from address must be same as authorization user.
2024-09-29T08:37:55.652918+00:00 xuruijie sSMTP[2193]: 501 Mail from address must be same as authorization user.

image.png
image.png
这条信息通常出现在电子邮件系统中,表示邮件被邮件服务器拒绝发送,因为它可能包含不当的词汇或内容。邮件服务器通常会使用内容过滤系统来检测和阻止可能的垃圾邮件、恶意软件或违反邮件发送政策的内容。
最后在测试
echo "This is a email content." | mail -s "Email Title" 2318338820@qq.com

image.png


回复

举报 使用道具

全部回复
暂无回帖,快来参与回复吧

科技江湖
金牌编辑
主题 167
回复 0
粉丝 0