介绍
这篇文章给大家分享的是有关Python3怎么实现带附件定时发送邮件功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
Python的优点有哪些
1,简单易用,与C/c++、Java、c#等传统语言相比,Python对代码格式的要求没有那么严格;2,Python属于开源的,所有人都可以看到源代码,并且可以被移植在许多平台上使用;3,Python面向对象,能够支持面向过程编程,也支持面向对象编程;4,Python是一种解释性语言,Python写的程序不需要编译成二进制代码,可以直接从源代码运行程序;5,Python功能强大,拥有的模块众多,基本能够实现所有的常见功能。
Python3定时发送邮件功能的具体内容如下
1,导入模块
import 操作系统 import datetime #定时发送,以及日期 import shutil #文件操作 import smtplib #邮件模块 得到email.mime.text import  MIMEText 得到email.mime.multipart import  MIMEMultipart 得到email.header import 头 import 时间 import xlwt # excel写入
2,写入excel
def eWrite (fLocate、file_sheet file_subject, style0): 尝试才能: ,,,if os.path.exists (fLocate): ,,,,,os.remove (fLocate),, #,如果文件存在,则删除 ,,,f =, xlwt.Workbook(编码=& # 39;utf - 8 # 39;),, #打开excel文件 ,,,fs =, f.add_sheet (file_sheet), #表名 ,,,subject =,列表(file_subject),, #列表化 ,,,for 小姐:范围(len(主题):拷贝,#找到日期列 ,,,,,if & # 39;日期& # 39;,拷贝主题(我): ,,,,,,col_num=我 ,,,for 小姐:范围(len(主题):拷贝,#表标题 ,,,,,fs.write(0,,我,,主题[我]) ,,,for 小姐:范围(10):拷贝,#单元格宽度为 ,,,,,fs.col (i) .width=3333 ,,,印刷(“WRITE FINISHED") ,,,f.save (fLocate) except 才能: ,,,print (“WRITE FAILED")
3,发送邮件
def eSend(发送方、接收方、用户名、密码、smtpserver主题,e_content, file_path, file_name): 尝试才能: #邮件头 ,,,message =, MIMEMultipart () ,,,消息[& # 39;从# 39;],=,发送方#发送 ,,,消息[& # 39;使# 39;],=,,,,. join(接收器)#收件 ,,,消息[& # 39;主题# 39;],=,头(主题,& # 39;utf - 8 # 39;) ,,,message.attach (MIMEText (e_content, & # 39;平原# 39;,,& # 39;utf - 8 # 39;)) #,邮件正文 #,构造附件 ,,,att1 =, MIMEText(开放(file_path + file_name & # 39; rb # 39;) .read (), & # 39; base64 # 39;,, & # 39; utf - 8 # 39;) ,,,att1 [“Content-Type"],=, & # 39;应用程序/八进制# 39; ,,,att1 [“Content-Disposition"],=,“附件;文件名=? file_name ,,,message.attach (att1) #执行 ,,,smtp =, smtplib.SMTP () ,,,smtp.connect (smtpserver), #连接服务器 ,,,smtp.login(用户名,密码),#登录 ,,,smtp.sendmail(发送器,接收器,message.as_string()), #发送 ,,,smtp.quit () ,,,印刷(“SEND") 除了才能: ,,,印刷(“SEND FAILED")
4,配置与执行
while 正确的: #配置 # __time_____才能 ehour=11 #才能定时小时 艾敏=49 #才能定时分钟 esec=50 #才能定时秒 时间=current_time 才能;time.localtime (time.time()),,, #当前时间 cur_time 才能=,time.strftime (& # 39; % H %猴# 39;,,time.localtime (time.time())),,,,,,, #当前时间str # __mysql_____才能 # __email_____才能 时间=sender 才能;& # 39;& # 39;,#,发件人邮箱 receiver 才能=,(& # 39;453032441 @qq.com& # 39;], #,收件人邮箱,可以多个(列表形式)群发 时间=username 才能;& # 39;& # 39;,#,发件人姓名 时间=password 才能;& # 39;& # 39;,#,smtp密码,qq是给你分配一串,163年是自己设置 时间=smtpserver 才能;& # 39;& # 39;,#,邮箱服务器 subject 才能=,“嘿,这是# 39;s something interesting",,, #邮件标题 e_content 才能=,& # 39;{0:^ 27}\ n {1: ^ 27} \ n {25} 2: ^ \ n {25} 3: ^ & # 39; .format(& # 39;我# 39;& # 39;/,,\ \ & # 39;,& # 39;(——)& # 39;,& # 39;(- - - - - - - - - -)& # 39;),,#邮件正文 # __file_____才能 file_path =,才能“D:/?, #文件位置 file_name才能=皊hit.xls",, #文件名 时间=fLocate 才能;file_path +, file_name ,, #文件路径 file_subject才能=& # 39;我# 39;,,& # 39;小姐# 39;,,& # 39;u # 39;, #表标题 file_sheet=& # 39;才能好# 39;#表名 style0才能=xlwt.XFStyle () style0.num_format_str才能=& # 39;YYYY-MM-DD& # 39; #操作 null null null null null nullPython3怎么实现带附件定时发送邮件功能