我有下面的 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)
这是我用来发送 HTML 电子邮件的代码片段。
另请阅读此内容
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = "my@email.com"
msg['To'] = "your@email.com"
text = "Hello World!"
html = """\
<html>
<head></head>
<body>
<p style="color: red;">Hello World!</p>
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1) # text must be the first one
msg.attach(part2) # html must be the last one
s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()
Run Code Online (Sandbox Code Playgroud)