为了账号安全,请及时绑定邮箱和手机立即绑定

如何让 email.utils.formataddr 显示带有 twisted.mail.smtp

如何让 email.utils.formataddr 显示带有 twisted.mail.smtp

蝴蝶不菲 2022-06-14 15:03:41
当我使用标准 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', 'noreply@example.com'))        msg["Reply-to"] = 'noreply@example.com'        msg["To"] = 'recipient@example.com'        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)收到此错误失败:twisted.mail._except.AddressError:在''的解析错误('“发件人名称”',['','<','noreply','@','发件人','。' ,'com', '>'])
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

“电子邮件地址”有不止一种标准和语法。SMTP使用的地址的标准和语法是(当前)RFC 5321。这是您传递给的地址类型twisted.mail.smtp.sendmailMIME使用的地址的标准和语法是(当前)RFC 5322。这是您放入 MIME 消息中的发件人、收件人、抄送等地址类型。

RFC 5321 地址没有发件人姓名组件。RFC 5322 地址可以。我希望这email.utils.formataddr会返回一个有效的 RFC 5322 地址,并且将其放入msg["From"]. 但是,您必须将 RFC 5321 地址传递给twisted.mail.smtp.sendmail. 粗略地说,这些格式为noreply@example.com.


查看完整回答
反对 回复 2022-06-14
  • 1 回答
  • 0 关注
  • 171 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号