Saturday, January 4, 2014

Arduino Yun: reading mails with Python: the simple way

All the previous stuff works, but was unnecessarily complicated. It can be done much simpler with Python. The following code will check all unseen messages and print the main 'body' of the mail.


#!/usr/bin/python
import sys
import imaplib
import private.pw
import email

USERNAME = private.pw.myMailUser
PASSWORD = private.pw.myMailPass
MAILTO  = private.pw.myMailTo

# first check if there is new mail
PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom"

n_email = int(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)["feed"]["fullcount"])
if n_email > 0:
    print "New mail!"
else:
    print "No new mail!"
    exit(0)

imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
imap_server.login(USERNAME, PASSWORD)
imap_server.select('INBOX')

result, data = imap_server.uid('search', None, "UNSEEN") # get unseen message UIDs
for my_uid in data[0].split():
   my_msg=''
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[HEADER.FIELDS (FROM)])')   # spaces are important here !!
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[])')   # use this if dont want to flag mail as seen
   result, data = imap_server.uid('fetch',my_uid, '(BODY[])')
   raw_email=data[0][1]
   email_msg = email.message_from_string(raw_email)
   maintype = email_msg.get_content_maintype()
   if maintype == 'multipart':
      for part in email_msg.get_payload():
         if part.get_content_maintype() == 'text':
            my_msg = part.get_payload()
   elif maintype == 'text':
     my_msg = email_msg.get_payload()
   print my_msg
imap_server.close()
imap_server.logout()



Note: feedparser should be installed on the Arduino Yun first. I did this as follows:
  • download it from https://code.google.com/p/feedparser/downloads/list (tar.gz) 
  • copy it to somewhere on the Yun SD card 
  • unzip and untar: 
    • gunzip feedp...
    • tar -xvf feedpa...
  • opkg update 
  • opkg install setuptools 
  • cd to the uncompressed folder 
  • python setup.py install
Note2: you need to have openssl stuff installed for this:
  • opkg update
  • opkg install python-openssl

2 comments:

  1. Hi Nico,

    Can We ask you a question about a bug that is turning us crazy?

    We are trying to connect to gmail in a way pretty similar to you use.
    The code is as follows:
    import imaplib
    import email
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login('user@example.com', 'password')

    But we only get: Arduino object has no attribute 'IMAP4_SSL'

    After some research we tried importing del ssl library:

    import imaplib
    import email
    import ssl
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login('user@example.com', 'password')
    mail.list()

    But in this occasion we got this:

    Traceback (most recent call last):
    File "mail-processor.py", line 3, in
    import ssl
    File "/usr/lib/python2.7/ssl.py", line 60, in
    import _ssl # if we can't import it, let the error propagate
    ImportError: No module named _ssl

    Well, any idea about this?

    Thanks in advance

    Francesc

    ReplyDelete
    Replies
    1. I am just blindly guessing, but did you install python-openssl:

      opkg update
      opkg install python-openssl

      Delete