2

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

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

这种方式行不通,好像不能msg用 sendMessage 方法发送。什么是正确的方法?: D

4

1 回答 1

1

如果您从 sourceforge 引用libgmail,则需要使用电子邮件模块撰写您的消息。

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

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

# 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)
于 2009-02-22T13:40:48.773 回答