Elvis Wolcott
Published © GPL3+

Send Unlimited Messages to your Raspbbery Pi for Free

Using IFTTT and Gmail you can send data to your Raspberry Pi for free and jumpstart your next IoT project.

EasyProtip1 hour10,406
Send Unlimited Messages to your Raspbbery Pi for Free

Things used in this project

Story

Read more

Code

Python Script

Python
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

Elvis Wolcott

Elvis Wolcott

31 projects • 79 followers
Self-taught app developer and hardware developer, interested in using VUI to improve accessibility

Comments