2

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

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Kyle Macey
  • 8,074
  • 2
  • 38
  • 78
  • 1
    just use the IF logic you have and set a variable to the server you want to use, then just use that variable in the server attribute of your cfmail tag – Limey Aug 10 '12 at 16:44

2 Answers2

2

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#">
Evik James
  • 10,335
  • 18
  • 71
  • 122
  • Thanks, but I want to be able to programmatically configure the global setting. The application has hundreds of `cfmail` tags without username and password specified, so how would I configure the global setting from within Application.cfm – Kyle Macey Aug 10 '12 at 17:20
  • @KyleMacey: If you have the login and password already in the administrator, then you just need to tell it the server in the CFMail tags, CF is smart enough to get the login and password – Limey Aug 10 '12 at 17:22
  • See my edit, it covers some features of Application.cfm I'm trying to take advantage of – Kyle Macey Aug 10 '12 at 17:52
  • Sorry, I can't help you with CF9/10 specific features. – Evik James Aug 10 '12 at 17:55
  • Ok, I didn't realize it was specific to ColdFusion 9. – Kyle Macey Aug 10 '12 at 18:00
  • However, it does seem that the link I provided was specific to a cart program that document is from – Kyle Macey Aug 10 '12 at 18:01
  • Is there anything about my answer that isn't working for you? It considers time of day and chooses the appropriate server. – Evik James Aug 10 '12 at 18:29
  • Yeah, I ended up doing a big find and replace across my application for `cfmail` and used a solution similar to yours. – Kyle Macey Aug 10 '12 at 18:47
  • 1
    I am glad I could help you out a bit. – Evik James Aug 10 '12 at 19:16
1

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.

Mike Causer
  • 8,196
  • 2
  • 43
  • 63
  • This was exactly what I was looking for, but unfortunately, I already went through and changed my app to work with the other answer. Nice find, though! – Kyle Macey Aug 15 '12 at 03:35
  • Another way to handle would be through the CF Administrator API (specifically mail.cfc): http://www.cfexecute.com/admin-api-documentation/mail-cfc/ – David Faber Aug 28 '12 at 03:35
  • The CF Admin API basically wraps this functionality (and probably adds it's own bugs, knowing Adobe) – Mike Causer Dec 20 '13 at 01:58