# from flask import Flask from db.db import Db # lob stuff import lob import requests import time from datetime import datetime, timedelta # # app = Flask(__name__) # # # @app.route('/send/') # def send(): print ("Send Postcards") date = int(time.time()) print (date) datesend = datetime.today() datesend += timedelta(days=5) datesend = datesend.isoformat() print ('[MySql] Getting mail queue for this day:', datesend) # Create DB instance db = Db() mailqueue = ("SELECT id" " FROM mailing_queues" " WHERE processed IN (0) AND date < " + str(int(time.time())) ) queueitems = db.query(mailqueue) while queueitems: itemid = queueitems.pop()['id'] # Get mailing Queue nextitem = ("SELECT *" " FROM `mailing_queues`" " JOIN contacts on contact_id = contacts.id" " JOIN templates on template_id = templates.id" " JOIN assets on mailing_queues.template_id = assets.template_id" " WHERE mailing_queues.id =" + str(itemid) ) results = db.query(nextitem) headers = {'Accept-Encoding': 'identity'} # Load HTML Templates print (itemid) front = '' back = '' while results: print ('[MySQL] Current Mailing Queue Item') item = results.pop() # Get Template print ("Retrieving " + item['side'] + " Template From Postal Growth") if item['side'] == 'front': front = requests.get('https://postalgrowth.com/designs/design/' + str(item['template_id']) + '/front', headers=headers).content elif item['side'] == 'back': back = requests.get('https://postalgrowth.com/designs/design/' + str(item['template_id']) + '/back', headers=headers).content # pprint(item) # print (item['contact_id']) # Prepare Send to Address toaddress = { 'name': '%s %s' % (item['first'], item['last']), 'address_line1': item['address'].strip(), 'address_line2': ('%s %s' % (item['address_2'], item['address_3'])).strip(), 'address_city': item['city'], 'address_state': item['state'], 'address_zip': str(item['zip']) } meta = { 'queue_id': itemid, 'date': date } print ('\n\n') # connect to lob api #lob.api_key = 'test_aea742c978ebe26f23a2d0d5d8d7fcb81ea' lob.api_key = 'live_1138ef58de0b08f25f3e4d43918203face6' #mail_type = 'usps_standard' mail_type = 'usps_first_class' print ('[LOB] Creating Postcard') try: response = lob.Postcard.create( description='Test Postcard', metadata=meta, to_address=toaddress, from_address={ 'name': 'Empire Genomics', 'address_line1': '1000 Youngs Rd', 'address_line2': 'Suite 207', 'address_city': 'Williamsville', 'address_state': 'NY', 'address_zip': '14221' }, front=front, back=back, size='6x9', mail_type= mail_type # , # send_date=datesend #Developer Edition does not allow for Scheduled mail ) if response.id: print ('[LOB] Postcard Creation Successful') updatequery = ("UPDATE mailing_queues" " SET processed = '1'" " WHERE id=" + str(itemid)) db.query(updatequery) else: print ('[LOB] Postcard Creation Failed') updatequery = ("UPDATE mailing_queues" " SET processed = '-1'" " WHERE id=" + str(itemid)) db.query(updatequery) except Exception as e: print ('[LOB] Postcard Creation Failed') # print (toaddress) if hasattr(e, 'message'): print(e.message) else: print(e) updatequery = ("UPDATE mailing_queues" " SET processed = '-1'" " WHERE id=" + str(itemid)) db.query(updatequery) # break continue # getTemplate HTML # print ('[LOB] Listing Postcards') # print (lob.Postcard.list()) # # return "

Created Templates

" # # return template # # # if __name__ == "__main__": # app.run()