Question

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="foo@bar.com" />
  <cfset email.password="s3cr3t" />
<cfelse>
  <cfset email.username="baz@bar.com" />
  <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...

Was it helpful?

Solution

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 = "foo@bar.com";
          application.email.password = "s3cr3t";
        } else {
          application.email.username = "foo@ffooodoijdbar.com";
          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#">

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top