chz_gmaillib.py 4.2 KB

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