============ Queue server ============ As shown in the general usage_ document, you can create subclasses of the Server class to define how you want to handle received messages. A very common pattern is to use a Queue to share messages between the controlling thread and the server thread. These are nice because, unlike a mailbox based server, no sorting is required to get the messages out of the server in the same order they are sent. .. _usage: usage.html First, you need a queue. >>> try: ... from queue import Empty, Queue ... except ImportError: ... # Python 2 ... from Queue import Empty, Queue >>> queue = Queue() Then you need a server. >>> from lazr.smtptest.server import QueueServer >>> server = QueueServer('localhost', 9025, queue) Finally, you need a controller. >>> from lazr.smtptest.controller import Controller >>> controller = Controller(server) Now, start the SMTP server. >>> controller.start() Send the server a bunch of messages. >>> import smtplib >>> smtpd = smtplib.SMTP() >>> code, helo = smtpd.connect('localhost', 9025) >>> print(code, str(helo)) 220 ... Python SMTP proxy version ... >>> smtpd.sendmail('iperson@example.com', ['jperson@example.com'], """\ ... From: Irie Person ... To: Jeff Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} >>> smtpd.sendmail('kperson@example.com', ['lperson@example.com'], """\ ... From: Kari Person ... To: Liam Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} >>> smtpd.sendmail('mperson@example.com', ['nperson@example.com'], """\ ... From: Mary Person ... To: Neal Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} All of these messages are available in the queue. >>> while True: ... try: ... message = queue.get_nowait() ... except Empty: ... break ... print(message['message-id']) We're done with the controller. >>> controller.stop() Queue controller ================ An even more convenient interface, is to use the QueueController. >>> from lazr.smtptest.controller import QueueController >>> controller = QueueController('localhost', 9025) >>> controller.start() >>> smtpd = smtplib.SMTP() >>> code, helo = smtpd.connect('localhost', 9025) >>> print(code, str(helo)) 220 ... Python SMTP proxy version ... We now have an SMTP server that we can send some messages to. >>> smtpd.sendmail('operson@example.com', ['pperson@example.com'], """\ ... From: Onua Person ... To: Paul Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} >>> smtpd.sendmail('qperson@example.com', ['rperson@example.com'], """\ ... From: Quay Person ... To: Raul Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} >>> smtpd.sendmail('sperson@example.com', ['tperson@example.com'], """\ ... From: Sean Person ... To: Thom Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} And we can dump out all the messages from the controller. >>> for message in controller: ... print(message['message-id']) We can send more messages and view them too. >>> smtpd.sendmail('uperson@example.com', ['vperson@example.com'], """\ ... From: Umma Person ... To: Vern Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} >>> for message in controller: ... print(message['message-id']) Resetting ========= Queue servers support a RSET (reset) method, which empties the queue. >>> smtpd.sendmail('wperson@example.com', ['xperson@example.com'], """\ ... From: Wynn Person ... To: Xerx Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} >>> smtpd.sendmail('yperson@example.com', ['zperson@example.com'], """\ ... From: Yikes Person ... To: Zell Person ... Subject: A test ... Message-ID: ... ... This is a test. ... """) {} >>> controller.queue.qsize() 2 >>> controller.reset() >>> controller.queue.qsize() 0 Clean up ======== We're done with this controller. >>> controller.stop()