Aqui teneis la función con algunas modificaciones:
Código Python:
Ver originaldef CatchAll ():
"""Catch all msg from the email Catch-all
Return values from, to, text, subject"""
host = cfg.get('Connection','host')
user = cfg.get('Connection','user')
pwd = cfg.get('Connection','pwd')
mails = imaplib.IMAP4(host)
mails.login(user,pwd)
tmsg ={}
total = mails.select('INBOX')
for num in range(int(total[1][0])):
msg = {}
# Headers from the message
typ, msg_data = mails.fetch(num+1, '(BODY.PEEK[HEADER])')
fro = msg_data[0][1].split("From: ")[1].split("\r\n")[0]
msg["from"] = fro
sub = msg_data[0][1].split("Subject: ")[1].split("\r\n")[0]
msg["subject"] = sub
to = msg_data[0][1].split ("To: ")[1].split("\r\n")[0]
msg["to"] = to
# Text from the message
typ, msg_data = mails.fetch(num+1, '(BODY.PEEK[TEXT])')
text = msg_data[0][1].split("\r\n")[0]
msg["text"] = text
tmsg[num] = msg
mails.close()
mails.logout()
return tmsg
Reeditado para que se guarden los datos en un solo diccionario y se pueda acceder atraves de una key numerica