Testing Email Without Spamming Your Customers

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

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:

  1. Create a small test list first and run our code against that.
  2. Modify the code that selects the target email address to always send to a single testing account.
  3. Modify the mail server to redirect outgoing email to a single testing account.

Option 1: Test List

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: Modified Targets

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.

Option 3: Modified Mail Server

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.

Example: Mail Snag

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.

How I Select The Mail Server

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.

<!--- Configure mail server settings --->
<cfif application.mode EQ "production">
      <cfset application.mailServer = {
            server="{production server address}"
            ,serverPort={production server port}
            ,useSSL=1
            ,username="{production server username}"
            ,password="{production server password}"
            ,from="{production server form}"
      }>

<cfelse>
      <!--- Local test server settings --->
      <!--- The username, password and from values can be anything you want or simply left blank --->
      <cfset application.mailServer = {
            server="127.0.0.1"
            ,serverPort=25
            ,useSSL=0
            ,username="anyname"
            ,password="anypass"
            ,from="[email protected]"
      }>

</cfif>

Then when sending email I simply use this code.

<cfmail 
      server="#application.mailServer.server#"
      username="#application.mailServer.username#"
      password="#application.mailServer.password#"
      port="#application.mailServer.serverPort#"
      useSSL="#application.mailServer.useSSL#"
      from="#application.mailServer.from#"
      to="{dynamically determined target address}"
      subject="{dynamically determined subject}">

            {dynamically determined content}
</cfmail>

Some Alternatives To Mail Snag

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.

Aside: Local vs Remote development

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.

List Of Test Email Servers

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.

Reader Comments

Dan Fredericks's Gravatar
Dan Fredericks
Tuesday, August 26, 2014 at 10:13:42 AM Coordinated Universal Time

Hey,
so i see you set up your localhost web server via code, but can you do the same thing in coldfusion admin? is there a way to create a default localhost webserver? I just added in localhost in cfadmin for the webserver address, port was 25, and left everything else as the defaults...but when i tried to run my program, it did not find a local mail server, this my code had an error...so need to set up a local mail server, or maybe set CF Admin mail up correctly...I want to see how this plugin works...any ideas? Thanks!!

dan fredericks's Gravatar
dan fredericks
Tuesday, August 26, 2014 at 10:23:24 AM Coordinated Universal Time

Hey,
i did not see how to turn on the mail logger, then i noticed the green run button on the view page in eclipse/builder, turned on, ran my code and bam the email showed up in the view window...awesome looks great.
Just typing in this post made me think, and i looked again, and found the answer.
thanks
dan

Steven Neiland's Gravatar
Steven Neiland
Thursday, September 4, 2014 at 9:13:03 PM Coordinated Universal Time

Glad you were able to figure it out Dan.

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing