用Python发送带有颜色格式的电子邮件

2024-10-01 07:44:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的Python代码,用于从文件filename的内容向我的ID发送电子邮件。我正在尝试发送带有文本颜色格式的电子邮件。在

有什么意见请指教。在

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()

Tags: path代码文本comid内容os电子邮件
1条回答
网友
1楼 · 发布于 2024-10-01 07:44:48

这是我用来发送HTML电子邮件的片段。在

请同时阅读this

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()

相关问题 更多 >