Manually testing email notifications with GreenMail mock mail server
A re you sending mails featured with HTML and CSS and want see how they look before pushing your changes to production? You also do not want mails to leak from your local environment to your customer? A mock mail server like GreenMail might just be the tool you need.
Whenever developing an application I aim to get local and testing environment as close to production as possible without neglecting elementary things like privacy and security. This gives an advantage when implementing new features. You get the same user experience on your local device. This enables you to find troublesome or unclear sections.
It also lowers the barrier for manual testing, which results in more and higher quality feedback from everyone (not only developers) involved in the project.
For testing purposes you usually do not want to connect to production systems. That is why local or test environments often use local instances of additional applications like a database. This can also be applied to a mail server. When executing a local instance of a mail server you have to configure it properly to avoid leakage of data to your customers. A mock mail server like GreenMail is built to keep data on the same machine thus serves as a good option.
To use GreenMail as a mail server you should use the standalone version. Running it in a docker container is the simplest approach.
There are a few environment variables you can set to configure GreenMail. A good option is setting the logging to verbose. This way you can identify misconfigurations easily.
All of the most common mail protocols are supported. The ports have an offset of 3000 compared to the default port of the corresponding protocol. That’s it, you are now able to use GreenMail in your application.
localhost as the host and specify the port you want to connect to in your local environment’s application properties.
To test your mails locally you can either connect to a desktop client like Thunderbird or install a web client. A simple but popular web client is Roundcube. In this example running it in a docker container is also the simplest approach.
To make Roundcube use GreenMail you habe to specify certain environment variables for the Roundcube container.
You can read mails using the IMAP or POP3 protocol. You just need to specify the url and the port. Writing mails can be done using the SMTP protocol where you also have to specify the url and the proper port.
GreenMail automatically accepts all incoming mails. If there is no corresponding address, one will automatically be created. The password is equal to the mail address.
Because of this you should not make the GreenMail and Roundcube instances publicly available. If you still want to integrate it in your test or staging environment it is highly recommended to secure it with a reverse proxy like nginx or Traefik.
You can check out the whole configuration on GitHub. There is also a small example application where you can test the described behaviour in action.