最近需要在Linux上找到一种基于命令行的SMTP邮件发送方式,经过多方尝试,用SSMTP实现了我的需求,在此记录一下。
先是在Ubuntu上安装需要的SSMTP软件:
sudo apt install ssmtp
安装完成之后需要对以下两个配置文件进行编辑。
$ cat /etc/ssmtp/ssmtp.conf
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
[email protected]
# 这里填腾讯企业邮箱的发送服务器
# 注意腾讯企业邮箱和QQ邮箱不是同一个服务器,请注意区分
# 官方提供的SMTP服务器端口是465 但实际上这里使用的端口是587
mailhub=smtp.exmail.qq.com:587
# 填写邮箱和密码(如果你用的是QQ邮箱,你可能会需要在这里填写“授权码”或“客户端专用密码”而不是“邮箱密码”)
[email protected]
AuthPass=你的密码
# 启用 STARTTLS
UseTLS=YES
UseSTARTTLS=YES
# Where will the mail seem to come from?
# 重定向的发件域
#rewriteDomain=demo.com
# The full hostname
# 发件的域名
#hostname=demo.com
# 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
下面设置用户别名(否则会出现 501 mail from address must be same as authorization user 错误)
$ cat /etc/ssmtp/revaliases
# sSMTP aliases
#
# Format: local_account:outgoing_address:mailhub
#
# Example: root:[email protected]:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:[email protected]:smtp.exmail.qq.com:587
这里的root是指在Linux中操作的用户,如果你当前Linux系统的用户不是root,可能需要更改。
配置文件中的[email protected],在实际应用中应该用你自己的邮箱来替代。
然后使用下面的命令就能发送测试邮件了:
echo "hello my friend"| ssmtp -v [email protected]
在实际应用中应该去除 -v 参数
echo -e "Subject: this is the subject\n\nthis is the body" | ssmtp [email protected]