我想在 python 中使用 imap 从 gmail 帐户中检索带有正文的电子邮件
I want to retrieve emails from gmail account with the body using imap in python
这是我的代码,我正在尝试从 gmail 获取带有正文的电子邮件,但它以错误结尾。
" 邮件 = email.message_from_string(数据0)
AttributeError:'str' 对象没有属性 'message_from_string'”
任何帮助都适用!
the error message is being shown in the picture
#!/usr/bin/env python
import getpass
import imaplib
import email
from email.parser import HeaderParser
M = imaplib.IMAP4_SSL("imap.gmail.com", 993)
email = raw_input("Email address: ")
password = getpass.getpass()
M.login(email, password)
M.select()
resp, data = M.FETCH(1, '(RFC822)')
mail = email.message_from_string(data[0][1])
for part in mail.walk():
# multipart are just containers, so we skip them
if part.get_content_maintype() == 'multipart':
continue
# we are interested only in the simple text messages
if part.get_content_subtype() != 'plain':
continue
payload = part.get_payload()
print payload
您已经用用户输入的字符串覆盖了 "email" 模块。
import email
...
email = raw_input("Email address: ")
...
mail = email.message_from_string(data[0][1])
将行 email = raw_input("Email address: ")
更改为其他名称,例如 address = raw_input("Email address: ")
并根据需要调整您的代码。
这是我的代码,我正在尝试从 gmail 获取带有正文的电子邮件,但它以错误结尾。 " 邮件 = email.message_from_string(数据0) AttributeError:'str' 对象没有属性 'message_from_string'” 任何帮助都适用! the error message is being shown in the picture
#!/usr/bin/env python
import getpass
import imaplib
import email
from email.parser import HeaderParser
M = imaplib.IMAP4_SSL("imap.gmail.com", 993)
email = raw_input("Email address: ")
password = getpass.getpass()
M.login(email, password)
M.select()
resp, data = M.FETCH(1, '(RFC822)')
mail = email.message_from_string(data[0][1])
for part in mail.walk():
# multipart are just containers, so we skip them
if part.get_content_maintype() == 'multipart':
continue
# we are interested only in the simple text messages
if part.get_content_subtype() != 'plain':
continue
payload = part.get_payload()
print payload
您已经用用户输入的字符串覆盖了 "email" 模块。
import email
...
email = raw_input("Email address: ")
...
mail = email.message_from_string(data[0][1])
将行 email = raw_input("Email address: ")
更改为其他名称,例如 address = raw_input("Email address: ")
并根据需要调整您的代码。