ยปCore Development>Code coverage>Doc/includes/email-dir.py

Python code coverage for Doc/includes/email-dir.py

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