Skip to content

Instantly share code, notes, and snippets.

@jonathanhculver
Created December 14, 2012 02:02
Show Gist options
  • Save jonathanhculver/4281908 to your computer and use it in GitHub Desktop.
Save jonathanhculver/4281908 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)
@emrahgultekin2007
Copy link

Hello Budy
I want to check email subject and IF subject is "Database Moved" then script has to send send to Arduino over serial.
How can I do that?
What is correct code?
Can you help me please

@csrgxtu
Copy link

csrgxtu commented Mar 31, 2017

hi, i found their is a idle method that can notify you with callback when new mail comes, but when i try to use it with imaplib and imaplib2, it doesn't work

@pktippa
Copy link

pktippa commented Jan 9, 2018

@csrgxtu could you please share the details about writing the callback way you tried

@amiedhillon
Copy link

Hi, Could you please help me to write the python code to receive a call when an email received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment