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

当我使用标准 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', '>'])


蝴蝶不菲
浏览 172回答 1
1回答

森栏

“电子邮件地址”有不止一种标准和语法。SMTP使用的地址的标准和语法是(当前)RFC 5321。这是您传递给的地址类型twisted.mail.smtp.sendmail。MIME使用的地址的标准和语法是(当前)RFC 5322。这是您放入 MIME 消息中的发件人、收件人、抄送等地址类型。RFC 5321 地址没有发件人姓名组件。RFC 5322 地址可以。我希望这email.utils.formataddr会返回一个有效的 RFC 5322 地址,并且将其放入msg["From"].&nbsp;但是,您必须将 RFC 5321 地址传递给twisted.mail.smtp.sendmail.&nbsp;粗略地说,这些格式为noreply@example.com.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python