![](/img/trans.png)
[英]Python SMTP - Receive email in Sent Items after email is sent to user
[英]Weird display of 'From' field in smtp email sent in python
我在 smtp 電子郵件中看到 From 字段的奇怪顯示。顯示就像;
發件人:test@gmail.com 收件人:test@gmail.com,主題:郵件 test@gmail.com 至:
to 字段為空,但另一個“to”收件人,即 test1@gmail.com 成功收到了電子郵件。 下面是我的代碼。
import smtplib
def SendEmailScenario1():
gmail_user = "test@gmail.com"
gmail_password = '******'
sent_from = gmail_user
to = ["test1@gmail.com"]
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 = "test@gmail.com"
gmail_password = '******'
sent_from = gmail_user
to = ["test1@gmail.com"]
subject = 'Message'
body = "Hi There! Done 2"
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...')
SendEmailScenario1()
SendEmailScenario2()
如何在不使用MIMEText、MIMEMultipart 的情況下使其正常顯示
RFC822指定標頭應以'\\r\\n'
終止:
field = field-name ":" [ field-body ] CRLF
[正文] 由空行(即,在 CRLF 之前沒有任何內容的行)與標題分開
所以消息應該像這樣構造:
headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
email_text = headers + body
另請參閱smtplib 文檔中的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.