| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 | # coding=utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerfrom smtplib import SMTP_SSLfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplicationfrom email.mime.base import MIMEBasefrom email.encoders import encode_base64import tracebackimport osclass EmailUtil(object):    host_server = 'smtp.exmail.qq.com'    sender_email = 'zhangbr@elab-plus.com'    pwd = '306492mnA'    send_name = 'elab'    receiver = ['1285211525@qq.com', '15773153135@163.com']    def __init__(self):        pass    def send_mail(self,                  mail_title='elab-test',                  content=None,                  mail_excel=None                  ):        try:            smtp = SMTP_SSL(self.host_server)            smtp.set_debuglevel(1)            smtp.ehlo(self.host_server)            smtp.login(self.sender_email, self.pwd)            msg = MIMEMultipart('related')            msg['Subject'] = Header(mail_title, 'utf-8')            msg['From'] = self.send_name            msgAlternative = MIMEMultipart('alternative')            msg.attach(msgAlternative)            if content:                textApart = MIMEText(content)                msg.attach(textApart)            if mail_excel:                part = MIMEBase('application', "vnd.ms-excel")                with open(mail_excel, 'rb') as fp:                    part.set_payload(fp.read())                    encode_base64(part)                    part.add_header('Content-Disposition', f'attachment; filename="{os.path.split(mail_excel)[1]}"')                    msg.attach(part)            for mail in self.receiver:                msg['To'] = mail                try:                    print(mail)                    smtp.sendmail(self.sender_email, mail, msg.as_string())                except Exception as e:                    smtp.sendmail(self.sender_email, mail, msg.as_string())                    print(str(e))            smtp.quit()            print('Success!')        except:            print('Error!')            traceback.print_exc()if __name__ == '__main__':    send_email = EmailUtil()    send_email.send_mail('elab_test', mail_excel=r'D:\elab\elab_mvp\resources\tongce1.xlsx')    pass
 |