发邮件
自動化任務完成后需要自動發送郵件記錄結果,發送郵件需要用到smtplib模塊,直接使用import導入即可:
163郵箱作為發件箱:在郵箱設置里面打開pop3/smtp,設置授權碼
1 import smtplib 2 from email.mime.text import MIMEText 3 username='185xxxxxxx@163.com' #發送者賬號 4 passwd='xxxxxx'#必須是授權碼 5 msg=MIMEText('新年好!')#郵件內容 6 msg['Subject']='昔年快樂' #郵件標題 7 msg['From']=username 8 msg['To']='8xxxxxx@qq.com' #發送到郵箱 9 msg['Cc']='xxxxxxx@qq.com' #抄送人郵箱 10 smtp=smtplib.SMTP('smtp.163.com',port=25)#連接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25 11 smtp.login(username,passwd)# 發送者的郵箱賬號,密碼 12 smtp.sendmail(username,'8xxxxxx@qq.com',msg.as_string())# 參數分別是發送者,接收者,第三個是把上面的發送郵件的內容變成字符串 13 smtp.quit()# 發送完畢后退出smtp 14 print('發送成功')qq郵箱作為發件箱:
1 import smtplib 2 from email.mime.text import MIMEText 3 username='8xxxxxxx@qq.com' 4 passwd='kpkvjjwlxqxgbcjd'#必須是授權碼 5 msg=MIMEText('新年好!') 6 msg['Subject']='昔年快樂' 7 msg['From']=username 8 msg['To']='xxxxxxxx@qq.com' 9 msg['Cc']='xxxxxxxx@qq.com' 10 smtp=smtplib.SMTP_SSL('smtp.qq.com',port=465) 11 smtp.login(username,passwd) 12 smtp.sendmail(username,'xxxxxxxx@qq.com',msg.as_string()) 13 smtp.quit() 14 print('發送成功')添加附件后發送郵件:
1 1 import smtplib 2 2 from email.mime.text import MIMEText 3 3 from email.mime.multipart import MIMEMultipart 4 4 username='xxxxxxx@163.com'# 發送者賬號 5 5 email_host = 'smtp.163.com'#郵箱地址 6 6 passwd='xxxxx'#163郵箱的授權碼 7 7 recv='xxxxxx@qq.com'#收件人郵箱,多個賬號的話,用逗號隔開 8 8 title='郵件標題' 9 9 content='發送郵件測試' 10 10 msg = MIMEMultipart() 11 11 file='url編碼.py'#附件的文件名 12 12 att = MIMEText(open(file,encoding='utf-8').read())#構建一個附件的U對象 13 13 att["Content-Type"] = 'application/octet-stream' 14 14 att["Content-Disposition"] = 'attachment; filename="%s"'%file#這個是在郵件中顯示的附件名 15 15 msg.attach(att)#把剛才創建好的附件,加到正文里面 16 16 msg.attach(MIMEText('郵件的內容。。。。'))#郵件正文的內容 17 17 msg['Subject'] = title # 郵件主題 18 18 msg['From'] = username # 發送者賬號 19 19 msg['To'] = recv # 接收者賬號列表 20 20 # smtp = smtplib.SMTP_SSL(email_host,port=456)#qq郵箱 21 21 smtp = smtplib.SMTP(email_host,port=25)#其他郵箱 22 22 smtp.login(username,passwd) 23 23 smtp.sendmail(username,recv,msg.as_string()) 24 24 smtp.quit()封裝成類,使用的時候直接調用函數,傳入郵箱賬號密碼,收件人,發件人,標題和內容即可:
1 import smtplib,os 2 from email.mime.text import MIMEText 3 from email.mime.multipart import MIMEMultipart 4 import base64 5 class SendMail(object): 6 def __init__(self,username,passwd,recv,title,content,email_host, 7 file=None,ssl=False, 8 port=25,ssl_port=465): 9 ''' 10 :param username: 用戶名 11 :param passwd: 密碼 12 :param recv: 收件人,多個要傳list ['a@qq.com','b@qq.com] 13 :param title: 郵件標題 14 :param content: 郵件正文 15 :param file: 附件路徑,如果不在當前目錄下,要寫絕對路徑,默認沒有附件 16 :param ssl: 是否安全鏈接,默認為普通 17 :param email_host: smtp服務器地址,默認為163服務器 18 :param port: 非安全鏈接端口,默認為25 19 :param ssl_port: 安全鏈接端口,默認為465 20 ''' 21 self.username = username #用戶名 22 self.passwd = passwd #密碼 23 self.recv = recv #收件人,多個要傳list ['a@qq.com','b@qq.com] 24 self.title = title #郵件標題 25 self.content = content #郵件正文 26 self.file = file #附件路徑,如果不在當前目錄下,要寫絕對路徑 27 self.email_host = email_host #smtp服務器地址 28 self.port = port #普通端口 29 self.ssl = ssl #是否安全鏈接 30 self.ssl_port = ssl_port #安全鏈接端口 31 def send_mail(self): 32 msg = MIMEMultipart() 33 #發送內容的對象 34 if self.file:#處理附件的 35 file_name = os.path.split(self.file)[-1]#只取文件名,不取路徑 36 try: 37 f = open(self.file, 'rb').read() 38 except Exception as e: 39 raise Exception('附件打不開!!!!') 40 else: 41 att = MIMEText(f,"base64", "utf-8") 42 att["Content-Type"] = 'application/octet-stream' 43 #base64.b64encode(file_name.encode()).decode() 44 new_file_name='=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?=' 45 #這里是處理文件名為中文名的,必須這么寫 46 att["Content-Disposition"] = 'attachment; filename="%s"'%(new_file_name) 47 msg.attach(att) 48 msg.attach(MIMEText(self.content))#郵件正文的內容 49 msg['Subject'] = self.title # 郵件主題 50 msg['From'] = self.username # 發送者賬號 51 msg['To'] = ','.join(self.recv) # 接收者賬號列表 52 if self.ssl: 53 self.smtp = smtplib.SMTP_SSL(self.email_host,port=self.ssl_port) 54 else: 55 self.smtp = smtplib.SMTP(self.email_host,port=self.port) 56 #發送郵件服務器的對象 57 self.smtp.login(self.username,self.passwd) 58 try: 59 self.smtp.sendmail(self.username,self.recv,msg.as_string()) 60 pass 61 except Exception as e: 62 print('出錯了。。',e) 63 else: 64 print('發送成功!') 65 self.smtp.quit() 66 67 68 if __name__ == '__main__': 69 m = SendMail( 70 username='xxxxx@qq.com', 71 passwd='cjkkvbbcdfrabejf', 72 recv=['xxxxxx@qq.com'], 73 title='郵件標題', 74 content='祝aa新年快樂,萬事如意,心想事成,狗年大吉!', 75 email_host='smtp.qq.com', 76 file=r'C:\Users\DELL\Desktop\郵件附件.txt',ssl=True, 77 ) 78 m.send_mail()?
轉載于:https://www.cnblogs.com/wxcx/p/8454444.html
總結
- 上一篇: 线性一致性与全序广播------《Des
- 下一篇: 为什么有的系统的事务码BSP_WD_CM