Skip to content

Instantly share code, notes, and snippets.

@jackgu1988
Forked from jonathanhculver/checkmail.py
Created March 15, 2018 15:19
Show Gist options
  • Save jackgu1988/1b8c7d4f65c1138423e59fb6aac9a895 to your computer and use it in GitHub Desktop.
Save jackgu1988/1b8c7d4f65c1138423e59fb6aac9a895 to your computer and use it in GitHub Desktop.
Python script to check gmail for new email every minute and send to Arduino over serial.
import imaplib, serial, struct, time
class Mail():
def __init__(self):
self.user= 'USER'
self.password= 'PASS'
self.ser = serial.Serial('/dev/tty.usbmodem621', 9600)
self.M = imaplib.IMAP4_SSL('imap.gmail.com', '993')
self.M.login(self.user, self.password)
def checkMail(self):
self.M.select()
self.unRead = self.M.search(None, 'UnSeen')
return len(self.unRead[1][0].split())
def sendData(self):
self.numMessages= self.checkMail()
#turn the string into packed binary data to send int
self.ser.write(struct.pack('B', self.numMessages))
email = Mail()
# check for new mail every minute
while 1:
print 'Sending'
email.sendData()
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment