Based on article How to send e-mails with Active FoxPro Pages I would like to show how to use mail functionality to drop a message in case of an error in your Web application.
Notes:
If you're not familiar with AFP and mail, please read the introduction. Thanks.
Okay, what do we need:
- AFP Engine - checked
- Mail component - checked
- AFP application - checked
- Event_Error - wow, that's new...
That's our 'malefactor' - AFP event called Event_Error. This event is called due to an occuring error message while executing an AFP document. Normally the event is empty or not used cause standard behaviour of AFP should run.
And that's the best place to code a small hook to write our own error handler...
Okay, not really a new error handler but we are extending the existing one to automagically drop a mail:
Procedure Event_Error
Local lcRecipient, lcSubject, lcBody
Store "" To lcRecipient, lcSubject, lcBody
*// Define error variables
lcRecipient = "joki@afpfaq.de"
lcSubject = "AFP FAQ - Fehlermeldung " + Transform(Datetime())
lcBody = Error.ErrorHTML()
* Send mail... :-)
SendMail(m.lcRecipient, m.lcSubject, m.lcBody)
EndProc
Bingo!
That's all, really! Save your application, restart the AFP threads and enjoy. Next time an error occurs a mail will lose one's way in your mailbox.
But this is not the end of our journey... maybe
If you're already using the HTML-Plugin then use it here as well:
Procedure Event_Error
Local lcRecipient, lcSubject, lcBody
Store "" To lcRecipient, lcSubject, lcBody
*// Define error variables
lcRecipient = "joki@afpfaq.de"
lcSubject = "AFP FAQ - Fehlermeldung " + Transform(Datetime())
lcBody = Error.ErrorHTML()
*// Strip any obsolete HTML elements
lcBody = Html.InnerText(m.lcBody)
*// Send mail... :-)
SendMail(m.lcRecipient, m.lcSubject, m.lcBody)
EndProc
If you are not using the HTML plugin... Think about it ;-)
And now enjoy the definitively smaller text-based mails instead of those huge HTML-blown messages. Uhm, okay, only if the error message doesn't mean too much disaster.
In theory, it's possible to create an error mail / message for every single AFP document - even to abandon an AFP Web application is okay - but does it make sense?
Alternative configurations are left to your phantasies.
If have any questions or criticsm on this article, drop a mail at faq@afpfaq.de.
Enjoy the AFP FAQ, JoKi