annotate python/example/sendmail.py @ 914:016d27ca39e4

py3
author Jeff Hammel <k0scist@gmail.com>
date Mon, 27 May 2024 16:02:17 -0700
parents eec5b7abff2b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
746
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 See:
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 - http://pymotw.com/2/smtplib/
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 - http://www.mkyong.com/python/how-do-send-email-in-python-via-smtplib/
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 - https://github.com/CognitiveNetworks/daily-reports/blob/master/send_added_tv_count.py
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 """
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import argparse
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import email.utils
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import os
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 import smtplib
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 import sys
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 from email.mime.text import MIMEText
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 __all__ = ['main']
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 class MailSender(object):
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def __init__(self, host, sender, password, port=587, type='plain'):
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 self.host = host
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 self.sender = sender
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 self.password = password
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 self.port = port
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 self.type = type
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 def __call__(self, message, *recipients, **headers):
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 assert recipients
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 # construct the message
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 msg = MIMEText(message, self.type)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 headers.setdefault('From', self.sender)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 headers.setdefault('To', ','.join(recipients))
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 for key, value in headers.items():
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 msg[key] = value
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 # connect to mail server
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 server = smtplib.SMTP(self.host, self.port)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 try:
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 server.set_debuglevel(True)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 # identify ourselves, prompting server for supported features
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 server.ehlo()
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 # If we can encrypt this session, do it
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 if server.has_extn('STARTTLS'):
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 server.starttls()
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 server.ehlo() # re-identify ourselves over TLS connection
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 # login
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 server.login(self.sender, self.password)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 # send the email
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 server.sendmail(self.sender, recipients, msg.as_string())
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 finally:
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 server.quit()
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 def main(args=sys.argv[1:]):
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 # parse command line
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 parser = argparse.ArgumentParser(description=__doc__)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 parser.add_argument('host')
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 parser.add_argument('sender')
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 parser.add_argument('password')
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 parser.add_argument('-r', '--recipients', dest='recipients',
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 nargs='+', required=True,
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 help="recipients")
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 parser.add_argument('-m', '--message', dest='message', required=True)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 parser.add_argument('--port', dest='port', type=int, default=587,
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 help="port to connect to [DEFAULT: %(default)s]")
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 options = parser.parse_args(args)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 message = options.message
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 # instantiate sender
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 sender = MailSender(options.host, options.sender, options.password, options.port)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 # send email
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 sender(message, *options.recipients)
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 if __name__ == '__main__':
eec5b7abff2b add example sendmail script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 main()