{"id":1262,"date":"2016-01-04T21:03:30","date_gmt":"2016-01-04T15:03:30","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1262"},"modified":"2021-10-23T13:55:10","modified_gmt":"2021-10-23T18:55:10","slug":"magento-custom-error-email","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/magento-custom-error-email\/","title":{"rendered":"Magento 1: Custom Error \/ Exception Email"},"content":{"rendered":"<p>Magento has a process that send the website administrator an email when an exception is thrown. &nbsp;That system works just fine. &nbsp;When this happens, the rendering of the remainder of the page is halted and the user will be displayed an error page.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/01\/Magento-Custom-Error-Email.jpg\" rel=\"attachment wp-att-279\" data-lasso-id=\"585\" data-rel=\"lightbox-gallery-ZdaFH7cf\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/01\/Magento-Custom-Error-Email-500x500.jpg\" alt=\"Magento Custom Exception Email\" class=\"wp-image-1279\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/01\/Magento-Custom-Error-Email-500x500.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/01\/Magento-Custom-Error-Email-150x150.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/01\/Magento-Custom-Error-Email.jpg 600w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption>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.<\/figcaption><\/figure><\/div>\n\n\n\n<p>However, what about the instance where you want to receive the same email while&nbsp;<em>not<\/em> interrupting the user experience. &nbsp;In other-words, send the email and then continue to render the page page. &nbsp;There is no default method for this built into Magento. &nbsp;So continue to read below for a quick fix that allows you to implement an Exception Email without the standard exception process happening.<\/p>\n\n\n\n<p>Note: This code is designed to worith with Magento 1.x versions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Caution<\/h2>\n\n\n\n<p>I&#8217;ll get this out of the way from the get-go. &nbsp;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. &nbsp;Use this sparingly and ensure that you are handling errors correctly.<\/p>\n\n\n\n<p>In my case, the situation where I&#8217;m using this is to alert me if a CURL request had failed. &nbsp;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. &nbsp;So that&#8217;s where this new email alert comes into play.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Code<\/h2>\n\n\n\n<p>In the code below, replace&nbsp;<strong>PromInc<\/strong> with your company name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Generate and Send the Error<\/h3>\n\n\n\n<p>We need to extend the Core helper to add two methods:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Generate a callstack to include in the email<\/li><li>Generate&nbsp;and send the&nbsp;contents of the email<\/li><\/ol>\n\n\n\n<p>Add the following files.<\/p>\n\n\n\n<p><strong>File:<br><\/strong>app\/etc\/modules\/<strong>PromInc_Modules<\/strong>.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;config&gt;\n &lt;modules&gt;\n  &lt;PromInc_Core&gt;\n    &lt;active&gt;true&lt;\/active&gt;\n     &lt;codePool&gt;local&lt;\/codePool&gt;\n  &lt;\/PromInc_Core&gt;\n &lt;\/modules&gt;\n&lt;\/config&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>File:<br><\/strong>app\/code\/local\/<strong>PromInc<\/strong>\/Core\/etc\/config.xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;config&gt;\n  &lt;global&gt;\n    &lt;helpers&gt;\n      &lt;core&gt;\n        &lt;rewrite&gt;\n          &lt;data&gt;PromInc_Core_Helper_Data&lt;\/data&gt;\n        &lt;\/rewrite&gt;\n      &lt;\/core&gt;\n    &lt;\/helpers&gt;\n  &lt;\/global&gt;\n&lt;\/config&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>File:<br><\/strong>app\/code\/local\/<strong>PromInc<\/strong>\/Core\/Helper\/Data.php &nbsp; &nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nclass PromInc_Core_Helper_Data extends Mage_Core_Helper_Data\n{\n\n&nbsp;const LS = \"\\n\";\n\n \/**\n * Get Callstack\n *\n * @param mixed $delim (default self::LS) Line delimiter\n *\n * @return string Callstack\n *\/\n public function get_callstack($delim=self::LS) {\n   $dt = debug_backtrace();\n   $cs = '';\n   foreach ($dt as $t) {\n     $cs .= $t&#091;'file'] . ' line ' . $t&#091;'line'] . ' calls ' . $t&#091;'function'] . \"()\" . $delim;\n   }\n   return $cs;\n }\n\n\n \/**\n * Send an error email to the website adminstrator\n *\n * This is the same error email recieved when an exception is thrown,\n * however this does not interupt the rendering of the rest of the page.\n *\n * This is used for custom functions where you need to be alerted of an instance on the site while not disrupting the user experience.\n *\n * @param mixed $errorTitle (default NULL) Title of the error (default NULL)\n * @param mixed $location (default NULL) Location in code that the error occurred\n * For .php files: __METHOD__\n * For .phtml files: __FILE__\n * @param mixed $extraInfo (default NULL) Optional information to pass in the email\n *\/\n public function sendExceptionEmail($errorTitle=NULL, $location=NULL, $extraInfo=NULL) {\n   $errorDetails = Mage::helper('core')-&gt;get_callstack();\n   $errorDetails .= self::LS.self::LS;\n   $errorDetails .= \"Location In Code:\".self::LS.$location;\n   if( $errorDetails ) {\n     $errorDetails .= self::LS.self::LS;\n     $errorDetails .= \"Error Details:\".self::LS.$extraInfo;\n    }\n    $errorDetails .= self::LS.self::LS;\n\n    $reportData = array(\n      0 =&gt; $errorTitle,\n      1 =&gt; $errorDetails,\n      \"script_name\" =&gt; __METHOD__,\n      \"skin\" =&gt; Mage::app()-&gt;getStore()-&gt;getCode(),\n      \"url\" =&gt; Mage::getSingleton('core\/url')-&gt;parseUrl( Mage::helper('core\/url')-&gt;getCurrentUrl() )-&gt;getPath()\n    );\n\n    if (isset($reportData) &amp;&amp; is_array($reportData)) {\n      require(Mage::getBaseDir().DS.'errors'.DS.'processor.php');\n      $processor = new Error_Processor();\n      $processor-&gt;saveReport($reportData);\n      $processor-&gt;sendReport();\n    }\n }\n\n}<\/code><\/pre>\n\n\n\n<p>With these two files added, clear the cache from within the Magento Admin.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>System -&gt; Cache Management<\/li><li>Check&nbsp;<strong>Configuration<\/strong> and&nbsp;<strong>Layouts<\/strong> and click&nbsp;<strong>Submit<\/strong>.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Trigger the Exception Email<\/h3>\n\n\n\n<p>All that needs to be done now is to trigger the email to send from within your code where an error has occurred.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Mage::helper('core')-&gt;sendExceptionEmail(\"Error Name\", \"Additional Error Information\");<\/code><\/pre>\n\n\n\n<p>Parameters:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parameter Name<\/strong><\/td><td><strong>Default Value<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>$errorTitle<\/td><td>ERROR (Magento)<\/td><td>The title\/name of the error that has occurred.<\/td><\/tr><tr><td>$location<\/td><td>NULL<\/td><td>\n<p>Location in code that the error occurred.<\/p>\n<p>It depends on where you are calling this exception email as to what you want to set this value to:<\/p>\n<ul>\n<li>For .php files: __METHOD__<\/li>\n<li>For .phtml files: __FILE__<\/li>\n<\/ul>\n<\/td><\/tr><tr><td>$extraInfo<\/td><td>NULL<\/td><td>\n<p>Extra information that you wish to have passed into the email.<\/p>\n<p>See below for the default contents of the email, which includes a stacktrace.<\/p>\n<p>Note: if you want a new line within this extra content, pass it in with the string &#8220;\\n&#8221; in your variable.<\/p>\n<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Email Contents<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">URL: http:\/\/www.example.com\/var\/www\/staging\/index.php\/some\/page.html\n\nIP Address: 10.10.10.10\n\nTime: 2015-12-18 22:17:04 GMT\n\nError:\nError Title Will Go Here\n\nTrace:\n\/var\/www\/staging\/app\/code\/core\/Mage\/Core\/Controller\/Varien\/Action.php line 420 calls newajaxAction() \/var\/www\/staging\/app\/code\/core\/Mage\/Core\/Controller\/Varien\/Router\/Standard.php line 254 calls dispatch() \/var\/www\/staging\/app\/code\/core\/Mage\/Core\/Controller\/Varien\/Front.php line 176 calls match() \/var\/www\/staging\/app\/code\/core\/Mage\/Core\/Model\/App.php line 349 calls dispatch() \/var\/www\/staging\/app\/Mage.php line 640 calls run() \/var\/www\/staging\/index.php line 81 calls run()\n\n\nLocation:\nPromInc_Core_Helper_Data::sendExceptionEmail\nThis is additional information I passed into the error<\/pre>","protected":false},"excerpt":{"rendered":"<p>Magento has a process that send the website administrator an email when an exception is thrown. &nbsp;That system [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1279,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[46,5],"tags":[],"class_list":["post-1262","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/01\/Magento-Custom-Error-Email.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-km","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1262"}],"version-history":[{"count":7,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":2033,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1262\/revisions\/2033"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1279"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}