gpt4 book ai didi

python - 在 Python 中发送带有颜色格式的电子邮件

转载 作者:行者123 更新时间:2023-12-01 02:54:53 25 4
gpt4 key购买 nike

我有以下 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()

最佳答案

这是我用来发送 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()

关于python - 在 Python 中发送带有颜色格式的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44322806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com