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

使用python libgmail在邮件中包含html部分,python,html,gmail,mime,libgmail,Python,Html,Gmail,Mime,Libgmail,关于它的用法我有一个问题:我需要发送一封html格式的邮件。我用英语准备我的信息 ga = libgmail.GmailAccount(USERNAME,PASSWORD) msg = MIMEMultipart('alternative') msg.attach(part1) msg.attach(part2) ... ga.sendMessage(msg.as_string()) 这种方式不起作用,似乎无法使用sendMessage方法发送msg。 正确的方法是什么D如果您参考sourc

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

ga = libgmail.GmailAccount(USERNAME,PASSWORD)
msg = MIMEMultipart('alternative') 
msg.attach(part1)
msg.attach(part2)
...
ga.sendMessage(msg.as_string())
这种方式不起作用,似乎无法使用sendMessage方法发送
msg

正确的方法是什么D

如果您参考sourceforge提供的
libgmail
,则需要使用编写消息

将HTML消息生成为,并将其包含为的一部分。当您拥有一个完全构造的多部分MIME时,使用to
.as\u 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)

谢谢你的解释。现在我有了msg对象,但我不知道如何将它传递给libgmail方法来发送邮件,比如libgmail.GmailAccount(用户名、密码)ga.sendMessage(msg.as_string())不起作用:/尝试从字符串创建“GmailComposedMessage”,然后尝试发送此对象。