Skip to content

Instantly share code, notes, and snippets.

@vwillcox
Forked from kimondo/email to LEDborg script for Raspberry PI
Last active May 20, 2019 15:28
Show Gist options
  • Save vwillcox/5090214 to your computer and use it in GitHub Desktop.
Save vwillcox/5090214 to your computer and use it in GitHub Desktop.
import imaplib
import email
#connect to gmail
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]','yourPassWordPlease')
mail.select('inbox')
mail.list()
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)')
typ, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
# get most recent email id
# Any Emails?
if id_list:
latest_email_id = int( id_list[-1] )
for i in range( latest_email_id, latest_email_id-1, -1):
typ, data = mail.fetch( i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
varFrom = varFrom.replace('<','')
varFrom = varFrom.replace('>','')
if len( varSubject ) >35: # Subject to large - turn the light off
varSubject = '000'
else: #No Emails so turn the light off
varSubject = '000'
#print the subject to test
print varSubject
#output the subject to the ledborg
LedBorg = open('/dev/ledborg', 'w')
LedBorg.write(varSubject)
del LedBorg
#Remove used emails from mailbox
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
mail.store(num, '+FLAGS', '\\Deleted')
mail.expunge()
mail.close()
mail.logout()
@vwillcox
Copy link
Author

vwillcox commented Mar 5, 2013

I have modified the script as follows

  1. Indented correctly
  2. Added If Else loop to check for empty mailbox and turn LED off if no emails
  3. Added Section to empty mailbox after using them

Possible future ideas - if more then one email - I may add a section in to run a pulse script between the two colours.
Also a way for me to keep the LED on if I have not seen it (so a flag somewhere).

@kimondo
Copy link

kimondo commented Mar 5, 2013

Thank you! - I'm going to add a link to this. I wondered about adding the option to make the LEDborg blink with the addition of a ! in the email subject - or the email being flagged as urgent.

@vwillcox
Copy link
Author

vwillcox commented Mar 5, 2013

Hi, thanks that would fine! I wont get more time to play tonight, but I do have some scripting that I will interject into this to get a multi-coloured fade going.

@comatech
Copy link

It's working! But now Google refuse connection from unsafe, as it's think, software and i don't know how to use this script now =(

@thermrpi9
Copy link

Hi, i've tried the program but it gives me a imaplib error : command SELECT illegal in state NONAUTH, only allowed in states AUTH, SELECTED. I'm not good at python i really don't know what to do

@vwillcox
Copy link
Author

Hi, i've tried the program but it gives me a imaplib error : command SELECT illegal in state NONAUTH, only allowed in states AUTH, SELECTED. I'm not good at python i really don't know what to do

Hi there, it's possible the library has changed a lot since this was written about 6 years ago.

I've also not used it for more than 5 years. However, I will try it when I get a chance and see if I can make some changes.

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