When developing code to dynamically generate email content for a list of users things become somewhat difficult in terms of testing. We need to be able see if the content we are going to send to a particular user is correct for that specific user as well as displaying correctly. In addition we have to run this test without spamming our actual users.
There are many approaches for handling this, but really our choices boil down to three options:
This is the easiest to setup and execute but it requires you to have setup multiple email accounts, one for each possible combination of dynamic content. If you control your email server this is not too bad. If though you need to talk to another department (IT) then its not so straight forward.
Option 2 requires less setup and allows you to run against your entire email list. Simply modify your code to always send to a test account when your app is in test mode. The disadvantage to this is that you must modify the content being generated to include a test data section so you can view who the email would have gone to.
I have used this technique a few times myself and if you are careful it can work. However it introduces the possibility that the layout may not be displayed correctly as you are viewing a modified version (showing testing data) and not a true view of what the layout will actually display as.
This is my preferred method of testing email generating code. How it works is that we setup a test email server that acts like a normal email server as far as our code knows but when it sends an email it does not obey the specified to,cc & bcc fields but instead sends it to a single test account or test interface.
Then when we want to test the email we have the emailing code select the test mail server instead of the normal one.
If like me you use eclipse there is a really good free plugin called "Mail Snag" which you can get on the eclipse marketplace. Install it in eclipse, turn it on and tell your application to use the email server on localhost.
When your application sends an email Mail Snag will accept the email request but instead of sending it, it will instead display it in a panel inside of eclipse. This is great because it means you don't have to worry about filling up your email inbox with a bunch of test emails and you can see the emails as the would have been sent from inside eclipse.
Note: This is all assuming you are testing your code on localhost.
In my applications I configure a mode flag which is set when the application is started depending on the server address. In production mode I set the mail server details to the normal server, while I use the "mail snag" details in development mode.
Then when sending email I simply use this code.
{dynamically determined content}
If you do not use eclipse or for some reason you cannot run your code in a local test environment, then you can use a standalone test mail server. I have compiled a short list below. While I have not used it, I am particularly intrigued by "Mail Trap". If anyone has used it I would love to hear about your experiences with it.
I would like to take a moment say something on local vs remote development.
If you are not developing and doing initial testing on a local environment then you are really limiting yourself in terms of the tools available to you. I know it seems easier to use a single development server for all your developers, but once you setup and gain experience with local per developer environments your productivity goes up significantly.
Note: I have only used mailsnag personally. This list is based on reviews I have found on other websites and I cannot speak to how good these others tools are. Use at your own risk.