-
-
Notifications
You must be signed in to change notification settings - Fork 640
/
debug.py
executable file
·41 lines (33 loc) · 1.18 KB
/
debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter13/debug.py
import sys, smtplib, socket
message_template = """To: {}
From: {}
Subject: Test Message from simple.py
Hello,
This is a test message sent to you from the debug.py program
in Foundations of Python Network Programming.
"""
def main():
if len(sys.argv) < 4:
name = sys.argv[0]
print("usage: {} server fromaddr toaddr [toaddr...]".format(name))
sys.exit(2)
server, fromaddr, toaddrs = sys.argv[1], sys.argv[2], sys.argv[3:]
message = message_template.format(', '.join(toaddrs), fromaddr)
try:
connection = smtplib.SMTP(server)
connection.set_debuglevel(1)
connection.sendmail(fromaddr, toaddrs, message)
except (socket.gaierror, socket.error, socket.herror,
smtplib.SMTPException) as e:
print("Your message may not have been sent!")
print(e)
sys.exit(1)
else:
s = '' if len(toaddrs) == 1 else 's'
print("Message sent to {} recipient{}".format(len(toaddrs), s))
connection.quit()
if __name__ == '__main__':
main()