123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import smtplib
- import traceback
- import os
- from email.mime.text import MIMEText
- from email.mime.image import MIMEImage
- from email.mime.multipart import MIMEMultipart
- from email.message import EmailMessage
- import codecs
- import jinja2
- def mail2(mailobj):
- gmail_user = mailobj['gmail_user']
- gmail_password=mailobj['gmail_password']
- sent_from = gmail_user
- toaddr=mailobj['toaddr']
- to = [toaddr]
- msg = MIMEMultipart()
- msg['Subject'] = mailobj['title']
- msg['From'] = gmail_user
- msg['To'] = toaddr
- # 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']
- image_paths=['c:/data/edm/20220504/images/img1.png','c:/data/edm/20220504/images/img12.jpeg']
- msgAlternative = MIMEMultipart('alternative')
- msg.attach(msgAlternative)
- #fr=codecs.open('c:/tmp/final.html','r','utf-8')
- fr=codecs.open('c:/data/edm/20220504/index.html','r','utf-8')
- content=fr.read()
- fr.close()
- display=mailobj['display']
- templateLoader = jinja2.FileSystemLoader(searchpath='c:/data/edm/20220504')
- templateEnv = jinja2.Environment(loader=templateLoader)
- TEMPLATE_FILE = 'index.html'
- template = templateEnv.get_template(TEMPLATE_FILE)
- outputText = template.render(display=display)
- content=outputText
- text = MIMEText(content,'html','utf-8')
- counter = 1
- for fp in image_paths:
- fp = open(fp, 'rb')
- msgImage = MIMEImage(fp.read())
- fp.close()
- # Define the image's ID as referenced above
- msgImage.add_header('Content-ID', '<image'+str(counter)+'>')
- msgAlternative.attach(msgImage)
- counter += 1
- msgAlternative.attach(text)
- try:
- server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
- server.ehlo()
- server.login(gmail_user, gmail_password)
- server.sendmail(sent_from, to, msg.as_string())
- server.close()
- print ('Email sent!')
- except:
- traceback.print_exc()
- print ('Something went wrong...')
- def mail_to_other(mailobj):
- gmail_user = mailobj['gmail_user']
- gmail_password=mailobj['gmail_password']
- sent_from = gmail_user
- toaddr=mailobj['toaddr']
- to = [toaddr]
- msg = MIMEMultipart()
- msg['Subject'] = mailobj['title']
- msg['From'] = gmail_user
- msg['To'] = toaddr
- image_paths=['c:/data/edm/20220504/images/img1.png','c:/data/edm/20220504/images/img2.jpeg']
- msgAlternative = MIMEMultipart('alternative')
- msg.attach(msgAlternative)
- #fr=codecs.open('c:/tmp/final.html','r','utf-8')
- fr=codecs.open('c:/data/edm/20220504/index.html','r','utf-8')
- content=fr.read()
- fr.close()
- display=mailobj['display']
- # display={}
- # display['custname']='敬啟者'
- # display['client']='j22332'
- # display['email']=toaddr
- # display['campaign']='20220305-test'
- templateLoader = jinja2.FileSystemLoader(searchpath='c:/data/edm/20220504')
- templateEnv = jinja2.Environment(loader=templateLoader)
- TEMPLATE_FILE = 'index.html'
- template = templateEnv.get_template(TEMPLATE_FILE)
- outputText = template.render(display=display)
- content=outputText
- text = MIMEText(content,'html','utf-8')
- counter = 1
- for fp in image_paths:
- fp = open(fp, 'rb')
- msgImage = MIMEImage(fp.read())
- fp.close()
- # Define the image's ID as referenced above
- msgImage.add_header('Content-ID', '<image'+str(counter)+'>')
- msgAlternative.attach(msgImage)
- counter += 1
- msgAlternative.attach(text)
- try:
- server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
- server.ehlo()
- server.login(gmail_user, gmail_password)
- server.sendmail(sent_from, to, msg.as_string())
- server.close()
- print ('Email sent!')
- except:
- traceback.print_exc()
- print ('Something went wrong...')
- #mailobj={}
- #mailobj['toaddr']='jeweiliang@gmail.com'
- #mailobj['title']=' jeweiliang x ChoozMo 合作洽談 '
- #display={}
- #display['custname']='jeweiliang'
- #display['client']='3323'
- #display['email']=mailobj['toaddr']
- #display['campaign']='testing'
- #mailobj['display']=display
- #mail2(mailobj)
|