Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | |
| 3 | """Send the contents of a directory as a MIME message.""" |
| 4 | |
| 5 | import os |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | import smtplib |
| 7 | # For guessing MIME type based on file name extension |
| 8 | import mimetypes |
| 9 | |
Serhiy Storchaka | 992cf1d | 2013-10-06 11:45:25 +0300 | [diff] [blame] | 10 | from argparse import ArgumentParser |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 12 | from email.message import EmailMessage |
| 13 | from email.policy import SMTP |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def main(): |
Serhiy Storchaka | 992cf1d | 2013-10-06 11:45:25 +0300 | [diff] [blame] | 17 | parser = ArgumentParser(description="""\ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | Send the contents of a directory as a MIME message. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | Unless the -o option is given, the email is sent by forwarding to your local |
| 20 | SMTP server, which then does the normal delivery process. Your local machine |
| 21 | must be running an SMTP server. |
| 22 | """) |
Serhiy Storchaka | 992cf1d | 2013-10-06 11:45:25 +0300 | [diff] [blame] | 23 | parser.add_argument('-d', '--directory', |
| 24 | help="""Mail the contents of the specified directory, |
| 25 | otherwise use the current directory. Only the regular |
| 26 | files in the directory are sent, and we don't recurse to |
| 27 | subdirectories.""") |
| 28 | parser.add_argument('-o', '--output', |
| 29 | metavar='FILE', |
| 30 | help="""Print the composed message to FILE instead of |
| 31 | sending the message to the SMTP server.""") |
| 32 | parser.add_argument('-s', '--sender', required=True, |
| 33 | help='The value of the From: header (required)') |
| 34 | parser.add_argument('-r', '--recipient', required=True, |
| 35 | action='append', metavar='RECIPIENT', |
| 36 | default=[], dest='recipients', |
| 37 | help='A To: header value (at least one required)') |
| 38 | args = parser.parse_args() |
| 39 | directory = args.directory |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | if not directory: |
| 41 | directory = '.' |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 42 | # Create the message |
| 43 | msg = EmailMessage() |
| 44 | msg['Subject'] = 'Contents of directory %s' % os.path.abspath(directory) |
| 45 | msg['To'] = ', '.join(args.recipients) |
| 46 | msg['From'] = args.sender |
| 47 | msg.preamble = 'You will not see this in a MIME-aware mail reader.\n' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
| 49 | for filename in os.listdir(directory): |
| 50 | path = os.path.join(directory, filename) |
| 51 | if not os.path.isfile(path): |
| 52 | continue |
| 53 | # Guess the content type based on the file's extension. Encoding |
| 54 | # will be ignored, although we should check for simple things like |
| 55 | # gzip'd or compressed files. |
| 56 | ctype, encoding = mimetypes.guess_type(path) |
| 57 | if ctype is None or encoding is not None: |
| 58 | # No guess could be made, or the file is encoded (compressed), so |
| 59 | # use a generic bag-of-bits type. |
| 60 | ctype = 'application/octet-stream' |
| 61 | maintype, subtype = ctype.split('/', 1) |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 62 | with open(path, 'rb') as fp: |
| 63 | msg.add_attachment(fp.read(), |
| 64 | maintype=maintype, |
| 65 | subtype=subtype, |
| 66 | filename=filename) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | # Now send or store the message |
Serhiy Storchaka | 992cf1d | 2013-10-06 11:45:25 +0300 | [diff] [blame] | 68 | if args.output: |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 69 | with open(args.output, 'wb') as fp: |
| 70 | fp.write(msg.as_bytes(policy=SMTP)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | else: |
Serhiy Storchaka | 992cf1d | 2013-10-06 11:45:25 +0300 | [diff] [blame] | 72 | with smtplib.SMTP('localhost') as s: |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 73 | s.send_message(msg) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | if __name__ == '__main__': |
| 77 | main() |