有人能告诉我如何用一个HTML格式的邮件发送邮件在python中的sendmail吗?Python:用sendmail发送一封邮件
我想发送此:
<pre>some code</pre>
Python版本2.4.3是,不能更新。
有人能告诉我如何用一个HTML格式的邮件发送邮件在python中的sendmail吗?Python:用sendmail发送一封邮件
我想发送此:
<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 [email protected]', 'w')
sendmail.write(message.as_string())
if sendmail.close() is not None:
print 'error: failed to send mail :-('
只需在邮件中提供HTML代码,并将MIME版本和内容提及为text/html。 http://www.tutorialspoint.com/python/python_sending_email.htm
您可以检查用于发送电子邮件,包括sendmail的各种方法webpy微架构的代码:https://github.com/webpy/webpy/blob/master/web/utils.py#L1415
我找到一个简单的方法来做到这一点:
当我运行我的脚本,我写我在一个文件中(mail.txt)输出,然后在最后,我只要致电:
os.popen('cat mail.txt | sendmail -t')
mail.txt内容:
To: [email protected]
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="123"
Subject: My subject
This is a MIME-encapsulated message
--123
Content-Type: text/html
<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<pre>Some code</pre>
</body>
</html>
也许不做到这一点的最好办法,但工作正常,我...
如果SMTP是你可以检查一个可行的解决方案http://docs.python.org/library/email -examples.html – 2011-02-15 11:06:08
可能的重复 - http://stackoverflow.com/questions/882712/sending-html-email-in-python? – 2011-02-15 11:06:38