A fake SMTP server setup for testing.

A fake SMTP server setup for testing.

What is a fake SMTP server?

A fake SMTP server accepts SMTP messages but does not transmit emails to external recipients. We use it for development and testing purposes.

Why should we test our emails?

We must test emails to overcome email-related issues like password reset, email links not working, etc.

Why should we set up a fake STMP server for email testing?

Using a dedicated SMTP server is more reliable as we use the same code paths when testing emails using a fake SMTP server to avoid unanticipated problems. How can Gmail as your development email hurt deliverability? Sending the same Gmail inbox several times can impair your email deliverability. Another issue arises when utilizing Gmail addresses on a shared server for test emails.

How to set up a fake SMTP server with smtp4dev?

smtp4 is an open-source fake SMTP server. Install Docker on your machine. For Linux or Windows, download and install a smtp4dev package. We are using Docker to set up smtp4dev for a faster process. Ensure the Docker daemon is running with the command docker info and check the Server: section.

A summary is displayed:

~ % docker info ... Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 ...
Start  rnwood/smtp4dev:v3 container and set up smtp4dev.

After launching smtp4dev for the first time, an image appears. Docker will download the image from Docker Hub and reuse it:

~ % docker run --rm -it -p 3000:80 -p 2525:25 rnwood/smtp4dev:v3 
Pulling from rnwood/smtp4dev 
68ced04f60ab: Pull complete 
4ddb1a571238: Pull complete 
94b78a0446e2: Pull complete 
b48f8e1b0b06: Pull complete 
a41ea3d79519: Pull complete 
bbbe93f6aff1: Pull complete 
Digest: sha256:a821221fd4f6e8cf17b371e11d2acc2fcc4ba05125bec827abec7f821b6be9f2 
Status: Downloaded newer image for rnwood/smtp4dev:v3 

Guide on each of the Docker options:

• --rm — removes the container when service finishes
• -it — launches the container interactively, streaming its output into the terminal. • -p 3000:80 — map port 80 on a standard HTTP server port to port 3000 on your local machine. The smtp4dev console will be available on http://localhost:3000. • -p 2525:25 — map port 25 in SMTP server port to port 2525 on the machine. You can visit the application on http://localhost:3000.

Our server is running on port 2525 using smtp-cli here. You can use any SMTP client, including these libraries in your application:

~ % ./smtp-cli --verbose --server localhost --port=2525 
Connection from [::1]:60618 to [::1]:2525 
[220] '4eb77fb909ab smtp4dev ready' 
> HELO localhost [250] 'Nice to meet you' 
> QUIT [221] 'Goodbye'

It shows a working server.

Create a data.txt file with the headers and the body of the email and send: subject: Dragons Either dragons should exist entirely or fail to exist at all, he felt. A dragon only half-existing was worse than the extremes.

Send this email to our fake SMTP server:

./smtp-cli --verbose --server localhost --port=2525 --from test@example.org --to test@example.net --data data.txt 
Connection from [::1]:60750 to [::1]:2525 
[220] '4eb77fb909ab smtp4dev ready' 
> HELO localhost [250] 'Nice to meet you' 
> MAIL FROM:<test@example.org> [250] 'New message started' 
> RCPT TO:<test@example.net> [250] 'Recipient accepted' 
> DATA [354] 'End message with period' [250] 'Mail accepted' 
> QUIT [221] 'Goodbye'

After this the email shows up.

Limitations: It is challenging to use smtp4dev for continuous integration. The default setting might not accurately represent what you'll encounter in practice. Try MailSlurps fake smtp servers for a more flexible alternative to SMTP4Dev.

Ready to dive in?Start building email applications today.