mtest.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #gmail_user = 'edm@choozmo.com'
  11. #gmail_user = 'video@choozmo.com'
  12. #gmail_password='Chuz2020'
  13. gmail_user = 'jared@choozmo.com'
  14. gmail_password='jgferbhelzrtgsbb'
  15. #gmail_password='wqdsyqwvppmubitv'
  16. sent_from = gmail_user
  17. #toaddr='jeweiliang@gmail.com'
  18. toaddr='jeweiliang@gmail.com'
  19. #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']
  20. to = [toaddr]
  21. #to = ['jared@choozmo.com']
  22. msg = MIMEMultipart()
  23. msg['Subject'] = '合作洽談 | ChoozMo '
  24. msg['From'] = 'jared@choozmo.com'
  25. msg['To'] = toaddr
  26. 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']
  27. msgAlternative = MIMEMultipart('alternative')
  28. msg.attach(msgAlternative)
  29. #fr=codecs.open('c:/tmp/final.html','r','utf-8')
  30. fr=codecs.open('c:/tmp/index.html','r','utf-8')
  31. content=fr.read()
  32. fr.close()
  33. display={}
  34. display['custname']='敬啟者'
  35. display['client']='j22332'
  36. display['email']=toaddr
  37. display['campaign']='20220305-test'
  38. templateLoader = jinja2.FileSystemLoader(searchpath='c:/tmp')
  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...')