4714

Years ago, I made an e-mail notifier with Raspberry pi.

Suppose, you have someone seriously ill at home, You can't go to the office. But you need to stay connected to your team. You have to check your emails. It may not be always possible to keep your phone with you or check that. But it's not an option to miss a new mail. In such a situation, it's really nice to have a system, that will alert you when a new mail arrives. From which, you can understand at a glance if you need to check your mail.
 Years ago, I made an e-mail notifier using a raspberry pi and two LEDs. The Raspberry pi was integrated with my gmail account. When there was a new e-mail, it glew a Red Led, when there was no new mail, it turned on a Green LED.
 
 Components:

Red LED x 1
Green LED x 1
Breadboard x 1
100 ohms resistor x 2
Jumper wires- Male to Female
Raspberry Pi 3B+ x 1

Connections:
Raspberry pi | Components
GPIO 18 |         Positive side of Green LED through a 100K resistor.
GPIO 15 |         Positive side of Red LED through a 100K resistor.
GND |               GND

Code:
At first, I installed pip
   
 sudo apt-get install python-pip 

  Then I installed a python package named   IMAPClient
   
 sudo pip install imapclient   

 
 Then I ran the following python script.
   
import RPi.GPIO as GPIO, imaplib, time
DEBUG = 1
USERNAME = "e-mail id"     # just the part before the @ sign, add yours here
PASSWORD = "password"     
NEWMAIL_OFFSET = 0
MAIL_CHECK_FREQ = 60      # check mail every 60 seconds
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) 
GREEN_LED = 18
RED_LED = 15
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)
M = imaplib.IMAP4_SSL('imap.gmail.com','993')
sessionValid = True

try:
    M.login(USERNAME, PASSWORD)
    print ("LOGIN SUCCESSFULL")

except Exception as e:
    
    print (e)
    
    print ("LOGIN FAILED!!! ")
    sessionValid= False

while sessionValid:

        M.select()

        newmails = len(M.search(None, 'UnSeen')[1][0].split())  # from http://stackoverflow.com/a/3984850 

        if newmails > NEWMAIL_OFFSET:
                GPIO.output(GREEN_LED, True)
                GPIO.output(RED_LED, False)
                print ("New mail")
        else:
                GPIO.output(GREEN_LED, False)
                GPIO.output(RED_LED, True)
                print ("No new mail")

        time.sleep(MAIL_CHECK_FREQ) 

 
 .