2010-03-11 18:53:45 -04:00
|
|
|
#!/usr/bin/env python3
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
"""Send the contents of a directory as a MIME message."""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import smtplib
|
|
|
|
# For guessing MIME type based on file name extension
|
|
|
|
import mimetypes
|
|
|
|
|
2013-10-06 05:45:25 -03:00
|
|
|
from argparse import ArgumentParser
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-09-07 22:15:59 -03:00
|
|
|
from email.message import EmailMessage
|
|
|
|
from email.policy import SMTP
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-10-06 05:45:25 -03:00
|
|
|
parser = ArgumentParser(description="""\
|
2007-08-15 11:28:22 -03:00
|
|
|
Send the contents of a directory as a MIME message.
|
|
|
|
Unless the -o option is given, the email is sent by forwarding to your local
|
|
|
|
SMTP server, which then does the normal delivery process. Your local machine
|
|
|
|
must be running an SMTP server.
|
|
|
|
""")
|
2013-10-06 05:45:25 -03:00
|
|
|
parser.add_argument('-d', '--directory',
|
|
|
|
help="""Mail the contents of the specified directory,
|
|
|
|
otherwise use the current directory. Only the regular
|
|
|
|
files in the directory are sent, and we don't recurse to
|
|
|
|
subdirectories.""")
|
|
|
|
parser.add_argument('-o', '--output',
|
|
|
|
metavar='FILE',
|
|
|
|
help="""Print the composed message to FILE instead of
|
|
|
|
sending the message to the SMTP server.""")
|
|
|
|
parser.add_argument('-s', '--sender', required=True,
|
|
|
|
help='The value of the From: header (required)')
|
|
|
|
parser.add_argument('-r', '--recipient', required=True,
|
|
|
|
action='append', metavar='RECIPIENT',
|
|
|
|
default=[], dest='recipients',
|
|
|
|
help='A To: header value (at least one required)')
|
|
|
|
args = parser.parse_args()
|
|
|
|
directory = args.directory
|
2007-08-15 11:28:22 -03:00
|
|
|
if not directory:
|
|
|
|
directory = '.'
|
2016-09-07 22:15:59 -03:00
|
|
|
# Create the message
|
|
|
|
msg = EmailMessage()
|
2019-11-15 05:11:48 -04:00
|
|
|
msg['Subject'] = f'Contents of directory {os.path.abspath(directory)}'
|
2016-09-07 22:15:59 -03:00
|
|
|
msg['To'] = ', '.join(args.recipients)
|
|
|
|
msg['From'] = args.sender
|
|
|
|
msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
for filename in os.listdir(directory):
|
|
|
|
path = os.path.join(directory, filename)
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
continue
|
|
|
|
# Guess the content type based on the file's extension. Encoding
|
|
|
|
# will be ignored, although we should check for simple things like
|
|
|
|
# gzip'd or compressed files.
|
|
|
|
ctype, encoding = mimetypes.guess_type(path)
|
|
|
|
if ctype is None or encoding is not None:
|
|
|
|
# No guess could be made, or the file is encoded (compressed), so
|
|
|
|
# use a generic bag-of-bits type.
|
|
|
|
ctype = 'application/octet-stream'
|
|
|
|
maintype, subtype = ctype.split('/', 1)
|
2016-09-07 22:15:59 -03:00
|
|
|
with open(path, 'rb') as fp:
|
|
|
|
msg.add_attachment(fp.read(),
|
|
|
|
maintype=maintype,
|
|
|
|
subtype=subtype,
|
|
|
|
filename=filename)
|
2007-08-15 11:28:22 -03:00
|
|
|
# Now send or store the message
|
2013-10-06 05:45:25 -03:00
|
|
|
if args.output:
|
2016-09-07 22:15:59 -03:00
|
|
|
with open(args.output, 'wb') as fp:
|
|
|
|
fp.write(msg.as_bytes(policy=SMTP))
|
2007-08-15 11:28:22 -03:00
|
|
|
else:
|
2013-10-06 05:45:25 -03:00
|
|
|
with smtplib.SMTP('localhost') as s:
|
2016-09-07 22:15:59 -03:00
|
|
|
s.send_message(msg)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|