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

发布于 2024-10-17 08:37:32 字数 159 浏览 1 评论 0原文

有人可以告诉我如何在 python 中使用 sendmail 发送 HTML 格式的邮件吗?

我想发送这个:

<pre>some code</pre>

Python版本是2.4.3并且无法更新。

Can someone tell me how to send a mail in a HTML format with sendmail in python?

I would like to send this:

<pre>some code</pre>

Python version is 2.4.3 and can't be updated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒岛晴空 2024-10-24 08:37:32
# 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 :-(' 
# 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 :-(' 
一抹淡然 2024-10-24 08:37:32

您可以检查webpy微框架的代码以了解各种发送电子邮件的方法,包括sendmail:https://github.com/webpy/webpy/blob/master/web/utils.py#L1415

You may check the code of webpy micro-framework for various methods of sending email, including the sendmail: https://github.com/webpy/webpy/blob/master/web/utils.py#L1415

路还长,别太狂 2024-10-24 08:37:32

只需在消息中提供 HTML 代码,并以 text/html 形式提及 mime 版本和内容。 http://www.tutorialspoint.com/python/python_sending_email.htm

Simply give the HTML code in the message and also mention the mime version and content as text/html. http://www.tutorialspoint.com/python/python_sending_email.htm

违心° 2024-10-24 08:37:32

我找到了一种简单的方法来做到这一点:

当我运行脚本时,我将输出写入文件(mail.txt)中,然后最后,我只是调用:

os.popen('cat mail.txt | sendmail -t')

mail.txt content:

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>

Maybe 不是最好的方法这个,但对我来说效果很好......

I find a easy way to do this:

When I run my script, I write my output in a file (mail.txt), then at the end, I just call:

os.popen('cat mail.txt | sendmail -t')

mail.txt content:

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>

Maybe is not the best way to do this, but works fine for me...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文