gpt4 book ai didi

Python:使用 sendmail 以 html 格式发送邮件

转载 作者:行者123 更新时间:2023-11-28 23:06:25 25 4
gpt4 key购买 nike

谁能告诉我如何在 python 中使用 sendmail 发送 HTML 格式的邮件?

我想发送这个:

<pre>some code</pre>

Python版本为2.4.3,无法更新。

最佳答案

# assemble the mail content
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
message = MIMEMultipart('alternative')
message.add_header('Subject', 'subject goes here')
# add a few more add_header calls here for things like "To", "Cc", "From"
message.attach(MIMEText('some code')) # plain text alternative
message.attach(MIMEText('<html><pre>some code</pre></html>', # html content
'html'))

# pipe the mail to sendmail
sendmail = os.popen('sendmail recipient@example.org', 'w')
sendmail.write(message.as_string())
if sendmail.close() is not None:
print 'error: failed to send mail :-('

关于Python:使用 sendmail 以 html 格式发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5002824/

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