Read Gmail using Python

The poplib module can be used to download mails from an email server such as gmail. The protocol used between your computer and the email server is called POP (Post Office Protocol). This protocol can not send mail.

First enable POP support in gmail. .

Connect with gmail
We can conect with gmail using:

import poplib
import string, random
import StringIO, rfc822
import logging

SERVER = "pop.gmail.com"
USER  = "XXXXXX"
PASSWORD = "XXXXXX"

# connect to server
logging.debug('connecting to ' + SERVER)
server = poplib.POP3_SSL(SERVER)
#server = poplib.POP3(SERVER)

# log in
logging.debug('log in')
server.user(USER)
server.pass_(PASSWORD)

Read an email
Read gmail messages using this code:

import poplib
import string, random
import StringIO, rfc822
import logging

SERVER = "pop.gmail.com"
USER  = "XXXXXXXXXXX"
PASSWORD = "XXXXXXXXXXX"

# connect to server
logging.debug('connecting to ' + SERVER)
server = poplib.POP3_SSL(SERVER)
#server = poplib.POP3(SERVER)

# login
logging.debug('logging in')
server.user(USER)
server.pass_(PASSWORD)

# list items on server
logging.debug('listing emails')
resp, items, octets = server.list()

# download the first message in the list
id, size = string.split(items[0])
resp, text, octets = server.retr(id)

# convert list to Message object
text = string.join(text, "\n")
file = StringIO.StringIO(text)
message = rfc822.Message(file)

# output message
print(message['From']),
print(message['Subject']),
print(message['Date']),
#print(message.fp.read())   

We login with the first block. We get all items on the server using server.list(). We go on downloading the first message in the list and finally output it. If you want to output the message data simply uncomment the line message.fp.read()

Previous Post
Next Post

6 Replies to “Read Gmail using Python”

  1. Traceback (most recent call last):
      File "C:/apps/Python32/myprogs/gmail.py", line 18, in 
        server.pass_(PASSWORD)
      File "C:\apps\Python32\lib\poplib.py", line 192, in pass_
        return self._shortcmd('PASS %s' % pswd)
      File "C:\apps\Python32\lib\poplib.py", line 155, in _shortcmd
        return self._getresp()
      File "C:\apps\Python32\lib\poplib.py", line 131, in _getresp
        raise error_proto(resp)
    poplib.error_proto: b'-ERR [AUTH] Web login required: https://support.google.com/mail/answer/78754'
    
  2. #POP enabled in gmail account…….
    #No known temporary system problem at gmail. All fine in chrome browser……….
    #Running ‘connect with gmail’ gives the following ……..
    #Login ID and pw are correct………
    #Any ideas would be appreciated. ……..

    Last login: Sun Aug  7 13:24:21 on ttys002
    Cascade:~myidhere$ /var/folders/bn/dw1b7qk93vx09r9sd4_xjck80000gn/T/geany_run_script_11AXLY.sh ; exit;
    Traceback (most recent call last):
      File "gmail.py", line 18, in 
        server.pass_(PASSWORD)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/poplib.py", line 197, in pass_
        return self._shortcmd('PASS %s' % pswd)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/poplib.py", line 160, in _shortcmd
        return self._getresp()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/poplib.py", line 136, in _getresp
        raise error_proto(resp)
    poplib.error_proto: -ERR [SYS/TEMP] Temporary system problem.  Please try again later. 45mb111276382otn
    
    
    ------------------
    (program exited with code: 1)
    Press return to continue
    

Leave a Reply

Your email address will not be published. Required fields are marked *