有问题的片段
server = 'smtp.mail.yahoo.com' if 'yahoo.com' in mail else 'smtp.gmail.com'
port = 465 if 'yahoo.com' in mail else 587
with smtplib.SMTP(server, port) as connection:
connection.starttls()
connection.login(MY_EMAIL, PASSWORD)
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=mail,
msg=f'Subject: Happy Birthday\n\n{reformatted_template}'
对于 Gmail 域,一切正常。但是当向 yahoo 域发送电子邮件时,它会抛出此错误:
smtplib.SMTPConnectError: (-1, b'\x02\x01Z\x15\x03\x03\x00\x02\x01\x00')
经历了很多关于如何解决这个问题的线程,但没有一个起作用。其中一些是:
当我使用 smtplib.SMTP_SSL 时,会抛出此错误:
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
我真的很感谢提前提供的帮助,迫不及待地想让它工作。
server = 'smtp.mail.yahoo.com' if 'yahoo.com' in mail else 'smtp.gmail.com'
port = 465 if 'yahoo.com' in mail else 587
with smtplib.SMTP_SSL(server, port) as connection: #changeed to SMTP_SSL
#connection.starttls() #this is not needed
connection.login(MY_EMAIL, PASSWORD)
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=mail,
msg=f'Subject: Happy Birthday\n\n{reformatted_template}'
还应该在 yahoo 和 gmail 中定义应用程序密码才能正常工作(小心,yahoo 应用程序和 google 应用程序不会为您提供相同的密码)。