[PYTHON] Set up a mail server using Twisted

Set up a mail server using Twisted

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.

Operating environment

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

Preparation before operation

--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

Environmental preparation

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

Start-up!

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.

Check the operation

Write a simple email sending test script in Python, Let's test sending and receiving emails.

Send an email to [email protected]

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 emails sent to [email protected]

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

Set up a mail server using Twisted
Set up a file server on Ubuntu 20.04 using Samba
Easily build a DNS server using Twisted
Set up a simple HTTPS server in Python 3
Set up a simple HTTPS server with asyncio
Set up a local server with Go-File upload-
Set up a test SMTP server in Python.
Set up a UDP server in C language
Set up a local server with Go-File download-
How to set up a local development server
Set up a simple SMTP server in Python
Send mail with mailx to a dummy SMTP server set up with python.
Set up a local web server in 30 seconds using python 3's http.server
Set up a simple local server on your Mac
Set up a Minecraft resource (Spigot) server via docker (2)
Set up a free server on AWS in 30 minutes
[Part 1] Let's set up a Minecraft server on Linux
Set up a Minecraft resource (Spigot) server via docker
[Vagrant] Set up a simple API server with python
Set up a file server using samba on ZeroPi of Friendly Arm [OS installation]
I want to set up a mock server for python-flask in seconds using swagger-codegen.
Set up a file server using samba on ZeroPi of Friendly Arm [Purchased Items]
Set up a file server using samba on ZeroPi of Friendly Arm [Personal import]
Set up a web server with CentOS7 + Anaconda + Django + Apache
Set up Ubuntu as a Linux cheat sheet and https server
Set up a node to do MNIST on ROS using Tensorflow
How to set up SVM using Optuna
Set up a server that processes multiple connections at the same time
How to set up Random forest using Optuna
Write a TCP server using the SocketServer module
How to set up Random forest using Optuna
Set up a Python development environment on Marvericks
Prepare a pseudo API server using GitHub Actions
Set up a development environment for natural language processing
Mailman delivers all mail posted from a particular mail server
Create a pseudo REST API server using GitHub Pages
Learning neural networks using Chainer-Creating a Web API server
[Python] I tried running a local server using flask
Set up a Python development environment with Sublime Text 2
When it is troublesome to set up an SMTP server locally when sending mail with Python.
How to set up a simple SMTP server that can be tested locally in Python
Set up Jetson nano
Set up a Python development environment with Visual Studio Code
SSH connection to a private server using a bastion server on EC2
Build a web API server at explosive speed using hug
Set up a Google Cloud service account key on heroku
Setting up a CentOS 7 server hosted on Alibaba Cloud ECS
Let's set up a survival prediction model for Titanic passengers
Board information acquisition Python 3 edition (A rank level up set)
Set up your own web server within your Pepper app project
Reload the server set up with gunicorn when changing the code