from django.core.mail import EmailMultiAlternatives
subject, from_email, to,bcc =
request.POST['sub'],'fulfillment@***.com', lead.mailid,email_ccaddress
msg = EmailMultiAlternatives(subject, "", from_email, [to])
filepdf = open("Plan.pdf",'rb').read()
msg.attach("Plan.pdf",filepdf,'application/pdf')
msg.attach_alternative(request.POST['bodyofmail'], "text/html")
msg.content_subtype = 'html'
msg.send()
使用 Django 1.11.3 python 3 电子邮件多种选择
两者都工作正常,但是一旦我们使用附件和 Attach_alternative HTML 运行此代码,电子邮件服务器中只会接收 pdf 附件
最佳答案
问题在于您在消息上设置 content_subtype
,然后附加单独的 text/html
替代方案。同时执行这两项操作没有意义 - 这意味着邮件客户端在电子邮件中收到两个 text/html
替代项,但不知道要呈现哪个。它只会使用第一个。
删除 content_subtype
,或者将 html 正文放入 body
参数中并删除 attach_alternative
。在后一种情况下,您可以停止使用 EmailMultiAlternative
并仅使用 EmailMessage
类。
关于django - 在 EmailMultiAlternatives 中, Attach 和 Attach_alternative 两者同时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842746/