Python發(fā)送以整個文件夾的內(nèi)容為附件的郵件的教程
由于我經(jīng)常需要備份文件夾下的內(nèi)容到郵件里面,每個打開郵件,上傳文件,發(fā)送,太過麻煩,其實每次發(fā)送的文件都是放在固定 置的,只是郵件標題不同而已,于是用 python 為自己寫了個發(fā)送文件到郵箱的小工具,在任意目錄下執(zhí)行該腳本,并指定郵件標 ,就將指定文件夾下的文件發(fā)送到郵箱中備份起來 。
#!/usr/bin/env python # coding: utf-8 from smtplib import SMTP, quotedata, CRLF, SMTPDataError from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders from sys import stderr, stdout import os import sys class 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 + CRLF q = q + "." + CRLF # begin modified send code chunk_size = 2048 bytes_sent = 0 while 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('\r') stdout.write("%s bytes sent of %s [%2.0f%%]" % (progress, total, percent)) stdout.flush() if percent >= 100: stdout.write('\n') def sendmail(subject): MAIL_FROM = 'mymail@qq.com' MAIL_TO = ['mymail@qq.com'] BAK_DIR = '/path/to/bak/folder' msg = MIMEMultipart() msg['From'] = MAIL_FROM msg['Subject'] = subject msg.attach( MIMEText('test 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('smtp.qq.com', 25) smtp.login('mymail', 'mypwd') smtp.sendmail(MAIL_FROM, MAIL_TO, msg.as_string()) smtp.close() os.system('rm -f %s/*' % BAK_DIR) except Exception, e: print e if __name__ == '__main__': if len(sys.argv) == 1: print 'Please specific a subject' print 'Usage: send_files <MAIL_SUBJECT>' else: sendmail(sys.argv[1])
安裝:
配置好收件人,發(fā)件人,smtp地址,用戶名,密碼及要發(fā)送文件所在的路徑。
將文件保存為 send_files,保存到 /usr/bin 下面。
然后設(shè)置文件權(quán)限為可執(zhí)行:
$ chmod +x send_files
用法:
$ send_files '郵件標題'
還帶有進度條哦~~
- python登錄pop3郵件服務(wù)器接收郵件的方法
- python實現(xiàn)批量解析郵件并下載附件
- Python3使用SMTP發(fā)送帶附件郵件
- python發(fā)送郵件的實例代碼(支持html、圖片、附件)
- 用Python實現(xiàn)一個簡單的能夠發(fā)送帶附件的郵件程序的教程
- python實現(xiàn)發(fā)送郵件及附件功能
- 二種python發(fā)送郵件實例講解(python發(fā)郵件附件可以使用email模塊實現(xiàn))
- 詳解python實現(xiàn)讀取郵件數(shù)據(jù)并下載附件的實例
- Python實現(xiàn)讀取郵箱中的郵件功能示例【含文本及附件】
- python+POP3實現(xiàn)批量下載郵件附件
相關(guān)文章
Python PyAutoGUI模塊控制鼠標和鍵盤實現(xiàn)自動化任務(wù)詳解
這篇文章主要介紹了Python PyAutoGUI模塊控制鼠標和鍵盤實現(xiàn)自動化任務(wù),結(jié)合實例形式詳細分析了pyautogui模塊的安裝、導(dǎo)入以及針對鼠標與鍵盤的各種常見響應(yīng)操作實現(xiàn)技巧,需要的朋友可以參考下2018-09-09python連接mysql數(shù)據(jù)庫并讀取數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了python連接mysql數(shù)據(jù)庫并讀取數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09解決TypeError: Object of type xxx is&
這篇文章主要介紹了解決TypeError: Object of type xxx is not JSON serializable錯誤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Python實戰(zhàn)之多種音樂格式批量轉(zhuǎn)換
Pydub是一個基于ffmpeg的Python音頻處理模塊,封裝了許多ffmpeg底層接口,因此用它來做音樂歌曲文件格式轉(zhuǎn)換會非常方便。今天給大家介紹它的音樂文件格式轉(zhuǎn)換功能,幾乎支持所有音樂音頻格式,需要的可以參考一下2022-06-06詳解Python 中的 defaultdict 數(shù)據(jù)類型
這篇文章主要介紹了Python 中的 defaultdict 數(shù)據(jù)類型,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02