Search code examples
coldfusionsmtpcoldfusion-9application.cfm

Switch between mail servers in Application.cfm


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...


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#">