SHARE
TWEET

Untitled

a guest Apr 29th, 2017 529 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import imaplib
  2.  
  3. import sys
  4.  
  5. import os
  6.  
  7. import time
  8.  
  9. from os import path
  10.  
  11.  
  12.  
  13. ''' Versie 1.0 dd 31-12-2014
  14.  
  15.         Versie 1.0.1 dd 03-01-2015 Loop verwijderd zodat programma periodiek via cron gestart kan worden
  16.  
  17.                    
  18.  
  19.         Versie 1.1 dd 08-01-2015 Send Email message to dutch pocsag group.
  20.     Versie 1.2 dd 20-4-2017 Send page to dapnet
  21.    
  22.  
  23. Programma voor detecteren van nieuwe Email
  24.  
  25. ingeval van nieuwe mail wordt er een bericht verstuurd naar de een ARDUINO
  26.  
  27. '''
  28.  
  29.  
  30.  
  31. old_lastmail = 0                         # Aantal UNSEEN messages van vorige controle
  32.  
  33. mailuser = 'user@gmail.com'          # user
  34.  
  35. mailpassword = 'pass'          # pass
  36.  
  37. uidfile='/home/pi/email_uid.dat'     # File to keep uid from last message
  38.  
  39. sendto="dapnet"                          # leave as is...
  40.  
  41.  
  42.  
  43. def save_lastuid(lastuid):
  44.  
  45.     lastuidfile = open (uidfile, 'w')
  46.  
  47.     lastuidfile.write(lastuid)
  48.  
  49.     lastuidfile.close()
  50.  
  51.  
  52.  
  53. def get_lastuid():
  54.  
  55.     if not (path.isfile(uidfile)):
  56.  
  57.       lastuid=0
  58.  
  59.     else:
  60.  
  61.       lastuidfile = open (uidfile, 'r')
  62.  
  63.       lastuid=lastuidfile.readline()
  64.  
  65.       lastuidfile.close
  66.  
  67.     return lastuid
  68.  
  69.  
  70. mail = imaplib.IMAP4_SSL('imap.gmail.com')
  71.  
  72. mail.login(mailuser, mailpassword)
  73.  
  74.  
  75.  
  76. mail.select("INBOX") # connect to inbox.
  77.  
  78.  
  79.  
  80. result, data = mail.uid('search', None, '(UNSEEN)')     # Get UNSEEN messages
  81.  
  82.  
  83.  
  84. ids = data[0] # data is a list.
  85.  
  86. id_list = ids.split() # ids is a space separated string
  87.  
  88. total_mails=len(id_list)
  89.  
  90.  
  91.  
  92. # Check last UID element in list              
  93.  
  94.  
  95. if ids:                            # Check UID from lastmail message
  96.  
  97.   lastmail = id_list[-1]           # If UID greater then UID from previous run then new mail
  98.  
  99.   old_lastmail=get_lastuid()
  100.  
  101.   if lastmail > old_lastmail:  
  102.  
  103.     print ("You have new mail and you have " + str(total_mails) + " unseen messages")
  104.  
  105.     old_lastmail = lastmail
  106.  
  107.     if sendto == "dapnet" or sendto == "both":
  108.  
  109.       os.system("sh nieuwe_mail.sh")      
  110.  
  111.       print ("Sending page to dapnet...")
  112.  
  113.     save_lastuid(lastmail) # Save last UID in datafile
  114.    
  115.     ...
  116.     make nieuwe_mail.sh with following content:
  117.     curl -H "Content-Type: application/json" -X POST -u USER:PASSWORD -d '{ "text": "FUNKRUFTEXT", "callSignNames": ["RUFZEICHEN"], "transmitterGroupNames": ["SENDERGRUPPENNAME"], "emergency": false }' URL/calls
  118.     ...
  119.  
  120.  
  121. # End code sending Email
  122.  
  123.    
  124.  
  125.   else:
  126.  
  127.     print ("No new mail and " + str(total_mails) + " unseen messages")
  128.  
  129. else:
  130.  
  131.   print ("No new mail and no unseen messages")
  132.  
  133.  
  134.  
  135. mail.logout()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top