A tutorial on Python sending messages with the contents of an entire folder as attachments

Source: Internet
Author: User
Tags file permissions

This article mainly describes a tutorial that Python sends messages that are attached to the contents of the entire folder. Ordinary we are in the carrier free mailbox send attachments usually can only send files and cannot send folders, and the script can be implemented to send the folder (their own hands-on programming powerful: D), the need for friends can refer to the

Because I often need to back up the contents of the folder in the mail, each open mail, upload files, send, too troublesome, in fact, each send the files are placed in a fixed, but the message header is different, so with Python for their own write a file to the mailbox of a gadget, in any directory to execute the script, and specify the message headers, the files under the specified folder are sent to the mailbox to be backed up.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66-67-68 69 70 71 72 73 74 75 76 77 78 79-80 #!/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 Exten DEDSMTP (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:]!= CR Lf: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, "CA Llback "): 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.flu SH () 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 &nb Sp 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 ', ' 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_fil Es <MAIL_SUBJECT> ' Else:sendmail (sys.argv[1])

Installation:

Configure the recipient, sender, SMTP address, username, password, and the path where you want to send the file.

Save the file as Send_files and save it under/usr/bin.

Then set the file permissions to executable:

?

1 $ chmod +x Send_files

Usage:

?

1 $ send_files ' mail title '

Also with the progress bar Oh ~ ~

Note < > : More Wonderful tutorials please focus on Triple programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.