Switch between mail servers in Application.cfm

547 views Asked by At

We use coldfusion 9, and have sending limits with our individual SMTP accounts that our site uses to send email. I want to alternate between two servers depending on what time of day it is.

<cfif timeformat(now(),'HH:mm:ss') GT '12:00:00' >
  <cfset email.username="[email protected]" />
  <cfset email.password="s3cr3t" />
<cfelse>
  <cfset email.username="[email protected]" />
  <cfset email.password="s3cr3t2" />
</cfif>

I would like to do this in Application.cfm (we don't use cfc) and not have to modify any cfmail tags...

2

There are 2 answers

9
Evik James On BEST ANSWER

If you want to split the volume of email between each server, I would suggest keeping a value that would tell you which server to use next:

This would go in your appplication file.

    <cfscript>
        // get this hour
        ThisHour = hour(now());
        ServerOneHours = "1,3,5,7,9,11,13,15,17,19,21,23";
        if (listFind(ServerOneHours, ThisHour) gt 0) {
          application.email.username = "[email protected]";
          application.email.password = "s3cr3t";
        } else {
          application.email.username = "[email protected]";
          application.email.password = "s3cr6516516513t";
        } 
    </cfscript>
</cffunction>

Whenever a cfmail is used, it would use the current settings:

<cfmail username="#application.email.username#" 
    password="#application.email.password#">
3
Mike Causer On

If all of your cfmail tags use authentication stored in your cf admin, you can alter the settings in your app at runtime using the ServiceFactory and MailSpooler.

var mss = createObject('java','coldfusion.server.ServiceFactory').getMailSpoolService();
mss.setServer('you-mail-server-host-or-ip');
mss.setUsername('you-mail-server-username');
mss.setPassword('you-mail-server-password');

You can get the settings back using the get() methods:

var mss = createObject('java','coldfusion.server.ServiceFactory').getMailSpoolService();
var host = mss.getServer();
var user = mss.getUsername();
var pass = mss.getPassword();

The getPassword() returns your password encrypted. Each time you set the password, it uses a new salt.

If you want to switch between servers automatically, create a scheduled task that checks your quota and switches when exceeded, or at a predefined interval.