Using hMailServer to test emails sent from application

970 views Asked by At

I have a Java webapp that sends out email notifications to client-users in a bunch of different scenarios. The content, schedule, etc. of the mails is determined by the admin-users of the system.

I've pointed the app to a local hMailServer instance in my test environment, now I want to configure this so that all emails it receives are forwarded to a set of email addresses and blocked from being sent to the real recipients.

Since I'm setting this up so that admin-users can test out their emails before we move them into production, it would be good if I could avoid having to rewrite any of the email headers (that way the users can see that the email would have been sent to the right people).

1

There are 1 answers

0
Sam On

I started by adding a script which changes the subject of the message, this gets called in my first rule.

Sub EditSubject(oMessage)
    oMessage.subject = "[TEST MESSAGE - IGNORE] " & oMessage.subject
    oMessage.save
End Sub

This makes it easier to write the conditions for rules later, and if an email does get sent it will at least say that it's a test.

Then I set up a Route which targets the actual company mail server (just referenced as SMTPSERVER). I've got this set up to only send to specific addresses, just in case anything gets past the rules.
When I first set this up I just set the route with the domain company.com, but it seems like this was getting triggered before my rules and causing issues. So in the end I just called it bounce.

Next I added the following rules:

  1. Edit and Forward
    This has a condition that checks the email subject doesn't contain my [TEST MESSAGE - IGNORE] string, this makes sure we don't trigger this rule on the new forwarded messages.
    Then triggers the following actions:

    • Run function to call the EditSubject script above
    • Forward email (repeated as many times as you like with each address you want to target),
    • Delete email
    • Stop rule processing
      These last 2 just stop anything further happening to the original message.
  2. Reroute
    This has a condition that checks the email subject DOES contain [TEST MESSAGE - IGNORE], so we only run this rule on the forwarded messages.
    It triggers the Send using route action and targets the bounce route.

The end result is that whenever one of my admin-users sets up a new email on the app in test it will be sent to this hMailServer relay.
The mail server edits the subject to include the "test" prefix, creates copies in its queue to forward on to my set recipients, and then drops the original.
The new copies are picked up and passed on (via the route) to the actual company mail server and delivered.

The emails you get out at the end display in Outlook with the To, CC, etc. all showing the original recipients.