blob: 3c7c770dda79abf799931cceebd9dd395000bcb2 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Georg Brandl116aa622007-08-15 14:28:22 +00002
3"""Send the contents of a directory as a MIME message."""
4
5import os
6import sys
7import smtplib
8# For guessing MIME type based on file name extension
9import mimetypes
10
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030011from argparse import ArgumentParser
Georg Brandl116aa622007-08-15 14:28:22 +000012
13from email import encoders
14from email.message import Message
15from email.mime.audio import MIMEAudio
16from email.mime.base import MIMEBase
17from email.mime.image import MIMEImage
18from email.mime.multipart import MIMEMultipart
19from email.mime.text import MIMEText
20
21COMMASPACE = ', '
22
23
24def main():
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030025 parser = ArgumentParser(description="""\
Georg Brandl116aa622007-08-15 14:28:22 +000026Send the contents of a directory as a MIME message.
Georg Brandl116aa622007-08-15 14:28:22 +000027Unless the -o option is given, the email is sent by forwarding to your local
28SMTP server, which then does the normal delivery process. Your local machine
29must be running an SMTP server.
30""")
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030031 parser.add_argument('-d', '--directory',
32 help="""Mail the contents of the specified directory,
33 otherwise use the current directory. Only the regular
34 files in the directory are sent, and we don't recurse to
35 subdirectories.""")
36 parser.add_argument('-o', '--output',
37 metavar='FILE',
38 help="""Print the composed message to FILE instead of
39 sending the message to the SMTP server.""")
40 parser.add_argument('-s', '--sender', required=True,
41 help='The value of the From: header (required)')
42 parser.add_argument('-r', '--recipient', required=True,
43 action='append', metavar='RECIPIENT',
44 default=[], dest='recipients',
45 help='A To: header value (at least one required)')
46 args = parser.parse_args()
47 directory = args.directory
Georg Brandl116aa622007-08-15 14:28:22 +000048 if not directory:
49 directory = '.'
50 # Create the enclosing (outer) message
51 outer = MIMEMultipart()
52 outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030053 outer['To'] = COMMASPACE.join(args.recipients)
54 outer['From'] = args.sender
Georg Brandl116aa622007-08-15 14:28:22 +000055 outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
56
57 for filename in os.listdir(directory):
58 path = os.path.join(directory, filename)
59 if not os.path.isfile(path):
60 continue
61 # Guess the content type based on the file's extension. Encoding
62 # will be ignored, although we should check for simple things like
63 # gzip'd or compressed files.
64 ctype, encoding = mimetypes.guess_type(path)
65 if ctype is None or encoding is not None:
66 # No guess could be made, or the file is encoded (compressed), so
67 # use a generic bag-of-bits type.
68 ctype = 'application/octet-stream'
69 maintype, subtype = ctype.split('/', 1)
70 if maintype == 'text':
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030071 with open(path) as fp:
72 # Note: we should handle calculating the charset
73 msg = MIMEText(fp.read(), _subtype=subtype)
Georg Brandl116aa622007-08-15 14:28:22 +000074 elif maintype == 'image':
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030075 with open(path, 'rb') as fp:
76 msg = MIMEImage(fp.read(), _subtype=subtype)
Georg Brandl116aa622007-08-15 14:28:22 +000077 elif maintype == 'audio':
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030078 with open(path, 'rb') as fp:
79 msg = MIMEAudio(fp.read(), _subtype=subtype)
Georg Brandl116aa622007-08-15 14:28:22 +000080 else:
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030081 with open(path, 'rb') as fp:
82 msg = MIMEBase(maintype, subtype)
83 msg.set_payload(fp.read())
Georg Brandl116aa622007-08-15 14:28:22 +000084 # Encode the payload using Base64
85 encoders.encode_base64(msg)
86 # Set the filename parameter
87 msg.add_header('Content-Disposition', 'attachment', filename=filename)
88 outer.attach(msg)
89 # Now send or store the message
90 composed = outer.as_string()
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030091 if args.output:
92 with open(args.output, 'w') as fp:
93 fp.write(composed)
Georg Brandl116aa622007-08-15 14:28:22 +000094 else:
Serhiy Storchaka992cf1d2013-10-06 11:45:25 +030095 with smtplib.SMTP('localhost') as s:
96 s.sendmail(args.sender, args.recipients, composed)
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
99if __name__ == '__main__':
100 main()