我正在尝试使用 yagmail 向测试本地 SMTP 服务器发送电子邮件。对于后者,我使用的是 localmail 包,它提供了一种运行本地服务器的方法,该服务器在端口 2025 上侦听 SMTP,在 2143 上侦听 IMAP。这是我的代码:
import yagmail
to = "test@mail.com"
subject = "hi there"
contents = "some content"
attachments = ['a.txt', 'b.txt']
def send_mail(to, subject, contents, attachments, host="localhost", port=2025):
yag = yagmail.SMTP("testuser", "testpass", host=host, port=port)
yag.send(to, subject, contents, attachments=attachments)
send_mail(to, subject, contents, attachments)
代码在尝试发送消息时遇到 SSLError,即在yag.send
级别。因此,我可以smtplib
使用不安全连接(即不使用 SSL)的库来执行此操作 -smtp = smtplib.SMTP(server, port)
它可以工作。有没有办法用yagmail达到同样的效果?