Python: 使用sendmail发送HTML邮件
有人能告诉我怎么用Python里的sendmail发送HTML格式的邮件吗?
我想发送这样的内容:
<pre>some code</pre>
我用的Python版本是2.4.3,不能更新。
4 个回答
0
简单来说,就是在信息中提供HTML代码,同时还要说明使用的mime版本和内容类型为text/html。你可以参考这个链接了解更多:http://www.tutorialspoint.com/python/python_sending_email.htm
1
你可以查看webpy这个小型框架的代码,里面有很多发送邮件的方法,包括sendmail这个方法。链接在这里:https://github.com/webpy/webpy/blob/master/web/utils.py#L1415
4
在编程中,有时候我们需要处理一些数据,比如从一个地方获取数据,然后在另一个地方使用它。这个过程就像是把水从一个水桶倒到另一个水桶里。
有些时候,我们会遇到一些问题,比如数据的格式不对,或者我们需要在处理数据的时候做一些额外的步骤。这就像是你在倒水的时候,发现水桶有个洞,水漏了,你需要想办法修好它,或者用另一个桶来接水。
在这些情况下,程序员会使用一些工具和方法来确保数据能够顺利地从一个地方转移到另一个地方。就像是使用漏斗来帮助你把水倒得更顺畅。
总之,处理数据就像是一个不断调整和优化的过程,确保每一步都能顺利进行,这样最终才能得到我们想要的结果。
# 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 :-('