Python Forum
No output for the code to read emails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No output for the code to read emails
#1
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()
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks. But I'm not getting any errors. Just the screen is blank, the same way it happens in case of infinite loop.
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 357 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  I cannot able to see output of this code ted 1 751 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 1,854 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
  why I dont get any output from this code William369 2 1,123 Jun-23-2022, 09:18 PM
Last Post: William369
  Sending Emails on Autopilot Gyga_Hawk 3 1,682 Mar-15-2022, 08:20 AM
Last Post: Larz60+
  How can I organize my code according to output that I want ilknurg 1 1,164 Mar-11-2022, 09:24 AM
Last Post: perfringo
  Mark outlook emails as read using Python! shane88 2 6,539 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,395 Feb-13-2022, 07:13 PM
Last Post: menator01
  Trying out the parsing/reading of emails from my outlook cubangt 0 6,150 Jan-12-2022, 08:59 PM
Last Post: cubangt
  How to read this code? Jlyk 3 1,679 Aug-19-2021, 06:10 AM
Last Post: Jlyk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020