No output for the code to read emails - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: No output for the code to read emails (/thread-12178.html) |
No output for the code to read emails - avani9659 - Aug-13-2018 Hello, I've tried the code given below to read emails using poplib but I'm unable to get the output. import poplib import string, random import io, email def readMail(): server = 'pop.gmail.com' user = 'user_email' password = 'password' srv = poplib.POP3_SSL(server) srv.user(user) srv.pass_(password) for i in range(0,5): id, size = string.split(items[i]) resp, text, octets = srv.retr(id) text = string.join(text,"\n") file = io.StringIO(text) message = email.Message(file) for k,v in message.items(): print(k, "=", v) readMail() RE: No output for the code to read emails - buran - Aug-13-2018 https://docs.python.org/3/library/poplib.html#pop3-example Please, post full traceback you get in error tags. I think with your code, you should be getting NameError: name 'items' not defined on line 15.Also as a side note - don't use id as variable name, it's a build-in function and you overshadow it (i.e. it's not longer available to you). In python2 same apply to file
RE: No output for the code to read emails - avani9659 - Aug-13-2018 Thanks. But I'm not getting any errors. Just the screen is blank, the same way it happens in case of infinite loop. RE: No output for the code to read emails - buran - Aug-13-2018 That is what i get
Try the example script from the link in my last post (it works for me) and make sure it works for you too. Then try to amend it as per your needs. RE: No output for the code to read emails - avani9659 - Aug-13-2018 I tried the script from the link. This is what I get
RE: No output for the code to read emails - buran - Aug-13-2018 I didn't mention you should change getpass.getuser() to your user name (in the example it reads it from environmental variables)Quote:getpass.getuser() although I'm not sure this is the cause of the problem. Also your file name refers to imap and you use POP3. Make sure pop3 is enabled for this account. if you want to use imap, check https://docs.python.org/3/library/imaplib.html#imaplib.IMAP4 RE: No output for the code to read emails - avani9659 - Aug-14-2018 This is the code I tried to extract only Subject and senders name. import imaplib import email mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('user_name','password') mail.list() mail.select('INBOX') typ, data = mail.search(None,'ALL') for i in data[0].split(): typ, data = mail.fetch(i, '(RFC822)') for response_part in data: if isinstance(response_part, tuple): msg = email.message_from_string(str(response_part[1])) varSubject = msg['subject'] varFrom = msg['from'] print('[ %s ] %s' %(varFrom, varSubject))This is the output I'm getting. Can anyone tell me what's wrong with the code?
|