我正在尝试发送一封简单的电子邮件,其中仅包含一个字符串。该字符串看起来类似于“ GB vs DEN游戏在{某时}结束”。
我的功能如下:
def send_email(msg,address):
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com',port,context=context) as server:
print('Sending Email')
server.login(user,password)
try:
server.sendmail(user,address,msg)
print('Email Sent!')
except:
print('Message Sending Failed')
我知道这可行,因为在程序的其他地方使用它是可行的。我使用它的时间之间唯一的区别是消息。
我正在使用它的上下文是这个。
if played_status == 'COMPLETED' or played_status == 'FINAL' or played_status == 'POSTGAME-REVIEWING':
message = 'The {} vs {} game just ended at {}.'.format(away_team,home_team,time.asctime())
send_email(message, receiver)
print(message)
执行此代码段时,输出如下:
Sending Email
Email Sent!
The GB vs DEN game just ended at Sun Dec 7 01:19:10 2019.
基于此输出,电子邮件应该发送正确了吗?有什么更好的方法可以弄清楚为什么它不起作用?
编辑:添加打印语句以显示sendmail返回代码后,这是我的代码。
def send_email(msg,address):
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com',port,context=context) as server:
print('Sending Email')
print(server.login(user,password))
try:
server.sendmail(user,address,msg)
print('Email Sent!')
except:
print('Message Sending Failed')
这是打印的返回值。根据我在网上发现的信息,它是否成功?
The WAS vs GB game just ended at Sun Dec 8 12:59:23 2019.
Sending Email
(235, b'2.7.0 Accepted')
Email Sent!