Magento 1: Custom Error / Exception Email

Magento has a process that send the website administrator an email when an exception is thrown.  That system works just fine.  When this happens, the rendering of the remainder of the page is halted and the user will be displayed an error page.

Magento Custom Exception Email
Trigger a custom error email in Magento that emulates the exception emails without throwing the exception on the website. This allows for custom error events without disrupting the user experience.

However, what about the instance where you want to receive the same email while not interrupting the user experience.  In other-words, send the email and then continue to render the page page.  There is no default method for this built into Magento.  So continue to read below for a quick fix that allows you to implement an Exception Email without the standard exception process happening.

Note: This code is designed to worith with Magento 1.x versions.

Caution

I’ll get this out of the way from the get-go.  Implementing this could have adverse effects in that your users may not have the experience they/you expect, allowing code to continue may result in excessive server usage, etc.  Use this sparingly and ensure that you are handling errors correctly.

In my case, the situation where I’m using this is to alert me if a CURL request had failed.  The error was being handled correctly already, but aside from reading the logs I have no way of being informed that the CURL request failed.  So that’s where this new email alert comes into play.

The Code

In the code below, replace PromInc with your company name.

Generate and Send the Error

We need to extend the Core helper to add two methods:

  1. Generate a callstack to include in the email
  2. Generate and send the contents of the email

Add the following files.

File:
app/etc/modules/PromInc_Modules.xml

File:
app/code/local/PromInc/Core/etc/config.xml

File:
app/code/local/PromInc/Core/Helper/Data.php    

With these two files added, clear the cache from within the Magento Admin.

  1. System -> Cache Management
  2. Check Configuration and Layouts and click Submit.

Trigger the Exception Email

All that needs to be done now is to trigger the email to send from within your code where an error has occurred.

Parameters:

Parameter NameDefault ValueDescription
$errorTitleERROR (Magento)The title/name of the error that has occurred.
$locationNULL

Location in code that the error occurred.

It depends on where you are calling this exception email as to what you want to set this value to:

  • For .php files: __METHOD__
  • For .phtml files: __FILE__
$extraInfoNULL

Extra information that you wish to have passed into the email.

See below for the default contents of the email, which includes a stacktrace.

Note: if you want a new line within this extra content, pass it in with the string “\n” in your variable.

Example of Email Contents