我对它的用法有疑问:我需要发送一个html格式的邮件。我用
准备我的信息ga = libgmail.GmailAccount(USERNAME,PASSWORD)
msg = MIMEMultipart('alternative')
msg.attach(part1)
msg.attach(part2)
...
ga.sendMessage(msg.as_string())
这种方式不起作用,似乎无法使用sendMessage方法发送msg
。
什么是正确的方法? :D
答案 0 :(得分:1)
如果您从sourceforge引用libgmail
,则需要使用email module撰写邮件。
以MIME document生成HTML邮件,并将其作为multipart MIME message的一部分包含在内。当您拥有完全构造的多部分MIME时,使用libgmail
方法将其作为字符串传递给.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)