使用 python libgmail 在邮件中包含 html 部分

发布于 2024-07-14 04:26:39 字数 305 浏览 6 评论 0原文

我对其用法有疑问:我需要发送 html 格式的邮件。 我用这种方式准备消息,

ga = libgmail.GmailAccount(USERNAME,PASSWORD)
msg = MIMEMultipart('alternative') 
msg.attach(part1)
msg.attach(part2)
...
ga.sendMessage(msg.as_string())

这种方式不起作用,似乎无法使用 sendMessage 方法发送 msg 。 正确的方法是什么? :D

I've a question about its usage: i need to send an html formatted mail. I prepare my message with

ga = libgmail.GmailAccount(USERNAME,PASSWORD)
msg = MIMEMultipart('alternative') 
msg.attach(part1)
msg.attach(part2)
...
ga.sendMessage(msg.as_string())

This way doesn't works, it seems can't send msg with sendMessage method.
What is the right way? : D

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

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

发布评论

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

评论(1

墨离汐 2024-07-21 04:26:39

如果您从 sourceforge 引用 libgmail,则需要使用 电子邮件模块

将 HTML 消息生成为 MIME 文档,并将其包含为多部分 MIME 消息。 当您拥有完全构造的多部分 MIME 时,请使用 .as_string() 方法将其作为字符串传递给 libgmail 构造函数。

文档中的示例包含类似要求的代码:

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
...
# Record the MIME types of both parts - text/plain and text/html.
# ... text and html are strings with appropriate content.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

If you refer to libgmail from sourceforge, you need to compose your messages with the email module.

Generate the HTML message as a MIME document, and include it as a part of a multipart MIME message. When you have a fully constructed multipart MIME, pass it along as a string to the libgmail constructor, using to .as_string() method.

An example in the doc contains the code for a similar requirement:

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you
...
# Record the MIME types of both parts - text/plain and text/html.
# ... text and html are strings with appropriate content.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文