首页 > 解决方案 > 如何为电子邮件内容格式化我的 HTML 文本 (smtplib)

问题描述

我正在使用 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 文件?

标签: pythonhtmlpython-3.xsmtplib

解决方案


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


推荐阅读