#!/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