Python3實現(xiàn)帶附件的定時發(fā)送郵件功能
更新時間:2020年12月22日 17:01:48 作者:小彌彌子
這篇文章主要為大家詳細介紹了Python3實現(xiàn)帶附件的定時發(fā)送郵件功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python3定時發(fā)送郵件功能的具體代碼,供大家參考,具體內(nèi)容如下
1、 導(dǎo)入模塊
import os import datetime #定時發(fā)送,以及日期 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、發(fā)送郵件
def eSend(sender,receiver,username,password,smtpserver,subject,e_content,file_path,file_name): try: #郵件頭 message = MIMEMultipart() message['From'] = sender#發(fā)送 message['To'] = ",".join(receiver)#收件 message['Subject'] = Header(subject, 'utf-8') message.attach(MIMEText(e_content, 'plain', 'utf-8'))# 郵件正文 # 構(gòu)造附件 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) #執(zhí)行 smtp = smtplib.SMTP() smtp.connect(smtpserver) #連接服務(wù)器 smtp.login(username, password) #登錄 smtp.sendmail(sender, receiver, message.as_string()) #發(fā)送 smtp.quit() print("SEND") except: print("SEND FAILED")
4、配置與執(zhí)行
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 = '' # 發(fā)件人郵箱 receiver = ['453032441@qq.com'] # 收件人郵箱,可以多個(列表形式)群發(fā) username = '' # 發(fā)件人姓名 password = '' # smtp密碼,qq是給你分配一串,163是自己設(shè)置 smtpserver = '' # 郵箱服務(wù)器 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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pandas查詢數(shù)據(jù)df.query的使用
本文主要介紹了Pandas查詢數(shù)據(jù)df.query的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-07-07keras實現(xiàn)多種分類網(wǎng)絡(luò)的方式
這篇文章主要介紹了keras實現(xiàn)多種分類網(wǎng)絡(luò)的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python3.5基礎(chǔ)之函數(shù)的定義與使用實例詳解【參數(shù)、作用域、遞歸、重載等】
這篇文章主要介紹了Python3.5基礎(chǔ)之函數(shù)的定義與使用,結(jié)合實例形式詳細分析了Python3.5函數(shù)的定義、參數(shù)、作用域、遞歸、重載、內(nèi)置函數(shù)等基本概念與相關(guān)使用技巧,需要的朋友可以參考下2019-04-04輕量級的Web框架Flask 中模塊化應(yīng)用的實現(xiàn)
說到flask的模塊化,大家可能第一時間想到的都是藍圖,今天我們不討論藍圖,先從0.2版本中的Module類的實現(xiàn)講起2017-09-09