在 EmailMultiAlternatives 中,attach 和 attach_alternative 都在时间上不起作用

In EmailMultiAlternatives attach and attach_alternative both at time is not working

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()

正在使用 姜戈 1.11.3 python3 EmailMultiAlternatives

单独两个都工作正常但是一旦我们 运行 带有附件的代码和 attach_alternative HTML 只有 pdf 附件在电子邮件服务器中接收

问题是您在邮件中设置了 content_subtype,然后附加了一个单独的 text/html 替代选项。同时执行这两项操作没有意义 - 这意味着邮件客户端在电子邮件中收到两个 text/html 备选方案,但不知道要呈现哪个。它只会使用第一个。

要么删除 content_subtype,要么将 html 正文放在 body 参数中并删除 attach_alternative。在后一种情况下,您可以停止使用 EmailMultiAlternative 并仅使用 EmailMessage class.