Python sends an email with the content of the entire folder as an attachment

Source: Internet
Author: User

Python sends an email with the content of the entire folder as an attachment

This article describes how to send an email with the content of the entire folder as an attachment in Python. In normal cases, attachments in free mail of the carrier can only be sent to files but not folders, this script can be used to send folders (strong in manual programming: D). For more information, see

Because I often need to back up the content in the folder to the email, it is too troublesome to open the email, upload the file, and send it. In fact, each file sent is fixed, only the mail title is different, so I used python to write a small tool for sending files to the mailbox, execute this script in any directory, and specify the mail label, send the files in the specified folder to the mailbox for backup.

?

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

43

44

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

81

82

#! /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 ['subobject'] = 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 t Exception, e:

Print e

 

If _ name _ = '_ main __':

If len (sys. argv) = 1:

Print 'Please specific a subobject'

Print 'usage: send_files <MAIL_SUBJECT>'

Else:

Sendmail (sys. argv [1])

Installation:

Configure the recipient, sender, smtp address, user name, password, and path of the file to be sent.

Save the file as send_files and save it to/usr/bin.

Then, set the File Permission to executable:

?

1

$ Chmod + x send_files

Usage:

?

1

$ Send_files 'email title'

It also has a progress bar ~~

Note <>: For more exciting tutorials, please pay attention to the help house 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.