Sendmail, postfix, qmail, etc. just for testing Set the MTA and change the config file, etc. It can be difficult, but even so, I sent a garbage test email. I feel that sending it to the production environment is also strange. You can also use a mock library such as Minimock, If you are using an unsupported library, I want to use it when I'm tired.
Twisted is versatile, so you can easily set up a mail server.
The verification environment is as follows. (It's cute that the environment and software are mixed)
software | version |
---|---|
Mac OS X | 10.8 |
Python | 2.7.4 |
Twisted | 0.13.0 |
--Do you have distribute installed?
If not, follow the steps below to install it.
> curl -O http://python-distribute.org/distribute_setup.py
> sudo python distribute_setup.py
--Do you have pip installed?
If not, follow the steps below to install it.
> sudo easy_install pip
--Are you installing virtualenv?
If not, follow the steps below to install.
> pip install virtualenv
It's also a reason to install Twisted globally, Let's create a sandbox environment with the following feeling.
> mkdir -p ~/Sandbox/TwistedMail
> cd ~/Sandbox/TwistedMail
> virtualenv python
> source python/bin/activate
> pip install twisted
Ends below
> twistd mail --maildirdbmdomain=example.com=/tmp/example.com --user=futoase=mogemoge
As a mail server for example.com and User is futoase, password is mogemoge It is in the form of settings. It's easy.
By default, the SMTP port is 8025 and the POP3 port is 8110. If it collides with the existing MTA process, I think the port should remain as it is.
Write a simple email sending test script in Python, Let's test sending and receiving emails.
Write a script that sends Hello world to [email protected].
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
server = smtplib.SMTP('localhost', 8025)
server.set_debuglevel(1)
server.sendmail('futoase@localhost', '[email protected]', 'Hello world')
server.quit()
When you execute the script, RCPT command etc. Send to the other party.
send: 'ehlo matsuzakipc.local\r\n'
reply: '500 Command not implemented\r\n'
reply: retcode (500); Msg: Command not implemented
send: 'helo matsuzakipc.local\r\n'
reply: '250 matsuzakipc.local Hello 127.0.0.1, nice to meet you\r\n'
reply: retcode (250); Msg: matsuzakipc.local Hello 127.0.0.1, nice to meet you
send: 'mail FROM:<futoase@localhost>\r\n'
reply: '250 Sender address accepted\r\n'
reply: retcode (250); Msg: Sender address accepted
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 Recipient address accepted\r\n'
reply: retcode (250); Msg: Recipient address accepted
send: 'data\r\n'
reply: '354 Continue\r\n'
reply: retcode (354); Msg: Continue
data: (354, 'Continue')
send: 'Hello world\r\n.\r\n'
reply: '250 Delivery in progress\r\n'
reply: retcode (250); Msg: Delivery in progress
data: (250, 'Delivery in progress')
send: 'quit\r\n'
reply: '221 See you later\r\n'
reply: retcode (221); Msg: See you later
Receive Hello world as a user [email protected] Write a script. (In addition, use the DELE command to delete the mail)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import poplib
client = poplib.POP3('localhost', 8110)
client.user('[email protected]')
client.pass_('mogemoge')
num_messages = len(client.list()[1])
for i in range(num_messages):
for j in client.retr(i+1)[1]:
print(j)
for i in range(num_messages):
client.dele(i)
When you execute the script to get the mail using POP3, The contents of the script created by the sender are displayed.
Delivered-To: [email protected]
Received: from matsuzakipc.local ([127.0.0.1] helo=matsuzakipc.local)
by matsuzakipc.local with esmtp ([twisted, version 13.0.0])
for <[email protected]>; Tue, 30 Apr 2013 13:23:12 +0900
Hello world
Without using the mock library If you want to set up a test mail server for the time being Twisted is easy. You don't have to think about anything.
It also supports overSSL. Twisted feels invincible.
Recommended Posts