步骤 1:安装 mailxmsmtp

1
yum install mailx msmtp

步骤 2:配置 msmtp

编辑 msmtp 的配置文件 /etc/msmtprc~/.msmtprc,并添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# QQ 邮箱的 SMTP 配置
defaults
tls on
tls_starttls on
tls_trust_file /etc/pki/tls/certs/ca-bundle.crt

account qq
host smtp.qq.com
port 587
from 3311987957@qq.com
auth on
user 3311987957@qq.com
password xxxxxxxxxx #替换为你在 QQ 邮箱中生成的授权码。
logfile ~/.msmtp.log

# 设置默认账户
account default : qq

步骤 3:配置 mailx 使用 msmtp

为了让 mailx 使用 msmtp 作为发送工具,你需要编辑 ~/.mailrc 文件,添加以下内容:

1
2
3
set sendmail=/usr/bin/msmtp #告诉 mailx 使用 msmtp 作为发送工具
set from=3311987957@qq.com
set message-sendmail-extra-arguments="--read-envelope-from"

步骤 4:发送带有主题和正文的邮件

1
echo "This is the body of the email." | mail -s "Test Subject" 3311987957@qq.com