问题描述
发件人:[email protected]收件人:[email protected],主题:邮件[email protected] 到:
“收件人”字段为空白,但另一个“收件人”收件人,即[email protected]已成功收到电子邮件。 下面是我的代码。
import smtplib
def SendEmailScenario1():
gmail_user = "[email protected]"
gmail_password = '******'
sent_from = gmail_user
to = ["[email protected]"]
subject = 'Message'
body = "Hi There! Done1"
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from,",".join(to),subject,body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com',465)
server.ehlo()
server.login(gmail_user,gmail_password)
server.sendmail(sent_from,to,email_text)
server.close()
print ('Email sent!')
except:
print ('Something went wrong...')
def SendEmailScenario2():
gmail_user = "[email protected]"
gmail_password = '******'
sent_from = gmail_user
to = ["[email protected]"]
subject = 'Message'
body = "Hi There! Done 2"
email_text = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from,email_text)
server.close()
print ('Email sent!')
except:
print ('Something went wrong...')
SendEmailScenario1()
SendEmailScenario2()
如何在不使用 MIMEText,MIMEMultipart
的情况下将其正常显示解决方法
RFC822 specifies,标头应以'\r\n'
结尾:
field =字段名“:” [field-body] CRLF
此外,标头应由一个空白的'\r\n'
separated from the body:
[正文]与标头之间用空行分隔(即, 行,CRLF之前没有任何内容)
因此,消息的结构应如下:
headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
email_text = headers + body
另请参见smtplib docs中的示例。