testingsmtpintegration-testinghmail-server

Sandbox/catch (catchall) mails sent from test environment


I'd like to be able to test if my automatically sent mails are really sent, but I do not want these mails to leak to real world. Is there a way to configure my mail settings so that all mails sent from my test environment are automatically forwarded to a single mailbox?

I suppose I can configure addresses in my configuration, but if I replace every e-mail in my system with my test e-mail, I won't be able to clearly check if mails are really created correctly.

While testing, I might enter real world e-mail addresses, but I do not want to risk mails being sent to customers.


Solution

  • One way to safely isolate your test environment is to configure a mailserver that catches all mails from and to all domains, and puts them in one big inbox. If I configure that mailserver in my test environment, all mails are cought.

    Many mailservers support this scenario. I used an open source one, hmailserver on an Azure VM. Installation steps:

    1. Create an Azure VM. I used a standard DS1 v2, but a lighter version probably also suffices. I chose Windows Server 2016. Again, many others also work.
    2. Give your machine a DNS name. e.g. anymail.yourregion.cloudapp.azure.com
    3. In the portal, open ports 25, 143 and 578 for TCP
    4. Connect through remote destop
    5. On the machine itself, also open ports 25, 143 and 587 in firewall settings
    6. Enable .net 3.5. I did this through Programs and Features => Turn Windows Features on and off.
    7. Download hmailserver to your VM D: drive (this is temporary space). You need to add the hmailserver.com-domain to your trusted sites to download it.
    8. Install it using default settings. Then configure it:

    9. Create a domain anymail

    10. On the domain tab advanced configure the cathchall-address: anymail@anymail
    11. Create an account in your domain: anymail@anymail and choose an easy password.
    12. Create a rule: delete all mail from mailer-daemon@anymail.yourregion.cloudapp.azure.com
    13. In settings - SMTP enter anymail.yourregion.cloudapp.azure.com as local hostname. Set number of retries and minutes between each retry to 0. Set the remote host name to localhost port 25 (so you really cannot mail out)
    14. In anti-spam whitelist 0.0.0.0 through 255.255.255.255
    15. In advanced - mirror enter anymail@anymail
    16. In advanced - IP ranges delete localhost
    17. In advanced - IP ranges double click internet and remove all checkboxes before 'requere SMTP authentication"
    18. IN advanced - Autoban disable autoban.

    Everything should work now. You should be able to configure anymail.yourregion.cloudapp.azure.com in your configuration, but also in your mail program (e.g. outlook). You can check that you can send mails from any sender to any recepient. Test that mails to your regular e-mail address don't arrive in your regular mailbox.

    DIAGNOSTICS

    While tweaking settings I often looked at status -> delivery queue. This queue is not automatically refreshed, so you need to refresh it manually. You may need to clear it while testing. (I often got into loops, resulting in many many messages.)