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

Setting Up Your Pi Cap on the Raspberry Pi 1, 2 or 3
Easy
  • 74
  • 2

Protip

Follow this tutorial to set up your Pi Cap with a Raspberry Pi 1 A+/B+, Raspberry Pi 2 or Raspberry Pi 3.

Setting Up Your Pi Cap on the Raspberry Pi Zero
Easy
  • 82
  • 2

Protip

Follow this tutorial to set up your Pi Cap with a Raspberry Pi Zero.

Secure Simple Remote Access for Camera Viewing
Easy
  • 2,148
  • 8

Full instructions

Turn your RPi into a full featured camera with remote access, end2end encryption, and easy user enrollment.

MyHumidity controlled by BME280 and Photon
Easy
  • 37
  • 1

Full instructions

Checks if humidity threshold is reached and informs the user by using Blynk/IFTTT-services with softAP integration.

PiCorder
Easy
  • 3,641
  • 14

Full instructions

A simple Pi-based camcorder using the very nice HyperPixel touchscreen from Pimoroni.

RPi Serial Console Automation using Arduino
Easy
  • 235
  • 3

Full instructions

A follow-up article to "One Button Restart of a Stalled Raspberry Pi": 3 digital pins provide 8 separate functions that can be called.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login