chz_gmaillib.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. def mail2(mailobj):
  11. # gmail_user = 'jared@choozmo.com'
  12. # gmail_password='jgferbhelzrtgsbb'
  13. # gmail_user = 'service@choozmo.com'
  14. # gmail_password='umueijckijzmbpco'
  15. gmail_user = mailobj['gmail_user']
  16. gmail_password=mailobj['gmail_password']
  17. sent_from = gmail_user
  18. toaddr=mailobj['toaddr']
  19. to = [toaddr]
  20. msg = MIMEMultipart()
  21. msg['Subject'] = mailobj['title']
  22. # msg['From'] = 'jared@choozmo.com'
  23. msg['From'] = gmail_user
  24. msg['To'] = toaddr
  25. image_paths=['c:/data/mail/mail3/images/image-1.png','c:/data/mail/mail3/images/image-2.png','c:/data/mail/mail3/images/image-3.png','c:/data/mail/mail3/images/image-4.png','c:/data/mail/mail3/images/image-5.png']
  26. msgAlternative = MIMEMultipart('alternative')
  27. msg.attach(msgAlternative)
  28. #fr=codecs.open('c:/tmp/final.html','r','utf-8')
  29. fr=codecs.open('c:/data/mail/mail3/index.html','r','utf-8')
  30. content=fr.read()
  31. fr.close()
  32. display=mailobj['display']
  33. # display={}
  34. # display['custname']='敬啟者'
  35. # display['client']='j22332'
  36. # display['email']=toaddr
  37. # display['campaign']='20220305-test'
  38. templateLoader = jinja2.FileSystemLoader(searchpath='c:/data/mail/mail3')
  39. templateEnv = jinja2.Environment(loader=templateLoader)
  40. TEMPLATE_FILE = 'index.html'
  41. template = templateEnv.get_template(TEMPLATE_FILE)
  42. outputText = template.render(display=display)
  43. content=outputText
  44. text = MIMEText(content,'html','utf-8')
  45. counter = 1
  46. for fp in image_paths:
  47. fp = open(fp, 'rb')
  48. msgImage = MIMEImage(fp.read())
  49. fp.close()
  50. # Define the image's ID as referenced above
  51. msgImage.add_header('Content-ID', '<image'+str(counter)+'>')
  52. msgAlternative.attach(msgImage)
  53. counter += 1
  54. msgAlternative.attach(text)
  55. try:
  56. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  57. server.ehlo()
  58. server.login(gmail_user, gmail_password)
  59. server.sendmail(sent_from, to, msg.as_string())
  60. server.close()
  61. print ('Email sent!')
  62. except:
  63. traceback.print_exc()
  64. print ('Something went wrong...')
  65. # mailobj['toaddr']
  66. # mailobj['title']
  67. #'合作洽談 | ChoozMo '
  68. def mail_to_other(mailobj):
  69. gmail_user = mailobj['gmail_user']
  70. gmail_password=mailobj['gmail_password']
  71. sent_from = gmail_user
  72. toaddr=mailobj['toaddr']
  73. to = [toaddr]
  74. msg = MIMEMultipart()
  75. msg['Subject'] = mailobj['title']
  76. msg['From'] = gmail_user
  77. msg['To'] = toaddr
  78. image_paths=['c:/tmp/images/image-1.png','c:/tmp/images/image-2.png','c:/tmp/images/image-3.png','c:/tmp/images/image-4.png','c:/tmp/images/image-5.png','c:/tmp/images/image-6.png','c:/tmp/images/image-7.jpeg','c:/tmp/images/image-8.jpeg']
  79. msgAlternative = MIMEMultipart('alternative')
  80. msg.attach(msgAlternative)
  81. #fr=codecs.open('c:/tmp/final.html','r','utf-8')
  82. fr=codecs.open('c:/tmp/index.html','r','utf-8')
  83. content=fr.read()
  84. fr.close()
  85. display=mailobj['display']
  86. # display={}
  87. # display['custname']='敬啟者'
  88. # display['client']='j22332'
  89. # display['email']=toaddr
  90. # display['campaign']='20220305-test'
  91. templateLoader = jinja2.FileSystemLoader(searchpath='c:/tmp')
  92. templateEnv = jinja2.Environment(loader=templateLoader)
  93. TEMPLATE_FILE = 'index.html'
  94. template = templateEnv.get_template(TEMPLATE_FILE)
  95. outputText = template.render(display=display)
  96. content=outputText
  97. text = MIMEText(content,'html','utf-8')
  98. counter = 1
  99. for fp in image_paths:
  100. fp = open(fp, 'rb')
  101. msgImage = MIMEImage(fp.read())
  102. fp.close()
  103. # Define the image's ID as referenced above
  104. msgImage.add_header('Content-ID', '<image'+str(counter)+'>')
  105. msgAlternative.attach(msgImage)
  106. counter += 1
  107. msgAlternative.attach(text)
  108. try:
  109. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  110. server.ehlo()
  111. server.login(gmail_user, gmail_password)
  112. server.sendmail(sent_from, to, msg.as_string())
  113. server.close()
  114. print ('Email sent!')
  115. except:
  116. traceback.print_exc()
  117. print ('Something went wrong...')
  118. #mailobj={}
  119. #mailobj['toaddr']='jeweiliang@gmail.com'
  120. #mailobj['title']=' jeweiliang x ChoozMo 合作洽談 '
  121. #display={}
  122. #display['custname']='jeweiliang'
  123. #display['client']='3323'
  124. #display['email']=mailobj['toaddr']
  125. #display['campaign']='testing'
  126. #mailobj['display']=display
  127. #mail2(mailobj)