Skip to content


Repository files navigation



This is an intelligent email-based agent server, written in Python, using state machine logic to handle the SMTP protocol.

The server works by responding to email sent to designated inboxes with canned, dynamic, or custom replies.

Among the included examples, email sent to nyc-weather@ will be automatically replied with the current weather forecast for NYC, using data from the National Weather Service website.

Email sent to just weather@ will return a report based on the sender's physical location, automatically determined from the email headers.

With the exception of the latter, more "intelligent" weather responder example, the basic examples do not take account the sender, subject, or email message text, but the server can be extended with natural language processing and other AI techniques to provide more customized auto-replies.

This code is loosely based on Paul Tyma's [Mailinator] ( in that it does not support the full range of SMTP commands, and instead uses ip traps and timeouts to foil spammers and other scripts or bots, continuing the work described here.

It also forms the basis for the server used by the [] ( and web services.


Clone this repo, and make sure you have pycurl and lxml installed (using pip is recommended):

git clone
sudo pip install pycurl
sudo pip install lxml

Optionally install libmagic for python since it is helpful for identifying mime types found in email messages (though this code will run without it):

sudo apt-get install python-magic

Important: there is a suble but important difference between the package available via pip versus what apt installs. The code in depends on the latter.

Getting Started

Note that this server listens to incoming requests, but it does NOT provide a way to send outbound emails, so make sure you have a outgoing mail server such as Postfix installed and running.

  1. Edit the file in this folder and redefine these variables with your server's domain instead of
domain_logo         = ""
server_auto_email   = ""
smtp_server_domain  = ""
pass_through_target = ""

Ideally, the pass_through_target email address resides on a different server than the one running this code.

You can also define a file with environment-specific configuration settings which is exempt from source control (this is basically the django local settings approach applied to this project).

  1. Edit the action_mailboxes dict in the file.

Each key is the name of the inbox you want the server to auto-respond to, and the value is the name of the module.class which is instantiated to carry out the request.

Each class needs to be implemented or referenced in the agents folder.

See the notes there for the class and functional prototype requirements, and also look at the time and weather response examples.

  1. (optional) Edit the pass_through_mailboxes list in the file.

These are the inboxes which the server ignores, and just passes through to the email address defined by the pass_through_target variable.

  1. (optional) For logging, set the smtp_server_debug variable to True

This will record the ip address, timestamp, and all commands up to DATA sent to the server by each client.

  1. Redirect port 25 (smtp) traffic

a. IPv4 servers

Run the script as root or sudo:

sh ./

The default port for redirecting incoming traffic is 8888, but you can change that as necessary.

Optionally, create an /etc/iptables.up.rules file, so that the redirect takes effect on reboots:

iptables-save > /etc/iptables.up.rules
vi /etc/network/if-pre-up.d/iptables
chmod +x /etc/network/if-pre-up.d/iptables

Where the /etc/network/if-pre-up.d/iptables file contains:

/sbin/iptables-restore < /etc/iptables.up.rules

For more details on this option, read: or

You can always undo the iptables setting by running the script as root/sudo (remember to remove or edit /etc/iptables.up.rules as well, if you chose that option initially).

b. IPv6 servers

If the server you are running this software also accepts IPv6 traffic, then you will need to do the following steps as well, since some email senders, notably Google's gmail, use IPv6 if available.

Run the script as root or sudo:

sh ./

As with, the default port for redirecting incoming traffic is 8888, but you can change that as necessary.

Note: If you change the port in it must be the same as the one in the script.

As as before, create an /etc/ip6tables.up.rules file, so that the redirect takes effect on reboots:

ip6tables-save > /etc/ip6tables.up.rules
vi /etc/network/if-pre-up.d/iptables

And update the /etc/network/if-pre-up.d/iptables file to contain a third line, like so:

/sbin/iptables-restore < /etc/iptables.up.rules
/sbin/ip6tables-restore < /etc/ip6tables.up.rules
  1. Edit the file.

By default, the server runs as user daemon, but if your auto-responder agents need access to different resources/user groups, just change the user defined at the end of this line:

su -c "export PYTHONPATH=$PYTHONPATH:$smtps:$smtps/server:$smtps/utils:$smtps/agents; python -c 'import smtp_server; smtp_server.start()' > /tmp/smtp_server_$logfile.log 2>&1" daemon

The script is designed to restart in case of an error, and if you'd like to get the log by email when that happens, change the target email address in this line:

mail -s "SMTP server error!" < /tmp/smtp_server_$logfile.log

Note that logfiles do not over-write each other, so you can remove that line altogether, if you'd rather not get the alert.

  1. Start the server as root/sudo using the file:
sh ./

If you'll be running it long-term, it's best to kick this off inside a screen session, from which you can detach.



This is an intelligent email-based agent server







No releases published


No packages published