0

I'm unable to find ANY code that will function to retrieve Gmail.

import poplib
from email import parser

SERVER = "pop.gmail.com"
USER  = "user@gmail.com"
PASSWORD = "password"

pop_conn = poplib.POP3_SSL(SERVER)
pop_conn.user(USER)
pop_conn.pass_(PASSWORD)
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print(message['subject'])
    print(message['body'])

This produces only:

TypeError Traceback (most recent call last) <ipython-input-8-e557fa99ae8d> in () 12 messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] 13 # Concat message pieces: ---> 14 messages = ["\n".join(mssg[1]) for mssg in messages] 15 #Parse message intom an email object: 16 messages = [parser.Parser().parsestr(mssg) for mssg in messages]

<ipython-input-8-e557fa99ae8d> in (.0) 12 messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] 13 # Concat message pieces: ---> 14 messages = ["\n".join(mssg[1]) for mssg in messages] 15 #Parse message intom an email object: 16 messages = [parser.Parser().parsestr(mssg) for mssg in messages]

TypeError: sequence item 0: expected str instance, bytes found

I'VE SPENT DAYS trying to do a "simple" e-mail retrieve, and EVERY scrap of code I've found is totally non-functional.

Can anyone actually get Gmail, with subjects, etc?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
John Bailey
  • 137
  • 2
  • 11
  • 1
    I don't know if this is helpful, but I know that Gmail keeps its servers very secure. It usually refuses the connection if a less secure application tries to access an account. I don't know if this will work, here is the on/off switch for security: https://myaccount.google.com/lesssecureapps?pli=1 – Rohan May 07 '18 at 04:20
  • why not use the gmail api? – Linda Lawton - DaImTo May 07 '18 at 08:50
  • Thank you for the reply. I've confirmed that's not the issue. – John Bailey May 07 '18 at 20:12

1 Answers1

0

This code is incomplete but may help you.

source: https://www.code-learner.com/python-use-pop3-to-read-email-example/

import poplib
import smtplib, ssl

# input email address, password and pop3 server domain or ip address
email = 'yourgmail@gmail.com'
password = 'password'

# connect to pop3 server:
server = poplib.POP3_SSL('pop.gmail.com')
# open debug switch to print debug information between client and pop3 server.
server.set_debuglevel(1)
# get pop3 server welcome message.
pop3_server_welcome_msg = server.getwelcome().decode('utf-8')
# print out the pop3 server welcome message.
print(server.getwelcome().decode('utf-8'))

# user account authentication
server.user(email)
server.pass_(password)

# stat() function return email count and occupied disk size
print('Messages: %s. Size: %s' % server.stat())
# list() function return all email list
resp, mails, octets = server.list()
print(mails)

# retrieve the newest email index number
index = len(mails)
# server.retr function can get the contents of the email with index variable value index number.
resp, lines, octets = server.retr(index)

# lines stores each line of the original text of the message
# so that you can get the original text of the entire message use the join function and lines variable. 
msg_content = b'\r\n'.join(lines).decode('utf-8')
# now parse out the email object.

from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr

import poplib

# parse the email content to a message object.
msg = Parser().parsestr(msg_content)

# get email from, to, subject attribute value.
email_from = msg.get('From')
email_to = msg.get('To')
email_subject = msg.get('Subject')
print('From ' + email_from)
print('To ' + email_to)
print('Subject ' + email_subject)

# delete the email from pop3 server directly by email index.
# server.dele(index)
# close pop3 server connection.
server.quit()
Alejandro Garcia
  • 140
  • 2
  • 3
  • 15