sendmail 本地 SMTP 测试安装 (Manjaro)

最近想要给自己的个人脚本加上邮件提醒, 尝试一下 Manjaro 下安装 sendmail .

安装

yay -S sendmail
sudo vim /etc/hosts , 把 myhostname 追加到 127.0.0.1 后边
sudo touch /etc/mail/local-host-names
echo "myhostname" >> /etc/mail/local-host-names
echo "localhost" >> /etc/mail/local-host-names
systemctl start sendmail

查看日志

journalctl -u sendmail.service --no-pager -f

测试

使用 python 脚本来发送邮件, 测试本地的 SMTP

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.message import EmailMessage

# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = f'The contents of {textfile}'
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

没有错误的话, 检查自己的邮箱有无收到(可能被放入垃圾箱).

My unqualified host name (localhost) unknown 的问题

如果 出现以下日志:

My unqualified host name (localhost) unknown; sleeping for retry

可以检查一下 /etc/hosts 的 127.0.0.1 地址有没有加上 localhost 和 localhost.localdomain

参考