SHARE
TWEET

Untitled

a guest Sep 17th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import ezgmail
  2. import re
  3. import pickle
  4. import datetime
  5. email_regex = r'Buyer Email[\s\n]+(.+@.+) <'
  6. label_id = ''
  7. #name - shoppya
  8. thirty_time_delta = datetime.timedelta(minutes=30)
  9.  
  10.  
  11. def dump_pickle(variable, name):
  12.     with open(f'{name}.dat', 'wb') as fp:
  13.         pickle.dump(variable, fp)
  14.  
  15.  
  16. def load_pickle(name):
  17.     with open(f'{name}.dat', 'rb') as fp:
  18.         return pickle.load(fp)
  19.  
  20.  
  21. try:
  22.     scheduler = load_pickle('scheduler')
  23. except FileNotFoundError:
  24.     scheduler = {}
  25.  
  26.  
  27. try:
  28.     email_frequency_dict = load_pickle('email_frequency_dict')
  29. except FileNotFoundError:
  30.     email_frequency_dict = {}
  31.  
  32.  
  33. mail_content = [{'subject': 'Thanks for your first purchase',
  34.                  'body': 'THIS IS THE MESSAGE BODY'},
  35.                 {'subject': 'Thanks for your second purchase',
  36.                  'body': 'THIS IS THE MESSAGE BODY'},
  37.                 {'subject': 'Thanks for your third purchase',
  38.                  'body': 'THIS IS THE MESSAGE BODY'}]
  39.  
  40.  
  41.  
  42. for email, time in scheduler.items():
  43.     if datetime.datetime.now() > time:
  44.         #send email
  45.         frequency = email_frequency_dict[email]
  46.         if frequency > 3:
  47.             frequency = 3
  48.         ezgmail.send(email, mail_content[frequency-1]['subject'], mail_content[frequency-1]['body'])
  49.     else:
  50.         continue
  51.  
  52.  
  53.  
  54. for thread in ezgmail.search(f'NOT label:shoppya no-reply@shoppy.gg'):
  55.     for message in thread.messages:
  56.         if message.sender != 'no-reply@shoppy.gg':
  57.             continue
  58.         buyer_email = re.search(email_regex, message.body)
  59.         if buyer_email:
  60.             print(buyer_email.group(1))
  61.             if buyer_email in email_frequency_dict:
  62.                 email_frequency_dict[buyer_email] += 1
  63.             else:
  64.                 email_frequency_dict[buyer_email] = 1
  65.  
  66.             #add to scheduler
  67.             scheduler[buyer_email] = datetime.datetime.now() + thirty_time_delta
  68.  
  69.         else:
  70.             continue
  71.  
  72.             # ezgmail.addLabel(thread, label_id)
  73.  
  74. dump_pickle(email_frequency_dict, 'email_frequency_dict')
  75. dump_pickle(scheduler, 'scheduler')
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