Posts: 4
Threads: 1
Joined: Aug 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()
Posts: 8,160
Threads: 160
Joined: Sep 2016
Aug-13-2018, 07:28 AM
(This post was last modified: Aug-13-2018, 07:28 AM by buran.)
https://docs.python.org/3/library/poplib...p3-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
Posts: 4
Threads: 1
Joined: Aug 2018
Thanks. But I'm not getting any errors. Just the screen is blank, the same way it happens in case of infinite loop.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Aug-13-2018, 08:37 AM
(This post was last modified: Aug-13-2018, 08:37 AM by buran.)
That is what i get
Error: Traceback (most recent call last):
File "*****************\foo.py", line 23, in <module>
readMail()
File "*****************\foo.py", line 15, in readMail
id, size = string.split(items[i])
NameError: global name 'items' is not defined
>>>
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.
Posts: 4
Threads: 1
Joined: Aug 2018
I tried the script from the link. This is what I get
Error: Traceback (most recent call last):
File "imap_email.py", line 3, in <module>
M = poplib.POP3('pop.gmail.com')
File "C:\Python37\lib\poplib.py", line 105, in __init__
self.welcome = self._getresp()
File "C:\Python37\lib\poplib.py", line 149, in _getresp
resp, o = self._getline()
File "C:\Python37\lib\poplib.py", line 133, in _getline
if not line: raise error_proto('-ERR EOF')
poplib.error_proto: -ERR EOF
Posts: 8,160
Threads: 160
Joined: Sep 2016
Aug-13-2018, 09:06 AM
(This post was last modified: Aug-13-2018, 09:06 AM by buran.)
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()
Return the “login name” of the user.
This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first one which is set to a non-empty string. If none are set, the login name from the password database is returned on systems which support the pwd module, otherwise, an exception is raised.
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/imapli...plib.IMAP4
Posts: 4
Threads: 1
Joined: Aug 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.
Output: [ None ] None
[ None ] None
[ None ] None
[ None ] None
[ None ] None
[ None ] None
[ None ] None
[ None ] None
Can anyone tell me what's wrong with the code?
|