這篇文章給大家分享的是有關Python3怎么實現帶附件定時發送郵件功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、簡單易用,與C/C++、Java、C# 等傳統語言相比,Python對代碼格式的要求沒有那么嚴格;2、Python屬于開源的,所有人都可以看到源代碼,并且可以被移植在許多平臺上使用;3、Python面向對象,能夠支持面向過程編程,也支持面向對象編程;4、Python是一種解釋性語言,Python寫的程序不需要編譯成二進制代碼,可以直接從源代碼運行程序;5、Python功能強大,擁有的模塊眾多,基本能夠實現所有的常見功能。
Python3定時發送郵件功能的具體內容如下
1、 導入模塊
import os import datetime #定時發送,以及日期 import shutil #文件操作 import smtplib #郵件模塊 from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header import time import xlwt #excel寫入
2、寫入EXCEL
def eWrite(fLocate,file_sheet,file_subject,style0): try: if os.path.exists(fLocate): os.remove(fLocate) # 如果文件存在,則刪除 f = xlwt.Workbook(encoding='utf-8') #打開excel文件 fs = f.add_sheet(file_sheet) #sheet名 subject = list(file_subject) #列表化 for i in range(len(subject)): #找到日期列 if '日期' in subject[i]: col_num=i for i in range(len(subject)): #sheet標題 fs.write(0, i, subject[i]) for i in range(10): #單元格寬度為 fs.col(i).width=3333 print("WRITE FINISHED") f.save(fLocate) except : print ("WRITE FAILED")
3、發送郵件
def eSend(sender,receiver,username,password,smtpserver,subject,e_content,file_path,file_name): try: #郵件頭 message = MIMEMultipart() message['From'] = sender#發送 message['To'] = ",".join(receiver)#收件 message['Subject'] = Header(subject, 'utf-8') message.attach(MIMEText(e_content, 'plain', 'utf-8'))# 郵件正文 # 構造附件 att1 = MIMEText(open(file_path+file_name,'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = "attachment;filename="+file_name message.attach(att1) #執行 smtp = smtplib.SMTP() smtp.connect(smtpserver) #連接服務器 smtp.login(username, password) #登錄 smtp.sendmail(sender, receiver, message.as_string()) #發送 smtp.quit() print("SEND") except: print("SEND FAILED")
4、配置與執行
while True: #配置 #__time_____ ehour=11#定時小時 emin=49#定時分鐘 esec=50#定時秒 current_time = time.localtime(time.time()) #當前時間date cur_time = time.strftime('%H%M', time.localtime(time.time())) #當前時間str #__mysql_____ #__email_____ sender = '' # 發件人郵箱 receiver = ['453032441@qq.com'] # 收件人郵箱,可以多個(列表形式)群發 username = '' # 發件人姓名 password = '' # smtp密碼,qq是給你分配一串,163是自己設置 smtpserver = '' # 郵箱服務器 subject = "Hey,here's something interesting" #郵件標題 e_content = '{0:^27}\n{1:^27}\n{2:^25}\n{3:^25}'.format('i','/ \\','(-----)','(--------)') #郵件正文 #__file_____ file_path = "D:/" #文件位置 file_name="shit.xls" #文件名 fLocate = file_path + file_name #文件路徑 file_subject='I', 'MISS', 'U' #sheet標題 file_sheet='ok' #sheet名 style0=xlwt.XFStyle() style0.num_format_str='YYYY-MM-DD' #操作 if ((current_time.tm_hour == ehour) and (current_time.tm_min == emin) and (current_time.tm_sec == esec)): print ("START") eWrite(fLocate, file_sheet, file_subject,style0) eSend(sender, receiver, username, password, smtpserver, subject, e_content, file_path,file_name) print(cur_time) time.sleep(1)
感謝各位的閱讀!關于“Python3怎么實現帶附件定時發送郵件功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。