Create a fake mail server to test mail functions in applications

Written by - 0 comments

Published on - Listed in Linux Mail


Developing and testing applications sometimes require you to go the "non-standard" way. A good example is when you have a web-application which has a mail function. The mails must correctly be sent but remember, this is just a test of the application and you don't want your users to get spammed with test mails. 

A fake/dummy mail server comes in handy. A developer suggested MailCatcher, which looked good to me except that it's written in ruby and depends on gems which I didn't want to put on this system. At least not if I find something easier...

Then I came across Dummy-SMTP, a simple listener written in python (so no additional software to be installed on my Ubuntu test servers) just saving all the received mails as text files in a folder.
By default, Dummy-SMTP runs on port 25 which requires root privileges to start with and it also must be started from within the correct folder. I made some modifications for the following purposes:

  • Launch the listener as non-root user
  • Launch the listener from anywhere with the absolute path

In my case, I defined the listener port to be 1025 and defined an absolute path: /srv/Dummy-SMTP which I chose where to run the listener.
To install Dummy-SMTP either clone the original repository or my forked one:

cd /srv/
git clone https://github.com/Napsty/Dummy-SMTP.git
chown -R appuser:appuser /srv/Dummy-SMTP

 Once the repository was cloned, become "appuser" if you aren't already and launch the listen.py file:

appuser@app01-test:~$ /srv/Dummy-SMTP/listen.py &
[1] 32189
Running fake smtp server on port 1025

Now I adapted the Postfix installation and defined that all mails should be relayed to this fake smtp server by setting the relayhost parameter:

cat /etc/postfix/main.cf|grep relayhost
relayhost = 127.0.0.1:1025

service postfix reload

From now on a mail sent from this server should be relayed to the dummy smtp server and be just stored as a text file in /srv/Dummy-SMTP/mails. Let's try this:

echo "Dummy SMTP Test" | mailx -s "Test" recipient@example.com

mail.log shows:

app01-test postfix/pickup[30289]: 3559024A6A: uid=0 from=
app01-test postfix/cleanup[4940]: 3559024A6A: message-id=<20160607120336.3559024A6A@app01-test.local>
app01-test postfix/qmgr[30290]: 3559024A6A: from=, size=385, nrcpt=1 (queue active)
app01-test postfix/smtp[4942]: 3559024A6A: to=, relay=127.0.0.1[127.0.0.1]:1025, delay=0.03, delays=0.02/0.01/0/0, dsn=2.0.0, status=sent (250 Ok)
app01-test postfix/qmgr[30290]: 3559024A6A: removed

And let's check out the Dummy SMTP mail folder:

ls /srv/Dummy-SMTP/mails
1465301016.24.txt

cat /srv/Dummy-SMTP/mails/1465301016.24.txt
Received: by app01-test.local (Postfix, from userid 0)
    id 3559024A6A; Tue,  7 Jun 2016 14:03:36 +0200 (CEST)
Subject: Test
To:
X-Mailer: mail (GNU Mailutils 2.99.98)
Message-Id: <20160607120336.3559024A6A@n
app01-test.local>
Date: Tue,  7 Jun 2016 14:03:36 +0200 (CEST)
From: root@nzzshop-app01-test (root)

Dummy SMTP Test

Neat! It worked.

The above mentioned Ruby alternative Mailcatcher has one nice feature though: The mails can be seen by browser. Well, this isn't very complicated either. The mail folder of the dummy smtp server is a given (/srv/Dummy-SMTP/mails) so by creating a simple "Alias" on the Apache running already on this test server, I was able to display all sent mails on the browser, too:

cat /etc/apache2/conf-enabled/dummysmtp.conf
Alias /mails "/srv/Dummy-SMTP/mails/"

<directory /srv/Dummy-SMTP/mails>
    Require all granted
    Options +FollowSymLinks +Indexes
    AllowOverride All
</directory>

service apache2 reload

Nothing fancy of course, but it works:

Fake dummy smtp server showing mails in browser


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.