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