Mutt and other tools to automate your Gmail box

First enable googles 'less secure apps' here ( applies to gmail account holders using 2 stage authentication, in fact i recommend this to secure your emailing system although it isn't necessary for this following script to work ) to delete them ( put the in the Bin folder ) go here

then put the 16 digit password note this isnt your normal password for your gmail account but one generated for you by that page on googles site !

into your mail program configuration section (.muttrc) ( I use Mutt )
 this is the resulting email from google below

mutt" password successfully created

Hi Stex,
You have successfully created an app password for your Google Account named mutt on Tuesday, April 25, 2017 5:15 PM (GMT).

This app password will allow you to access your Google account from a device or application that can only be configured with a username and password, rather than a username, password and a verification code.

Don't recognize this activity?
helpful fac here

um but back to the script now you need to download/install a mail retrieval programs i used mutt as it seems
to be the one with the most demos on the net :) ( remember when scripting to go the easy path first then to the more complex scripts once you have the basic ideas working ) now install mutt and MTA ( mail transport agent )

sudo apt-get install mutt
sudo apt-get install default-mta
#now make a python script and call it or whatever
# this script looks for the word Microsoft on the subject line then beeps the linux speeker and prints out 'found'
# cut script after this line .....................
#!/usr/bin/env python

import sys
import imaplib
import getpass
import email
import email.header
import datetime
import os

# Note: This function definition needs to be placed
#       before the previous block of code that calls it.
def process_mailbox(M):
  rv, data =, "ALL")
  if rv != 'OK':
      print "No messages found!"

  for num in data[0].split():
      rv, data = M.fetch(num, '(RFC822)')
      if rv != 'OK':
          print "ERROR getting message", num

      msg = email.message_from_string(data[0][1])
      print 'Message %s: %s' % (num, msg['Subject'])
      print msg['Subject']
  #if msg['Subject'] == 'Welcome aboard!':
      words2 = msg['Subject'].split(' ')
      if words2[0] == 'Microsoft':
      print 'Found Microsoft on the subject line .......... playing system sound for 3 secs.'
        os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % ( 3,2000))
#os.system('DISPLAY=:0.0 zenity --warning--text="Warning email alert recieved." ') for a nice dialog box style warning
       #     print 
     #elem1 = [x for x in msg['Subject']]
#print item

      print 'Raw Date:', msg['Date']
      date_tuple = email.utils.parsedate_tz(msg['Date'])
      if date_tuple:
          local_date = datetime.datetime.fromtimestamp(
          print "Local Date:", \
              local_date.strftime("%a, %d %b %Y %H:%M:%S")

M = imaplib.IMAP4_SSL('')
    M.login('', '16digitPa$$w0rd') #abcdefghijklmnop
except imaplib.IMAP4.error:
    print "LOGIN FAILED!!! "
    # ... exit or deal with failure...rv, mailboxes = M.list()
rv = 'OK'
print "Mailboxes:"
#print mailboxes
#select a box
#rv, data ="noreply@JobSearch")
rv, data ="Inbox")
if rv == 'OK':
    print "Processing mailbox...\n"
    process_mailbox(M) # ... do something with emails, see below ...

Automate running this script using Anarcron