【问题标题】:How to format my HTML text for email content (smtplib)如何为电子邮件内容格式化我的 HTML 文本 (smtplib)
【发布时间】:2021-01-01 16:46:57
【问题描述】:

我正在使用 SMTPlib 发送电子邮件,但我想用 HTML 格式化内容,以便看起来更好。这是我当前的代码:

msg = EmailMessage()  # creating an object of EmailMessage class
msg['Subject'] = 'Day 1 of the project'
msg['From'] = Sender_Email  # Defining sender email
msg['To'] = Receiver_Email  # Defining receiver email
msg.set_content(f'''<pre><h3> Hi Dani,</h3>
<p style = "font-family:candara,times,helvetica; font-size:14px;">today is day <b>1</b> since you started this project.

Here is a good quote for you
<i>Fight until the end.</i>

I wish you a good day :)

Until tomorrow,
<p style = "font-family:candara,times,helvetica; font-size:14px; color:#FF0000;"><i>your fateful script</i></p></pre>''', subtype="html")

with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
    smtp.login(Sender_Email, Password)  # Login to SMTP server
    smtp.send_message(msg)  # Sending email using send_message method by passing EmailMessage object

还有其他的格式化方式吗?例如通过导入 css 文件?

【问题讨论】:

  • 我认为电子邮件中不可能包含 css 文件,因此它必须是内联样式。
  • this
  • &lt;pre&gt; 标签有什么用?
  • 我使用pre标签来保留换行符和新行

标签: python html python-3.x smtplib


【解决方案1】:

我决定使用在线 html 编辑器(例如 https://htmlg.com/html-editor/)对其进行格式化,然后将其复制粘贴到我的电子邮件内容中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2011-06-02
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多