The server receives the messages from the clients and logs them to the console.
Listing 5.9a gives the code for the SMTP server as follows:
#!/usr/bin/env python # Python Network Programming Cookbook, Second Edition -- Chapter - 5 # This program is optimized for Python 2.7.12 and Python 3.5.2. # It may run on any other version with/without modifications. import smtplib import email.utils import argparse from email.mime.text import MIMEText def mail_client(host, port, fromAddress, toAddress, subject, body): msg = MIMEText(body) msg['To'] = email.utils.formataddr(('Recipient', toAddress)) msg['From'] = email.utils.formataddr(('Author', fromAddress)) msg['Subject'] = subject server = smtplib.SMTP(host, port) server.set_debuglevel(True) try: server.sendmail(fromAddress, toAddress, msg.as_string()) finally: server.quit() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Mail Server Example') parser.add_argument('--host', action="store",
dest="host", type=str, required=True) parser.add_argument('--port', action="store",
dest="port", type=int, required=True) parser.add_argument('--fromAddress', action="store",
dest="fromAddress", type=str, required=True) parser.add_argument('--toAddress', action="store",
dest="toAddress", type=str, required=True) parser.add_argument('--subject', action="store",
dest="subject", type=str, required=True) parser.add_argument('--body', action="store",
dest="body", type=str, required=True) given_args = parser.parse_args() mail_client(given_args.host, given_args.port,
given_args.fromAddress, given_args.toAddress,
given_args.subject, given_args.body)
Listing 5.9b gives the code for the SMTP client as follows:
#!/usr/bin/env python # Python Network Programming Cookbook, Second Edition -- Chapter - 5 # This program is optimized for Python 2.7.12 and Python 3.5.2. # It may run on any other version with/without modifications. import smtpd import asyncore import argparse class CustomSMTPServer(smtpd.SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data): print ('Message Received from:', peer) print ('From:', mailfrom) print ('To :', rcpttos) print ('Message :', data) return if __name__ == '__main__': parser = argparse.ArgumentParser(description='Mail Server Example') parser.add_argument('--host', action="store",
dest="host", type=str, required=True) parser.add_argument('--port', action="store",
dest="port", type=int, required=True) given_args = parser.parse_args() server = CustomSMTPServer((given_args.host, given_args.port), None) asyncore.loop()
You may run the server and client code. The server handles the emails from the clients, as follows:
$ python 15_9a_mail_server.py --host='127.0.0.1' --port=1025 Message Received from the peer: ('127.0.0.1', 47916) Addressed from: [email protected] Addressed to : ['[email protected]'] Message : Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit To: Recipient <[email protected]> From: Author <[email protected]> Subject: Hi, Hello $ python 15_9b_mail_client.py --host='127.0.0.1' --port=1025 [email protected] [email protected] --subject="Hi, Hello" --body="Good to see you all. Keep in touch. Take Care" send: 'ehlo [127.0.1.1] ' reply: b'250-llovizna ' reply: b'250-SIZE 33554432 ' reply: b'250 HELP ' reply: retcode (250); Msg: b'llovizna SIZE 33554432 HELP' send: 'mail FROM:<[email protected]> size=232 ' reply: b'250 OK ' reply: retcode (250); Msg: b'OK' send: 'rcpt TO:<[email protected]> ' reply: b'250 OK ' reply: retcode (250); Msg: b'OK' send: 'data ' reply: b'354 End data with <CR><LF>.<CR><LF> ' reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>' data: (354, b'End data with <CR><LF>.<CR><LF>') send: b'Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit To: Recipient <[email protected]> From: Author <[email protected]> Subject: Hi, Hello Good to see you all. Keep in touch. Take Care . ' reply: b'250 OK ' reply: retcode (250); Msg: b'OK' data: (250, b'OK') send: 'quit ' reply: b'221 Bye ' reply: retcode (221); Msg: b'Bye'
The output is depicted by the following screenshot:
SMTP Server and Client