Skip to content

Instantly share code, notes, and snippets.

@Micah81
Forked from vwillcox/gmail.py
Created January 27, 2017 22:28
Show Gist options
  • Save Micah81/02aa726ac87a4c76b9c29b95df520f62 to your computer and use it in GitHub Desktop.
Save Micah81/02aa726ac87a4c76b9c29b95df520f62 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment