chz_gmaillib.py 4.6 KB

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