1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Python发送以整个文件夹的内容为附件的邮件的教程

Python发送以整个文件夹的内容为附件的邮件的教程

时间:2023-04-01 16:14:23

相关推荐

Python发送以整个文件夹的内容为附件的邮件的教程

后端开发|Python教程

Python

后端开发-Python教程

由于我经常需要备份文件夹下的内容到邮件里面,每个打开邮件,上传文件,发送,太过麻烦,其实每次发送的文件都是放在固定 置的,只是邮件标题不同而已,于是用 python 为自己写了个发送文件到邮箱的小工具,在任意目录下执行该脚本,并指定邮件标 ,就将指定文件夹下的文件发送到邮箱中备份起来 。

火狐浏览器看源码,Vscode直穿码云,ubuntu移除ibus,scp tomcat8,爬虫 图片 node,php sql多表查询,襄阳外包seo推广公司排名,本地搭建php网站,威兔手机模板(wap)_4.3lzw

#!/usr/bin/env python# coding: utf-8from smtplib import SMTP, quotedata, CRLF, SMTPDataErrorfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEBase import MIMEBasefrom email.MIMEText import MIMETextfrom email import Encodersfrom sys import stderr, stdoutimport osimport sysclass ExtendedSMTP(SMTP): def data(self, msg): self.putcmd("data") (code,repl)=self.getreply() if self.debuglevel > 0 : print >> stderr, "data:", (code, repl) if code != 354:raise SMTPDataError(code,repl) else:q = quotedata(msg)if q[-2:] != CRLF: q = q + CRLFq = q + "." + CRLF# begin modified send codechunk_size = 2048bytes_sent = 0while bytes_sent != len(q): chunk = q[bytes_sent:bytes_sent+chunk_size] self.send(chunk) bytes_sent += len(chunk) if hasattr(self, "callback"):self.callback(bytes_sent, len(q))# end modified send code(code,msg)=self.getreply()if self.debuglevel >0 : print>>stderr, "data:", (code,msg)return (code,msg)def callback(progress, total): percent = 100. * progress / total stdout.write(\ ) stdout.write("%s bytes sent of %s [%2.0f%%]" % (progress, total, percent)) stdout.flush() if percent >= 100: stdout.write(\ )def sendmail(subject): MAIL_FROM = mymail@ MAIL_TO = [mymail@] BAK_DIR = /path/to/bak/folder msg = MIMEMultipart() msg[From] = MAIL_FROM msg[Subject] = subject msg.attach( MIMEText( est send attachment) ) for filename in os.listdir(BAK_DIR): part = MIMEBase(application, "octet-stream") part.set_payload(open(os.path.join(BAK_DIR, filename),"rb").read() ) Encoders.encode_base64(part) part.add_header(Content-Disposition, attachment; filename="%s" % os.path.basename(filename)) msg.attach(part) try: smtp = ExtendedSMTP() smtp.callback = callback smtp.connect(\, 25) smtp.login(mymail, mypwd) smtp.sendmail(MAIL_FROM, MAIL_TO, msg.as_string()) smtp.close() os.system( m -f %s/* % BAK_DIR) except Exception, e: print eif __name__ == \__main__: if len(sys.argv) == 1: print Please specific a subject print Usage: send_files else: sendmail(sys.argv[1])

cj60lib源码下载,虚拟机跑vscode很卡,ubuntu 改为中文,运维打开tomcat,爬虫网不好,php工程师的发展前景,seo全网营销平台霸屏推广,网站禁用右键lzw

安装:

同城恋爱源码,vscode无法商店扩展,ubuntu 鼠标 快,进入tomcat项目目录,爬虫测试 Python,php 读取文件分页,盐田区seo推广服务,淘宝刷单网站源码,bootstrap oa 模板lzw

配置好收件人,发件人,smtp地址,用户名,密码及要发送文件所在的路径。

将文件保存为 send_files,保存到 /usr/bin 下面。

然后设置文件权限为可执行:

$ chmod +x send_files

用法:

$ send_files 邮件标题

还带有进度条哦~~

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。