【发布时间】: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。
-
<pre>标签有什么用? -
我使用pre标签来保留换行符和新行
标签: python html python-3.x smtplib