當我使用標準 smtplib.SMTP.sendmail 函數時,我可以使用 email.utils.formataddr(senderName, local-part@domain) 作為 from_addr 來調用它,以在電子郵件客戶端中顯示發件人的姓名。當我使用twisted.mail.smtp.sendmail 做同樣的事情時,而不是senderName 出現,我得到本地部分。到目前為止,我只在 Gmail 中對此進行了測試,但由于這是我公司使用的,所以這是我要求的一部分。我嘗試通過 twisted.mail.smtp.sendmail 發送格式化的地址并收到解析錯誤。我使用了 twisted.mail.smtp.quoteaddr,但發現它只是將格式化的名稱和地址剝離回地址。雖然沒有解析錯誤。 #print('populate the message') msg = MIMEMultipart('alternative') msg["Subject"] = 'A Subject' msg["From"] = email.utils.formataddr(('Sender Name', '[email protected]')) msg["Reply-to"] = '[email protected]' msg["To"] = '[email protected]' content = MIMEText("Hey, what's up?" ,"html") msg.attach(content) msg_timeout = round(float(30)) # create the sender and send it result = twisted.mail.smtp.sendmail("mail-sc", msg["From"], msg["To"].split(","), msg, port=25, requireAuthentication=False)收到此錯誤失?。簍wisted.mail._except.AddressError:在''的解析錯誤('“發件人名稱”',['','<','noreply','@','發件人','。' ,'com', '>'])
1 回答

森欄
TA貢獻1810條經驗 獲得超5個贊
“電子郵件地址”有不止一種標準和語法。SMTP使用的地址的標準和語法是(當前)RFC 5321。這是您傳遞給的地址類型twisted.mail.smtp.sendmail
。MIME使用的地址的標準和語法是(當前)RFC 5322。這是您放入 MIME 消息中的發件人、收件人、抄送等地址類型。
RFC 5321 地址沒有發件人姓名組件。RFC 5322 地址可以。我希望這email.utils.formataddr
會返回一個有效的 RFC 5322 地址,并且將其放入msg["From"]
. 但是,您必須將 RFC 5321 地址傳遞給twisted.mail.smtp.sendmail
. 粗略地說,這些格式為[email protected]
.
添加回答
舉報
0/150
提交
取消