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

2024-06-17 11:14:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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


Tags: 方法信息用法html格式username邮件msg
1条回答
网友
1楼 · 发布于 2024-06-17 11:14:36

如果您从sourceforge引用libgmail,则需要用email module组合消息。在

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

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

# 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)

相关问题 更多 >