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 # mailobj['toaddr'] # mailobj['title'] #'合作洽談 | ChoozMo ' def mail_to_other(mailobj): gmail_user = 'jared@choozmo.com' gmail_password='jgferbhelzrtgsbb' sent_from = gmail_user toaddr=mailobj['toaddr'] to = [toaddr] msg = MIMEMultipart() msg['Subject'] = mailobj['title'] msg['From'] = 'jared@choozmo.com' msg['To'] = toaddr 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'] msgAlternative = MIMEMultipart('alternative') msg.attach(msgAlternative) #fr=codecs.open('c:/tmp/final.html','r','utf-8') fr=codecs.open('c:/tmp/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:/tmp') 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', '') 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...')