send_edm.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import smtplib
  2. import traceback
  3. import os
  4. from email.mime.text import MIMEText
  5. from email.mime.image import MIMEImage
  6. from email.mime.multipart import MIMEMultipart
  7. from email.message import EmailMessage
  8. import codecs
  9. import jinja2
  10. import codecs
  11. import os
  12. import dataset
  13. import time
  14. def gen_email(email,clientid,campaign):
  15. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
  16. fname=os.path.abspath(__file__)
  17. elmts=fname.split(os.path.sep)
  18. path2=os.path.sep.join(elmts[0:-1])
  19. keysdir=path2+os.path.sep
  20. print(keysdir)
  21. templateLoader = jinja2.FileSystemLoader(searchpath=keysdir)
  22. templateEnv = jinja2.Environment(loader=templateLoader)
  23. TEMPLATE_FILE = 'test.tmpl'
  24. template = templateEnv.get_template(TEMPLATE_FILE)
  25. #SELECT distinct ts_word FROM trending_searches ORDER BY ts_date DESC limit 21;
  26. cursor=db.query('SELECT distinct ts_word FROM trending_searches ORDER BY ts_date DESC limit 21;')
  27. display={}
  28. idx=1
  29. for c in cursor:
  30. display['a'+str(idx)]=c['ts_word']
  31. idx+=1
  32. display['client']=clientid
  33. display['email']=email
  34. display['campaign']=campaign
  35. outputText = template.render(display=display)
  36. fw=codecs.open('c:/tmp/final.html','w','utf-8')
  37. fw.write(outputText)
  38. fw.close()
  39. def send_email(campaign,email):
  40. gmail_user = 'edm@choozmo.com'
  41. gmail_password='wqdsyqwvppmubitv'
  42. sent_from = gmail_user
  43. #to = ['jared@choozmo.com','nina.huang@choozmo.com','ana@choozmo.com','ming@choozmo.com','mike@choozmo.com','andy@choozmo.com','hana@choozmo.com','stacy@choozmo.com','wen@choozmo.com','yukyo@choozmo.com','fxp87257@gmail.com','noodlesloves@gmail.com']
  44. to = [email]
  45. msg = MIMEMultipart()
  46. msg['Subject'] = '['+campaign+'] 趨勢日報 | ChoozMo '
  47. msg['From'] = 'edm@choozmo.com'
  48. msg['To'] = email
  49. msgAlternative = MIMEMultipart('alternative')
  50. msg.attach(msgAlternative)
  51. fr=codecs.open('c:/tmp/final.html','r','utf-8')
  52. content=fr.read()
  53. fr.close()
  54. text = MIMEText(content,'html','utf-8')
  55. msgAlternative.attach(text)
  56. try:
  57. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  58. server.ehlo()
  59. server.login(gmail_user, gmail_password)
  60. server.sendmail(sent_from, to, msg.as_string())
  61. server.close()
  62. print ('Email sent!')
  63. except:
  64. traceback.print_exc()
  65. print ('Something went wrong...')
  66. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/cmm_test?charset=utf8mb4')
  67. #cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='test';")
  68. #cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='choozmo';")
  69. #cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='g2';")
  70. #cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='zec';")
  71. #cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='core_cust';")
  72. #cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='g4';")
  73. #cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='g1';")
  74. cursor=db.query("SELECT id,email FROM cmm_test.edm_list where ugroup='g3';")
  75. for c in cursor:
  76. clientid=c['id']
  77. email=c['email']
  78. campaign='2021-06-13'
  79. gen_email(email,clientid,campaign)
  80. send_email(campaign,email)
  81. time.sleep(30)
  82. #clientid='1'
  83. #email='jeweiliang@gmail.com'