Unable to successfully dump emails retrieved from gmail to mbox file using python -


i have written below 2 code snippets. second 1 work's expected not first one.i unable find out why output list of print(msg_obj.keys()) @ line #22 in first snippet doesn't contain 'subject','from' keys while msg_obj contains header fields 'subject','from'. when dump emails mbox file using script , later open file using utility(mboxview.exe windows) ,utility doesn't recognise email dumped.please me out of this. suggestions welcomed.

import imaplib,email,mailbox  m=imaplib.imap4_ssl('imap.gmail.com',993) status,data=m.login('someone@gmail.com', 'password') m.select()  #create new mbox file if doesn't exist mbox_file=mailbox.mbox('gmail_mails.mbox') mbox_file.lock()  #get mails number    status,data=m.search(none, 'all')  try:     mail_no in data[0].split():          status,msg=m.fetch(mail_no,'(rfc822)')          msg_obj=email.message_from_string(str(msg[0][1]))          #print debugging purpose         print(msg_obj.keys())         print(msg_obj["subject"])          mbox_msg_obj=mailbox.mboxmessage(msg_obj)          mbox_file.add(mbox_msg_obj)          mbox_file.flush()  finally:     mbox_file.unlock()     mbox_file.close()  m.close() m.logout() 

also found in case of below code works:

from email.parser import parser  str="""received: (qmail 8580 invoked network); 15 jun 2010 21:43:22      -0400\r\nreceived: mail-fx0-f44.google.com (209.85.161.44) ip-73-187-35-131.ip.secureserver.net smtp; 15 jun 2010 21:43:22 -0400\r\nreceived: fxm19 smtp id 19so170709fxm.3 <username@domain.com>; tue, 15 jun 2010 18:47:33 -0700 (pdt)\r\nmime-version: 1.0\r\nreceived: 10.103.84.1 smtp id m1mr2774225mul.26.1276652853684; tue, 15 jun 2010 18:47:33 -0700 (pdt)\r\nreceived: 10.123.143.4 http; tue, 15 jun 2010 18:47:33 -0700 (pdt)\r\ndate: tue, 15 jun 2010 20:47:33 -0500\r\nmessage-id: <aanlktikfsijj3kyw1hjwcaqqlgxnixe2ymzrj39i0tdb@mail.gmail.com>\r\nsubject: test 12\r\nfrom: full name <username@sender.com>\r\nto: username@domain.com\r\ncontent-type: text/plain; charset=iso-8859-1 one\ntwo\nthree"""  msg=parser().parsestr(str)  print (msg['subject']) print (msg['from']) print (msg['to']) 

here output is

test 12 full name <username@sender.com> username@domain.com 

problem solved using email.parser.bytesparser().parsebytes() instead of email.message_from_string().but not getting why?

from example code, i'm trying following:

import email  estr = """received: (qmail 8580 invoked network); 15 jun 2010 21:43:22      -0400\r\nreceived: mail-fx0-f44.google.com (209.85.161.44) ip-73-187-35-131.ip.secureserver.net smtp; 15 jun 2010 21:43:22 -0400\r\nreceived: fxm19 smtp id 19so170709fxm.3 <username@domain.com>; tue, 15 jun 2010 18:47:33 -0700 (pdt)\r\nmime-version: 1.0\r\nreceived: 10.103.84.1 smtp id m1mr2774225mul.26.1276652853684; tue, 15 jun 2010 18:47:33 -0700 (pdt)\r\nreceived: 10.123.143.4 http; tue, 15 jun 2010 18:47:33 -0700 (pdt)\r\ndate: tue, 15 jun 2010 20:47:33 -0500\r\nmessage-id: <aanlktikfsijj3kyw1hjwcaqqlgxnixe2ymzrj39i0tdb@mail.gmail.com>\r\nsubject: test 12\r\nfrom: full name <username@sender.com>\r\nto: username@domain.com\r\ncontent-type: text/plain; charset=iso-8859-1 one\ntwo\nthree"""  msg = email.message_from_string(estr)  print (msg['subject']) print (msg['from']) print (msg['to']) 

that prints results fine.

test 12 full name <username@sender.com> username@domain.com 

so first code snippet, function input str(msg[0][1]) must contain unparsable contents. you'll have have closer @ contents of str(msg[0][1]) failing parse.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -