chz_gmail_temp.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 re
  11. import random as rd
  12. from personal_settings import *
  13. def mail2(mailobj):
  14. gmail_user = mailobj['gmail_user']
  15. gmail_password = mailobj['gmail_password']
  16. toaddr = mailobj['toaddr']
  17. to_text = mailobj['totext']
  18. to = [toaddr]
  19. sent_from = gmail_user
  20. msg = MIMEMultipart()
  21. msg['Subject'] = mailobj['title']
  22. msg['From'] = gmail_user
  23. msg['To'] = toaddr
  24. msg.attach(MIMEText(to_text))
  25. path = './templates/index.html'
  26. fr=codecs.open(path,'r','utf-8')
  27. html_=fr.read()
  28. image_paths=[]
  29. initial_count = 0
  30. dir_ = "./templates/images/"
  31. def sorted_alphanumeric(data):
  32. convert = lambda text: int(text) if text.isdigit() else text.lower()
  33. alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
  34. return sorted(data, key=alphanum_key)
  35. image_cate =[]
  36. for path in sorted_alphanumeric(os.listdir(dir_)):
  37. image_paths.append(dir_+path)
  38. image_cate.append(path.split(".")[-1])
  39. image_cate=set(image_cate)
  40. body_use=re.findall(r'<body.*?>',html_)[0]
  41. html_ =re.sub(r'<body.*?>', r"%s<img src='http://www.google-analytics.com/collect?v=1&tid=UA-153100602-5&cid=%d&t=event&ec=email&ea=open&el=%s&cs=newsletter&cm=email&cn=%d&dt=%d'>"%(body_use,rd.randint(1000,9999),toaddr,rd.randint(1000,9999),rd.randint(1000,9999)), html_)
  42. for j in image_cate:
  43. dir_2 ="images/image-%s.%s"
  44. for i in range(len(image_paths)):
  45. # print(dir_2%(i+1,j))
  46. html_ = html_.replace(dir_2%(i+1,j), "cid:image%s"%(i+1))
  47. msgAlternative = MIMEMultipart('alternative')
  48. msg.attach(msgAlternative)
  49. content=html_
  50. text = MIMEText(content,'html','utf-8')
  51. counter = 1
  52. for fp in image_paths:
  53. fp = open(fp, 'rb')
  54. msgImage = MIMEImage(fp.read())
  55. fp.close()
  56. # Define the image's ID as referenced above
  57. msgImage.add_header('Content-ID', '<image'+str(counter)+'>')
  58. msgAlternative.attach(msgImage)
  59. counter += 1
  60. msgAlternative.attach(text)
  61. try:
  62. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  63. server.ehlo()
  64. server.login(gmail_user, gmail_password)
  65. server.sendmail(sent_from, to, msg.as_string())
  66. server.close()
  67. print ('Email sent!')
  68. except:
  69. traceback.print_exc()
  70. print ('Something went wrong...')
  71. if __name__ == "__main__":
  72. mailobj={}
  73. mailobj['toaddr']=gmail_toaddr
  74. mailobj['gmail_user']=gmail_user
  75. mailobj['gmail_password']=gmail_password
  76. mailobj['title'] = gmail_title
  77. mailobj['totext'] = gmail_totext
  78. mail2(mailobj)