Ver Mensaje Individual
  #4 (permalink)  
Antiguo 01/06/2010, 03:30
Avatar de Nekeniehl
Nekeniehl
 
Fecha de Ingreso: julio-2009
Ubicación: Berlin, Alemania / Granada, España
Mensajes: 132
Antigüedad: 14 años, 9 meses
Puntos: 6
Respuesta: [Aporte] Como recuperar mensajes de la bandeja de entrada.

Aqui teneis la función con algunas modificaciones:

Código Python:
Ver original
  1. def CatchAll ():
  2.     """Catch all msg from the email Catch-all
  3.    Return values from, to, text, subject"""
  4.     host = cfg.get('Connection','host')
  5.     user = cfg.get('Connection','user')
  6.     pwd = cfg.get('Connection','pwd')
  7.  
  8.     mails = imaplib.IMAP4(host)
  9.     mails.login(user,pwd)
  10.     tmsg ={}
  11.     total = mails.select('INBOX')
  12.     for num in range(int(total[1][0])):
  13.         msg = {}
  14. #        Headers from the message
  15.         typ, msg_data = mails.fetch(num+1, '(BODY.PEEK[HEADER])')
  16.         fro = msg_data[0][1].split("From: ")[1].split("\r\n")[0]
  17.         msg["from"] = fro
  18.         sub = msg_data[0][1].split("Subject: ")[1].split("\r\n")[0]
  19.         msg["subject"] = sub
  20.         to = msg_data[0][1].split ("To: ")[1].split("\r\n")[0]
  21.         msg["to"] = to
  22. #        Text from the message
  23.         typ, msg_data = mails.fetch(num+1, '(BODY.PEEK[TEXT])')
  24.         text = msg_data[0][1].split("\r\n")[0]
  25.         msg["text"] = text
  26.         tmsg[num] = msg
  27.  
  28.     mails.close()
  29.     mails.logout()
  30.     return tmsg
Reeditado para que se guarden los datos en un solo diccionario y se pueda acceder atraves de una key numerica
__________________
Antes de cambiar el mundo...dá tres vueltas por tu casa

Usa Google es tu amigo ;)

Última edición por Nekeniehl; 02/06/2010 a las 09:42