1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- # coding=utf-8
- from email.mime.text import MIMEText
- from email.header import Header
- from smtplib import SMTP
- from email.mime.multipart import MIMEMultipart
- from email.mime.base import MIMEBase
- from email.encoders import encode_base64
- import traceback
- import os
- class 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(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 = EmailUtils()
- send_email.send_mail('elab_test', mail_excel=r'D:\elab-code\elab_mvp\resources\tongce1.xlsx')
- pass
|