小编Asw*_* MA的帖子

如何在html中添加动态内容以在Python中发送邮件

我正在 python 中使用“smtplib”发送带有 html 内容的邮件,我想向该 html 添加动态内容。

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

message = MIMEMultipart("alternative")
message["Subject"] = "Error Notification"
message["From"] = sender
message["To"] = sender

# Create the plain-text and HTML version of your message
html = """\
    <html>
      <body>
        <p>Hi,<br>
           <span>Something went wrong !</span><br>
        </p>
      </body>
    </html>
    """
part1 = MIMEText(html, "html")

# Add HTML/plain-text parts to MIMEMultipart message
message.attach(part1)
try:
    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(sender, receivers, message.as_string())
    print "Successfully sent email"
except smtplib.SMTPException:
    print …
Run Code Online (Sandbox Code Playgroud)

html python email smtplib

4
推荐指数
1
解决办法
3722
查看次数

标签 统计

email ×1

html ×1

python ×1

smtplib ×1