Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import imaplib
- import sys
- import os
- import time
- from os import path
- ''' Versie 1.0 dd 31-12-2014
- Versie 1.0.1 dd 03-01-2015 Loop verwijderd zodat programma periodiek via cron gestart kan worden
- Versie 1.1 dd 08-01-2015 Send Email message to dutch pocsag group.
- Versie 1.2 dd 20-4-2017 Send page to dapnet
- Programma voor detecteren van nieuwe Email
- ingeval van nieuwe mail wordt er een bericht verstuurd naar de een ARDUINO
- '''
- old_lastmail = 0 # Aantal UNSEEN messages van vorige controle
- mailuser = 'user@gmail.com' # user
- mailpassword = 'pass' # pass
- uidfile='/home/pi/email_uid.dat' # File to keep uid from last message
- sendto="dapnet" # leave as is...
- def save_lastuid(lastuid):
- lastuidfile = open (uidfile, 'w')
- lastuidfile.write(lastuid)
- lastuidfile.close()
- def get_lastuid():
- if not (path.isfile(uidfile)):
- lastuid=0
- else:
- lastuidfile = open (uidfile, 'r')
- lastuid=lastuidfile.readline()
- lastuidfile.close
- return lastuid
- mail = imaplib.IMAP4_SSL('imap.gmail.com')
- mail.login(mailuser, mailpassword)
- mail.select("INBOX") # connect to inbox.
- result, data = mail.uid('search', None, '(UNSEEN)') # Get UNSEEN messages
- ids = data[0] # data is a list.
- id_list = ids.split() # ids is a space separated string
- total_mails=len(id_list)
- # Check last UID element in list
- if ids: # Check UID from lastmail message
- lastmail = id_list[-1] # If UID greater then UID from previous run then new mail
- old_lastmail=get_lastuid()
- if lastmail > old_lastmail:
- print ("You have new mail and you have " + str(total_mails) + " unseen messages")
- old_lastmail = lastmail
- if sendto == "dapnet" or sendto == "both":
- os.system("sh nieuwe_mail.sh")
- print ("Sending page to dapnet...")
- save_lastuid(lastmail) # Save last UID in datafile
- ...
- make nieuwe_mail.sh with following content:
- curl -H "Content-Type: application/json" -X POST -u USER:PASSWORD -d '{ "text": "FUNKRUFTEXT", "callSignNames": ["RUFZEICHEN"], "transmitterGroupNames": ["SENDERGRUPPENNAME"], "emergency": false }' URL/calls
- ...
- # End code sending Email
- else:
- print ("No new mail and " + str(total_mails) + " unseen messages")
- else:
- print ("No new mail and no unseen messages")
- mail.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement