Since I often need to back up the contents of the folder into the email. Every time I open the email, upload the file, and send it, it is too troublesome. In fact, the files sent are placed in a fixed position, but the email title is different. So I wrote a widget for myself to send files to the email address in Python, execute the script in any directory, and specify the email tag, and send the files under the specified folder to the email address to back up.
#!/usr/bin/env python # coding: utf-8 from smtplib import SMTP, quotedata, CRLF, SMTPDataError from import MIMEMultipart from import MIMEBase from import MIMEText from email import Encoders from sys import stderr, stdout import os import sys class ExtendedSMTP(SMTP): def data(self, msg): ("data") (code,repl)=() if > 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] (chunk) bytes_sent += len(chunk) if hasattr(self, "callback"): (bytes_sent, len(q)) # end modified send code (code,msg)=() if >0 : print>>stderr, "data:", (code,msg) return (code,msg) def callback(progress, total): percent = 100. * progress / total ('\r') ("%s bytes sent of %s [%2.0f%%]" % (progress, total, percent)) () if percent >= 100: ('\n') def sendmail(subject): MAIL_FROM = 'mymail@' MAIL_TO = ['mymail@'] BAK_DIR = '/path/to/bak/folder' msg = MIMEMultipart() msg['From'] = MAIL_FROM msg['Subject'] = subject ( MIMEText('test send attachment') ) for filename in (BAK_DIR): part = MIMEBase('application', "octet-stream") part.set_payload(open((BAK_DIR, filename),"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % (filename)) (part) try: smtp = ExtendedSMTP() = callback ('', 25) ('mymail', 'mypwd') (MAIL_FROM, MAIL_TO, msg.as_string()) () ('rm -f %s/*' % BAK_DIR) except Exception, e: print e if __name__ == '__main__': if len() == 1: print 'Please specific a subject' print 'Usage: send_files <MAIL_SUBJECT>' else: sendmail([1])
Install:
Configure the recipient, sender, smtp address, username, password and the path where the file to be sent.
Save the file as send_files and save it under /usr/bin.
Then set the file permissions to be executable:
$ chmod +x send_files
usage:
$ send_files 'Email Title'
There is also a progress bar~~