Advertisement
s4ros

Makowa Przypominajka

Nov 7th, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import MySQLdb as mdb # pip install MySQL-python
  4. import smtplib
  5. import logging
  6. from time import gmtime, strftime
  7.  
  8. loglevel=logging.DEBUG
  9. # loglevel=logging.INFO
  10.  
  11. logging.basicConfig(format='%(asctime)s [Przypominajka-%(levelname)s] %(message)s',filename='przypomiajka.log',level=loglevel)
  12.  
  13. class Mys:
  14.     """ Simple MySQL class to handle select2dict and execute queries """
  15.     def __init__(self, db_server, db_user, db_pass, db_name):
  16.         logging.debug("Connecting to database %s on server: %s as %s", db_name, db_server, db_user)
  17.         self.con = mdb.connect(db_server, db_user, db_pass, db_name)
  18.  
  19.     def execute(self,query):
  20.         try:
  21.             #~ con = mdb.connect('localhost','s4ros','.ZXCasd!@#.','s4ros')
  22.             logging.debug("Executing SQL query: '%s'",query)
  23.             cur = self.con.cursor()
  24.             cur.execute(query)
  25.             return True
  26.         except mdb.Error, e:
  27.             print "Error %d: %s" % (e.args[0],e.args[1])
  28.             logging.error("Can't execute SQL query!")
  29.             return False
  30.         self.con.close()
  31.  
  32.     def select2dict(self,query):
  33.         try:
  34.             #~ con = mdb.connect('localhost','s4ros','.ZXCasd!@#.','s4ros')
  35.             cur = self.con.cursor(mdb.cursors.DictCursor)
  36.             logging.debug("select2dict from SQL query: '%s'",query)
  37.             cur.execute(query)
  38.             rows = cur.fetchall()
  39.             cols = cur.description
  40.             return rows
  41.         except mdb.Error, e:
  42.             print "Error %d: %s" % (e.args[0],e.args[1])
  43.             logging.error("Can't select2dict SQL query!")
  44.             return False
  45.         self.con.close()
  46.  
  47. # test
  48. if __name__ == '__main__':
  49.     logging.info("Started new session of makova-reminder-cron job!")
  50.  
  51.     db_server = 'sql.second.vdl.pl'
  52.     db_user = 'kwg13_makova'
  53.     db_pass = '11roT2yUBV2wTByF4LSX'
  54.     db_name = db_user
  55.  
  56.     smtp_server = "makovapanienka.pl"
  57.     smtp_port = 587
  58.     smtp_user = "[email protected]"
  59.     smtp_passwd = "RGDjOJkt"
  60.  
  61.     logging.info("Creating connection to Database")
  62.     m = Mys(db_server, db_user, db_pass, db_name)
  63.     logging.info("Select all reminder data for ... good date.")
  64.     a = m.select2dict('SELECT * from reminder where event_date = DATE(CURDATE() + INTERVAL 3 DAY)')
  65.  
  66. ## pobralim wszystkie dane z przypominajki na dzien dzisiejszy..
  67. ## czas skomponowac maila
  68.     for event in a:
  69.         # making a connection to SMTP server
  70.         logging.info("Connecting to SMTP %s:%s server",smtp_server,smtp_port)
  71.         server = smtplib.SMTP(smtp_server, smtp_port)
  72.         logging.debug("Sending EHLO")
  73.         server.ehlo()
  74.         logging.debug("Starting TLS communication")
  75.         server.starttls()
  76.         logging.debug("Sending second EHLO")
  77.         server.ehlo()
  78.         logging.info("Logging into SMTP server as %s",smtp_user)
  79.         server.login(smtp_user, smtp_passwd)
  80.  
  81.         # gather all neccessary data from 'reminder' table
  82.         mail=dict()
  83.         mail['to'] = event['email']
  84.         logging.debug("E-MAIL TO: %s",mail['to'])
  85.         mail['subject'] = 'Przypominajka: %s' % event['event_name']
  86.         logging.debug("E-MAIL SUBJECT: %s",mail['subject'])
  87.         mail['content'] = event['content']
  88.         logging.debug("E-MAIL CONTENT: %s",mail['content'])
  89.  
  90.         # building email message with HEADERS and CONTENT
  91.         header='To:'+mail['to']+'\n'+'From:"Makowa Panienka"<'+smtp_user+'>\n'+'Subject:'+mail['subject']+'\n'
  92.         msg = header+'\n'+mail['content']+'\n\n'
  93.  
  94.         # sending email
  95.         logging.info("Sending an email to: %s",mail['to'])
  96.         server.sendmail(smtp_user, mail['to'], msg)
  97.  
  98.         # closing SMTP connection
  99.         logging.info("Closing connection to SMTP server.")
  100.         server.close()
  101.         # print 'done!'
  102.     logging.info("Shutting down makova-reminder-cron job.")
  103.     exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement