Every now and then, my ColdFusion mail server breaks. I don't know why; it seems to be completely random. But, every now and then, it cannot connect to the SMTP host and it simply stops sending out emails. I typically am not aware that this has happened until I happen notice that someone has left a comment on my blog for which I never got an email alert. At that point, I'll check my mail server to find out that the mail hasn't actually worked in days and I have dozens of undelivered mail stubs.
My ColdFusion mail server stopped working again a few days ago and this time, I decided it was time to put a process in place to help me stay on top of the situation. I created a scheduled task that runs every hour and monitors the undelivered mail folder (Undelivr) on the server. When the number of items in the undelivered mail folder gets above, say 3 files, the ColdFusion server shoots me an email to let me know something is wrong.
But, if the mail server is down, I can't just send myself an email alert in the normal fashion! If I did that, my alert would end up in the undelivered mail folder along with everything else. As such, I have to use a 3rd party mail server to have ColdFusion send the alert. In this case, since ColdFusion 8+ now has SSL support for CFMail, I have decided to go with Google's GMail. I set up a GMail account for my ColdFusion server and wired up this script:
<!--- Create a directory path to the undelivered mail folder. ---> <cfset undeliveredMailDirectory = "C:\ColdFusion8\Mail\Undelivr\" /> <!--- Query for the undelivered mail items in this directory. ---> <cfdirectory name="mailStubs" action="list" directory="#undeliveredMailDirectory#" sort="dateLastModified DESC" /> <!--- Check to see how many undelivered files we have. If we have more than the given threshhold, we need to alert the server administrator that something has crapped-out in the mail server. ---> <cfif (mailStubs.recordCount gt 3)> <!--- Too many emails have failed! Something is breaking on the server - might need a restart. Send email - of course, since MAIL SERVER IS BROKEN, we cannot use the configured mail server. Rather, we have to use a 3rd party mail service; in this case, Google Mail (GMAIL). ---> <cfmail to="email@example.com" from="firstname.lastname@example.org" subject="ColdFusion Mail Server Not Working (#mailStubs.recordCount# Undelivered Messages)" type="html" server="smtp.gmail.com" username="****" password="****" port="465" usessl="true"> <h1> ColdFusion Mail Server Has Probably Failed </h1> <p> The following emails are sitting in the undelivered mail folder on the server: </p> <ul> <cfloop query="mailStubs"> <li> #mailStubs.name# - #dateFormat( mailStubs.dateLastModified, "mm/dd/yyyy" )# at #timeFormat( mailStubs.dateLastModified, "hh:mm TT" )# </li> </cfloop> </ul> <p> You might want to consider restarting the service. </p> </cfmail> </cfif> <!--- Mail sent! ---> Done.
As you can see, if there are more than 3 files in the undelivered mail folder, I am using the CFMail tag and the GMail service to send a list of undelivered files to myself.
Of course, this makes the assumption that the CFMail tag will work once the mail service is broken. But, since I don't actually know what is breaking when the mail stops working, it's definitely possible that the CFMail tag won't even work when routing through the GMail server. Who knows? I'll just have to see what happens the next time my mail server decides to give up on life.
I know that there are services that will help you monitor stuff like this (server uptime and what not); but, I figured this was, if nothing else, a chance to try using the CFMail tag with Google's GMail service.
Want to use code from this post? Check out the license.