Solved

Coldfusion - notify administrator via email on error

Posted on 2011-02-24
12
710 Views
Last Modified: 2012-05-11
Is it possible to be notified by an email whenenver a coldfusion error occurs ... specifically when there is an email error ... EG: an undeliverable email? ... but also if an error occurs on a page.  I've searched through the administrator and google, but cannot find out how to do this ... if it is even possible.

Max
0
Comment
Question by:MaxwellTurner
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 15

Accepted Solution

by:
Gurpreet Singh Randhawa earned 300 total points
ID: 34973296
If you are using Application.cfc you can use the the Following

<cffunction name="onError" returnType="void" output="true">
      <cfargument name="exception" required="true">
      <cfargument name="eventname" type="string" required="true">
      <cfset var errortext = "">

      <cflog file="myapperrorlog" text="#arguments.exception.message#">
      
      <cfsavecontent variable="errortext">
      <cfoutput>
      An error occurred: http://#cgi.server_name##cgi.script_name#?#cgi.query_string#<br />
      Time: #dateFormat(now(), "short")# #timeFormat(now(), "short")#<br />
      
      <cfdump var="#arguments.exception#" label="Error">
      <cfdump var="#form#" label="Form">
      <cfdump var="#url#" label="URL">
      
      </cfoutput>
      </cfsavecontent>
      
      <cfmail to="webster@urdomain.com" from="webster@urdomain.com" subject="Error: #arguments.exception.message#" type="html">
            #errortext#
      </cfmail>
      
      <cflocation url="error.cfm">
      
</cffunction>

The Error.cfm page, you can beautify this and show a custom message to the use that error has occurred and will be fixed asap and you will be notified whenever the error comes up

The above will handle all kinds of error in CF

if you a mailing system and you want to find such error and email you
in that case you can use the cftry cfcatch block and use the cfcatch.detail or cfcatch.message to catch the error and redirect the user to another Page!
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 34973587
I am not using Application.cfc but I will look into this ... I do know abit about cftry/cfcatch  ... do I just place my cfmail block inside the try & catch tags ... ?

[cftry]
      [cfmail] ...[/cfmail] - to user
      [cfcatch]
          [cfmail] ... [/cfmail] - to administrator
      [/cfcatch]
[/cftry]

Max
0
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 34973658
Yes the way you defined, You have to do like that, and everything will work according to you

You can also use the cflog atg inside the cfcatch statement to log all the rrors in a text fiel or you can use the DB to add the entries in the table and later check on that entries
0
 

Expert Comment

by:AltControl
ID: 34973680
How to get rid of EE's junk mail? I tried to get rid of my account but there's no delete button. So I changed my e-mail to Experts-Sexchange@mailinator.com, only to find they don't send mail there. So I used a throw away Gmail account instead. So now I am relieved of their shit, I am going to send them some of mine!
0
 
LVL 4

Assisted Solution

by:Hammo777
Hammo777 earned 200 total points
ID: 34974068
Most of the email errors I've seen fail silently.  Email goes to the spool folder and if they fail (bad email address) then they go to the undeliverable folder.  We check our undeliverable folder each day.  I believe you would have to write some code to check the undeliverable folder and notify you when a file appears there.  Sometimes email fails because the SMTP server is unavailable.  In that instance I have seen people write code that watches the undeliverable folder and moves mail back to the spool folder so a second attempt can be made to send it.
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 34974618
@AltControl ... wtf??
@Hammo777 ... hmmm, that made me think ... I can import my mailsent log into a dbase a few times a day via windows scheduled task and then run a cf scheduled task to run a page periodically that will email me if new entries have been added.
@myselfrandhawa ... hmmm that also made think ... can I wrap my entire page in a cftry\catch to catch the other non-email errors (so I don't have to use Application.cfc)?  

Max
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Expert Comment

by:_agx_
ID: 34975173
no points..

can I wrap my entire page in a cftry\catch.... (so I don't have to use Application.cfc)?  

You could, but why not use an Application.cfc? Then all your error handling code would be one place
rather than scattered across different pages.

to catch the other non-email errors
As @Hammo777 said you can't catch email errors with cftry/cfcatch anyway.  Mail is spooled and sent by a different thread. So 99% of problems won't be reported to your cfm page, meaning you can't use cftry/cfcatch on them.
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 34975296
@aqx ... I see the sense in that.  I just don't know much about Application, but I'll have to start learning ... I wasn't 100% sure if 'silent errors' meant that cftry/cfcatch would not catch the error - thanks for confirming that..

Thanks to everyone.  I think I know what I have to do now.

Max
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34976076
Setting one up for error handling only shouldn't be too hard. Here's a good intro
http://www.coldfusionjedi.com/index.cfm/2007/11/9/Applicationcfc-Methods-and-Example-Uses

0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 34977356
Just realized that Application.cfc was introduced in v.7 ... I am working with v6.1  and Application.cfm ... does that matter for my purpose?  

@aqx ... thanks for the resource, but I'm at the stage where I am not really sure what Application.xxx is and how/where to use it on my site.,  I wiill try find a beginner to application.cfm, but if you have any suggested reading, I'm all ears.  

Max
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34977568
Then you can't use Application.cfc / onError then.  You'd have to use an Application.cfm file instead.

The basics are you create a file named "Application.cfm" and stick in the root folder of your application.  It'll apply to any cfm file in that folder AND any of it's subfolders.  If an error occurs in any of those cfm pages, the error handler in your Application.cfm file will handle it.

Here's a simple example from Ray Camden's site. It's probably where myselfrandhawa got his example ;-)
http://www.coldfusionjedi.com/index.cfm/2007/12/5/The-Complete-Guide-to-Adding-Error-Handling-to-Your-ColdFusion-Application

FILE: Application.cfm
------------------------
<cfapplication name="rayrules">
<cferror type="exception" template="MyErrorHandler.cfm">	

FILE: MyErrorHandler.cfm  (place in same directory as "Application.cfm")
--------------------------
<!--- 
     display a user friendly message first
--->
<h1>My Error Handler</h1>
We are so sorry. Something went wrong. We are working on it now.

<!--- 
     next capture details about the error 
--->
<cfsavecontent variable="errortext">
	<cfoutput>
	An error occurred: http://#cgi.server_name##cgi.script_name#?#cgi.query_string#<br />
	Time: #dateFormat(now(), "short")# #timeFormat(now(), "short")#<br />
	
	<cfdump var="#error#" label="Error">
	<cfdump var="#form#" label="Form">
	<cfdump var="#url#" label="URL">
	
	</cfoutput>
</cfsavecontent>

<!--- 
     finally email yourself the error details
--->
<cfmail to="youremail@yourcompany.com" from="errors@yourcompany.com" subject="Error: #error.message#" type="html">
	#errortext#
</cfmail>

File:   test.cfm    (creates an error to test it out ...)
---------------------
<cfset a = 1>
<cfset b = a / 0>

Open in new window

     

0
 
LVL 52

Expert Comment

by:_agx_
ID: 34977577
if you have any suggested reading, I'm all ears.

Also check the definitive source of course. Not sure if the 6.1 docs are still on line, but here's v8
http://livedocs.adobe.com/coldfusion/8/Errors_01.html
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now