我正在尝试使用 smtplib 发送 HTML 电子邮件。但我需要 HTML 内容有一个使用字典中的值填充的表。我确实查看了Python网站上的示例。但它没有解释如何在 HTML 中嵌入 Python 代码。有什么解决方案/建议吗?
我也看了这个问题。我可以这样格式化吗?
.format(字典名称)
我修改了python文档中的示例,以测试电子邮件模块中的unicode.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"
umlauts='German Umlauts: üöä ÜÖÄ ß'
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = umlauts
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text …Run Code Online (Sandbox Code Playgroud) 我正在使用此帖子发送使用Python发送电子邮件的信息
到目前为止,说明是完美的.我还有两件事要做:
变量将是今天的日期.就是这个:
today = datetime.datetime.today ()
tday = today.strftime ("%m-%d-%Y")
Run Code Online (Sandbox Code Playgroud)
我知道使用mailx,您可以使用-a选项附加.
我有下面的 Python 代码,用于从 file 的内容向我的 ID 发送电子邮件filename。我正在尝试发送带有文本颜色格式的电子邮件。
有什么想法请指教。
def ps_Mail():
filename = "/tmp/ps_msg"
f = file(filename)
if os.path.exists(filename) and os.path.getsize(filename) > 0:
mailp = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
msg = MIMEMultipart('alternative')
msg['To'] = "karn@abc.com"
msg['Subject'] = "Uhh!! Unsafe rm process Seen"
msg['From'] = "psCheck@abc.com"
msg1 = MIMEText(f.read(), 'text')
msg.attach(msg1)
mailp.communicate(msg.as_string())
ps_Mail()
Run Code Online (Sandbox Code Playgroud)