首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python libgmail在邮件中包含html部分

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

Stack Overflow用户
提问于 2009-02-22 12:41:46
回答 1查看 465关注 0票数 2

我有一个关于它的用法的问题:我需要发送一封html格式的邮件。我用以下内容准备我的信息

代码语言:javascript
复制
ga = libgmail.GmailAccount(USERNAME,PASSWORD)
msg = MIMEMultipart('alternative') 
msg.attach(part1)
msg.attach(part2)
...
ga.sendMessage(msg.as_string())

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

EN

回答 1

Stack Overflow用户

发布于 2009-02-22 13:40:49

如果您从sourceforge引用libgmail,则需要使用email module编写消息。

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

example in the doc包含类似需求的代码:

代码语言:javascript
复制
# 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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/574861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档