Outlook 显示“这是一封 HTML 电子邮件”。

Outlook displays "This is an HTML email."

提问人:bartu celasun 提问时间:9/27/2023 更新时间:9/27/2023 访问量:28

问:

当我通过python发送电子邮件时。Outlook 显示“这是一封 HTML 电子邮件”。Gmail 运行良好。

这是正文:

使用排序的 URL 创建电子邮件正文

 body = "<html><body><div style='background-color: #0072c6; padding: 10px;'>"
 body += "<h2 style='color: #fff; margin: 0;'>Latest News:</h2>"
 body += "</div><ul style='list-style-type: none; padding: 0;'>"
 for url_info in matching_urls:
     summary, title, date, name, keywords, url = url_info
     body += f"<li style='margin-bottom: 10px;'><strong><a href=\"{url}\" style='color: 
     body += f"<span style='font-size: 14px;'><strong>Date:</strong> {date}</span><br>"
     body += f"<span style='font-size: 14px;'><strong>Summary:</strong> {summary}</span><br>"
     body += f"<span style='font-size: 14px;'><strong>Keywords:</strong> 

 body += "</ul></body></html>"

 em = EmailMessage()
 em["From"] = sender_email
 em["To"] = ", ".join(reciever_emails)  # Combine recipient emails with commas

 em["Subject"] = subject
 em.set_content("This is an HTML email.", subtype="html")
 em.add_alternative(body, subtype="html")
 file_path = "/tmp/News.xlsx"
 # Attach the Excel file to the email
 with open(file_path, "rb") as excel_file:
     em.add_attachment(
         excel_file.read(),
         maintype="application",
         subtype="vnd.openxmlformats-officedocument.spreadsheetml.sheet",
         filename="News.xlsx"
    )

 context = ssl.create_default_context()

 with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as smtp:
     smtp.login(sender_email, password)
     smtp.sendmail(sender_email, reciever_emails, em.as_string())`
python 电子邮件 html-email

评论

1赞 tripleee 9/27/2023
因为你明确地做到了这一点。它完全按照你的要求去做。set_content
0赞 tripleee 9/27/2023
(顺便说一句,我认为你不再需要在上下文中创建和传递;这种笨拙的黑客在很多年前是必要的,但现在 API 应该是理智的。
1赞 tripleee 9/27/2023
许多营销人员有一个令人反感的习惯,即放置一个无用的部分,基本上是说,“如果你更喜欢纯文本而不是HTML,因为你有身体残疾,我们讨厌你是残疾人。如果你只是碰巧喜欢纯文本,我们只是讨厌你。放置一个有用的明文部分,或者根本不提供一个。在后一种情况下,如果垃圾邮件过滤器讨厌你,很容易说你应得的。text/plain

答: 暂无答案