我正在尝试使用 smtplib 发送 HTML 电子邮件。但我需要 HTML 内容有一个使用字典中的值填充的表。我确实查看了Python网站上的示例。但它没有解释如何在 HTML 中嵌入 Python 代码。有什么解决方案/建议吗?
我也看了这个问题。我可以这样格式化吗?
.format(字典名称)
从您的链接:
\n\n\n这里\xe2\x80\x99是如何使用替代\n纯文本版本创建 HTML 消息的示例:2
\n
import smtplib\n\nfrom email.mime.multipart import MIMEMultipart\nfrom email.mime.text import MIMEText\n\n# me == my email address\n# you == recipient\'s email address\nme = "my@email.com"\nyou = "your@email.com"\n\n# Create message container - the correct MIME type is multipart/alternative.\nmsg = MIMEMultipart(\'alternative\')\nmsg[\'Subject\'] = "Link"\nmsg[\'From\'] = me\nmsg[\'To\'] = you\n\n# Create the body of the message (a plain-text and an HTML version).\ntext = "Hi!\\nHow are you?\\nHere is the link you wanted:\\nhttp://www.python.org"\nhtml = """\\\n<html>\n <head></head>\n <body>\n <p>Hi!<br>\n How are you?<br>\n Here is the <a href="http://www.python.org">link</a> you wanted.\n </p>\n </body>\n</html>\n"""\nRun Code Online (Sandbox Code Playgroud)\n及其发送部分:
\n# Record the MIME types of both parts - text/plain and text/html.\npart1 = MIMEText(text, \'plain\')\npart2 = MIMEText(html, \'html\')\n\n# Attach parts into message container.\n# According to RFC 2046, the last part of a multipart message, in this case\n# the HTML message, is best and preferred.\nmsg.attach(part1)\nmsg.attach(part2)\n\n# Send the message via local SMTP server.\ns = smtplib.SMTP(\'localhost\')\n# sendmail function takes 3 arguments: sender\'s address, recipient\'s address\n# and message to send - here it is sent as one string.\ns.sendmail(me, you, msg.as_string())\ns.quit()\nRun Code Online (Sandbox Code Playgroud)\n编辑 2022:对于新来者,请使用 python\xe2\x80\x99s 最新稳定版本文档提供建议。
\n| 归档时间: |
|
| 查看次数: |
9871 次 |
| 最近记录: |