Things used in this project

Code

Python ScriptPython
This is the code to recieve mail and trigger a function.
#!/usr/bin/python
#code adapted from http://www.nyccnc.com/uploads/1/3/0/4/13049512/judd_weebly.txt


#generic imports
import time
import os
import RPi.GPIO as GPIO
from time import sleep, strftime
from subprocess import *
from subprocess import call

#imports for email
import imaplib
import smtplib
import email
from email.mime.text import MIMEText
from email.parser import HeaderParser
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.Utils import COMMASPACE, formatdate
from email import Encoders

USERNAME = ""     #your gmail
PASSWORD = "" 			#your gmail password	qa

##########################################
#FUNCTION TO SEARCH TO BUILD LIST OF UNREAD EMAILS	
##########################################

def check_email():
    status, email_ids = imap_server.search(None, '(UNSEEN)')    #searches inbox for unseen aka unread emails ::The SEARCH command searches the mailbox for messages that match the given searching criteria.  Searching criteria consist of one or more search keys. The untagged SEARCH response from the server contains a listing of message sequence numbers corresponding to those messages that match the searching criteria. Status is result of the search command: OK - search completed, NO - search error: can't search that charset or criteria, BAD - command unknown or arguments invalid. Criteria we are using here is looking for unread emails
    if email_ids == ['']:
        mail_list = []
    else:
        mail_list = get_subjects(email_ids)      #FYI when calling the get_subjects function, the email is marked as 'read'

    imap_server.close()
    return mail_list
	
##########################################
#FUNCTION TO SCRAPE EMAIL SUBJECTS	
##########################################

def get_subjects(email_ids):
    subjects_list = []          				   #creates subjects_list list 
    for e_id in email_ids[0].split():   		   #Loops IDs of a new emails created from email_ids = imap_server.search(None, '(UNSEEN)')
    	resp, data = imap_server.fetch(e_id, '(RFC822)')   #FETCH command retrieves data associated with a message in the mailbox.  The data items to be fetched can be either a single atom or a parenthesized list. Returned data are tuples of message part envelope and data.
    	perf = HeaderParser().parsestr(data[0][1])	   #parsing the headers of message
    	subjects_list.append(perf['Subject'])		   #Looks through the data parsed in "perf", extracts the "Subject" field 
    return subjects_list

##########################################
#USER CODE
##########################################


##########################################
#MAIN LOOP
##########################################

while True:
	time.sleep(1)
        imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
        imap_server.login(USERNAME, PASSWORD)
        imap_server.select('INBOX')
        mail_list = check_email()
        if mail_list :
            user_code(mail_list)
    sleep(1)



    

Credits

Big engram isns8txm7l
Elvis Wolcott

Self-taught app developer and hardware developer, currently working on making the Random Rumble Generator and other voice based solutions

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Test Your Internet Speed Using a Raspberry Pi + Ubidots
Easy
  • 218
  • 4

Protip

Use your Raspberry Pi to measure any internet connection speed with Ubidots.

IFTTT - Enabled Sensors with Particle Photon
Easy
  • 627
  • 11

Full instructions

Connect any sensor to Particle Photon and use it to trigger an IFTTT action with just a few lines of code.

Chicken Coop Livestream
Easy
  • 2,161
  • 7

Full instructions

We have two chickens, and their coop is located at the back of the garden. I decided to build a Pi-based streaming device to check on them.

Raspberry Pi Shutdown / Restart Button
Easy
  • 5,087
  • 20

Full instructions

Building an installation project? Shut down or reboot your project safely, without a keyboard or SSH!

3d printed case for a RaspPi 3, an Arduino and a 5" display
Easy
  • 275
  • 4

Full instructions

A simple and compact frame to mount a 5" monitor, a Raspberry Pi, an Arduino, and a battery, which is extensible for other sensors.

DIY Raspberry Pi Indoor Outdoor Webcam
Easy
  • 1,320
  • 6

Full instructions

Ever want to monitor your home but don't want to buy a $100 1080p webcam? Well I did, and you can too!

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog