1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| import smtplib import time import xlrd from email import encoders from email.header import Header from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import parseaddr,formataddr from email.mime.application import MIMEApplication from datetime import date,datetime sender = '###@qq.com' receiver = '2956150544@qq.com' def read_excel():
ExceFileUrl=xlrd.open_workbook(r'../data/email_list.xlsx') sheet_date = ExceFileUrl.sheet_by_index(0) email_data = sheet_date.col_values(1) return email_data
def sender_mail(): smt_p = smtplib.SMTP() smt_p.connect(host='smtp.qq.com', port=25) password="###" smt_p.login(sender,password) count_num = 1 for i in read_excel(): try: msg = MIMEMultipart() msg['From'] = "brook" msg['To'] = i msg['Cc'] = 'CQ3754@126.com' msg['subject'] = Header('通知', 'utf-8') msg.attach(MIMEText('您好,' 'XXX2.0全新升级,XXX1.0版本到2018年10月31号停止所有服务。','plain', 'utf-8')) xlsxpart=MIMEApplication(open(r'../data/email_test.xlsx','rb').read()) xlsxpart.add_header('Content-Disposition','attachment',filename='1.xlsx') msg.attach(xlsxpart) message_docx1 = MIMEText(open(r'../data/email_test.docx', 'rb').read(), 'base64', 'utf8') message_docx1.add_header('crontent-disposition', 'attachment', filename='测试.docx') msg.attach(message_docx1) message_image = MIMEText(open(r'../data/email_test.jpg', 'rb').read(), 'base64', 'utf8') message_image.add_header('content-disposition', 'attachment', filename='plot2.jpg') msg.attach(message_image) smt_p.sendmail(sender,i,msg.as_string()) time.sleep(10) print('第%d次发送给%s' % (count_num,i)) count_num = count_num + 1 except (UnicodeEncodeError,smtplib.SMTPRecipientsRefused,smtplib.SMTPSenderRefused,AttributeError) as e: print('第%d次给%s发送邮件异常' % (count_num,i)) continue smt_p.quit() sender_mail()
|