ubuntu linux命令行發送郵件 heirloom-mailx
1,安裝mailutils
- 系統
cat /etc/issue
Ubuntu 18.04.4 LTS \n \l
apt install -y mailutils
2,mailutils配置
cat /usr/share/doc/mailutils/examples/mail.rc
3,發送郵件
- 發送郵件報錯
# echo "This is the mail body" | mail -s "Subject" 郵箱
mail: cannot send message: Process exited with a non-zero status
- 查看日誌
# tail /var/log/mail.err
May 22 15:52:21 zj01 postfix/sendmail[102701]: fatal: open /etc/postfix/main.cf: No such file or directory
May 22 16:34:18 zj01 postfix/sendmail[113790]: fatal: open /etc/postfix/main.cf: No such file or directory
4,安裝postfix
postfix
apt-get install postfix
5,安裝heirloom-mailx
- 添加源
# vim /etc/apt/sources.list
deb http://cz.archive.ubuntu.com/ubuntu xenial main universe
# apt update
# apt install heirloom-mailx -y
5.1,配置heirloom-mailx
vim /etc/s-nail.rc
添加到配置文件末尾- 使用網易郵箱,郵箱開啓
POP3/SMTP/IMAP
服務
set from="網易郵箱@163.com"
set smtp="smtps://smtp.163.com:465"
set smtp-auth-user="網易郵箱@163.com"
set smtp-auth-password="授權碼"
set smtp-auth=login
- 測試
echo "郵件內容" | s-nail -s "郵件主題" QQ郵箱@qq.com
-
測試成功
-
報錯
s-nail: smtp-server: 554 DT:SPM 163 smtp9,DcCowADH0GhpEdFeEW2WDg--.437S2 1590759786,please see http://mail.163.com/help/help_spam_16.htm?ip=223.247.87.181&hostid=smtp9&time=1590759786
/root/dead.letter 13/382
... message not sent
- 需要安裝
mailutils
,成功發送測試郵件
# apt install -y mailutils
6,關閉郵件提示
- 終端會有郵件提示
您在 /var/mail/root 中有新郵件
mail
查看郵件
# mail
"/var/mail/root": 14 messages 14 new
>N 1 Cron Daemon 六 5月 30 00:0 23/958 Cron <root@zj> /bin/bash /root/mail.sh
- 關閉提示
echo "unset MAILCHECK" >> /etc/profile
參考: