Ubuntu 20.04 安装ssmtp通过SMTP方式发送邮件

本文介绍了如何在Ubuntu20.04系统中移除默认邮件服务,安装并配置SSMTP以使用SMTP发送邮件,包括无验证和TLS模式,以及发送邮件的测试、问题排查和日志检查方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统中使用脚本监控内存和磁盘使用情况,需要用到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 <from@mail.com>" to@mail.com

5、问题处理和日志检查

 

查看发送邮件的日志和记录

tail -n 10 /var/log/mail.log

查看错误日志

tail -n 10 /var/log/mail.err

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值