使用[Python 3.6]提取电子邮件正文

时间:2018-11-09 06:10:23

标签: email python-3.6 poplib

我有一个收件箱,我需要编写一个代码,该代码在每次收到新邮件时都会触发,并提取该邮件的正文。

到目前为止,我已经有了以下代码:

import poplib, email
from email import parser

pop_conn = poplib.POP3_SSL('pop.gmail.com')
email_user = 'email@gmail.com'
email_pass = 'password'

pop_conn.user(email_user)
pop_conn.pass_(email_pass)

#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, 
len(pop_conn.list()[1]) + 1)]

#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print(message.get_payload(None, True))

pop_conn.quit()

我得到的错误是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-2a08d6a8ccf9> in <module>()
  2 #Parse message intom an email object:
  3 #messages = [email.message_from_bytes(mssg) for mssg in messages]
----> 4 messages = [parser.Parser().parsestr(mssg) for mssg in messages]
  5 for message in messages:
  6 #    print(message['body'])

<ipython-input-6-2a08d6a8ccf9> in <listcomp>(.0)
  2 #Parse message intom an email object:
  3 #messages = [email.message_from_bytes(mssg) for mssg in messages]
----> 4 messages = [parser.Parser().parsestr(mssg) for mssg in messages]
  5 for message in messages:
  6 #    print(message['body'])

/Applications/anaconda3/lib/python3.6/email/parser.py in parsestr(self, text, headersonly)
 66         the file.
 67         """
---> 68         return self.parse(StringIO(text), headersonly=headersonly)
 69 
 70 

TypeError: initial_value must be str or None, not tuple

TypeError: initial_value must be str or None, not tuple

我为什么要得到一个元组?如何从消息中提取正文[n]?

1 个答案:

答案 0 :(得分:0)

retr()的返回值是一个元组。元组中的第二个值是组成实际消息的行的列表。有关详细信息,请参见Python numpy.savetxt [numpy-doc]

# Get messages from server
popped = ['\n'.join(pop_conn.retr(i)[1])
    for i in range(1, len(pop_conn.list()[1]) + 1)]

# Parse message into an email object
messages = [parser.Parser().parsestr(mssg) for mssg in popped]

还请注意我们如何避免对不同类型的对象重复使用相同的变量名。