<p>I'd recommend using a service like <a href="https://sentry.io" rel="nofollow noreferrer">Sentry</a> or <a href="https://www.bugsnag.com" rel="nofollow noreferrer">Bugsnag</a> that are tailored exactly for this situation and include some Craft integrations as well:</p>
<p><a href="https://plugins.craftcms.com/search?q=sentry" rel="nofollow noreferrer">https://plugins.craftcms.com/search?q=sentry</a></p>
<p><a href="https://plugins.craftcms.com/search?q=bugsnag" rel="nofollow noreferrer">https://plugins.craftcms.com/search?q=bugsnag</a></p>

CC BY-SA 4.0
2021-02-05T10:00:49.800Z
Brilliant, thanks. I always thought Sentry was for JS errors for some reason, so didn't think about that.
Laurence Cope
CC BY-SA 4.0
2021-02-04T23:40:30.907Z
2021-02-04T23:40:30.907Z
Brad Bell
<p>The following is untested and unfinished, but I hope it might get you going:</p>
<p>In Craft CMS, there is an event &quot;EVENT_BEFORE_HANDLE_EXCEPTION&quot;, which you could use to handle exceptions. With a plugin or a module, you could do something like:</p>
<pre><code>    Event::on(
        ErrorHandler::className(),
        ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION,
        function(ExceptionEvent $event) {
            $this-&gt;myPlugin-&gt;processException($event-&gt;exception);
        }
    );
</code></pre>
<p>And in your plugin services, you have that function 'processException' that should be able to read the resulting status code and act accordingly</p>
<pre><code>use Craft;
use craft\base\Component;
use yii\base\Exception;

class myService extends Component
{
    public function processException($exception)
    {
        $lastStatusCode = $exception-&gt;statusCode ?? null;
        
        // check for errorcodes, i.e. 400, then do something
</code></pre>
<p>Lastly, if you wish to have Craft send you emails, there is a built-in function for that</p>
<pre><code>public function sendErrorMail($e, $myerror, $url = null)
{
    Craft::$app-&gt;mailer-&gt;send($this-&gt;getErrorMessage($e, $myerror, $url));

}
</code></pre>
<p>getErrorMessage just puts together the email contents and is reusable</p>
<pre><code>    public $senderEmail = &quot;website@doe.com&quot;; // or get craft website email
    public $adminEmail = &quot;admin@doe.com&quot;; // or get craft admin unser email

    public function getErrorMessage($e, $myerror, $url = null)
    {
    if (!empty($e)) {
        $message = $e-&gt;getMessage();
    } else {
        $message = null;
    }

    {# might need to set temp view path with  $this-&gt;view-&gt;setTemplatesPath(ModuleName::$instance-&gt;getBasePath());#}
    
    $body = $this-&gt;view-&gt;renderTemplate('/templates/email/error.twig',
        array(&quot;exceptionMessage&quot; =&gt; $message, &quot;error&quot; =&gt; $myerror, &quot;url&quot; =&gt; $url));

    $senderFull = array(&quot;john@doe.com&quot; =&gt; Craft::t('module-handle','www.doe.com Website'));

    $message = new Message();
    $message-&gt;setFrom($senderFull);
    $message-&gt;setReplyTo($this-&gt;senderEmail);
    $message-&gt;setTo($this-&gt;adminEmail);
    $message-&gt;setSubject(&quot;Error on www.doe.com&quot;);
    $message-&gt;setHtmlBody($body);
    return $message;
}
</code></pre>

CC BY-SA 4.0
2021-02-04T10:55:07.737Z
2021-02-04T10:55:07.737Z
MisterMike
<p>Is it possible to receive email notifications when there is an error or Bad Request issue in Craft CMS?</p>
<p>We had an issue where our contact forms were not working due to craft.app.config.general.[csrfTokenName] changing to craft.app.config.general.csrfTokenName. No warnings in the deprecated code section. Instead, potential customers would see a Bad Request message, but we had no idea for days.</p>
<p>I would like to be alerted if such an error occurs so I can fix the issue much quicker.</p>
<p>Similarly, if there is an error on the site anywhere, I would like to be alerted.</p>
<p>Is it possible?</p>
<p>Thanks</p>

<error-handling>
How to receive email notification when an error or bad request happens?
# How to receive email notification when an error or bad request happens?

(Asked by: username_0 on 2021-02-04)

Is it possible to receive email notifications when there is an error or Bad Request issue in Craft CMS?

We had an issue where our contact forms were not working due to craft.app.config.general.[csrfTokenName] changing to craft.app.config.general.csrfTokenName. No warnings in the deprecated code section. Instead, potential customers would see a Bad Request message, but we had no idea for days.

I would like to be alerted if such an error occurs so I can fix the issue much quicker.

Similarly, if there is an error on the site anywhere, I would like to be alerted.

Is it possible?

Thanks

**Tags:** error-handling


---

## 2 answers

### Answer by username_1 on 2021-02-04. Score: 3

I'd recommend using a service like [Sentry](https://sentry.io) or [Bugsnag](https://www.bugsnag.com) that are tailored exactly for this situation and include some Craft integrations as well:

<https://plugins.craftcms.com/search?q=sentry>

<https://plugins.craftcms.com/search?q=bugsnag>

> **Comments:**
> **username_0**: Brilliant, thanks. I always thought Sentry was for JS errors for some reason, so didn't think about that.  

---

### Answer by username_2 on 2021-02-04. Score: 1

The following is untested and unfinished, but I hope it might get you going:
In Craft CMS, there is an event "EVENT_BEFORE_HANDLE_EXCEPTION", which you could use to handle exceptions. With a plugin or a module, you could do something like:
    Event::on(
        ErrorHandler::className(),
        ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION,
        function(ExceptionEvent $event) {
            $this->myPlugin->processException($event->exception);
        }
    );

And in your plugin services, you have that function 'processException' that should be able to read the resulting status code and act accordingly
use Craft;
use craft\base\Component;
use yii\base\Exception;

class myService extends Component
{
    public function processException($exception)
    {
        $lastStatusCode = $exception->statusCode ?? null;
        
        // check for errorcodes, i.e. 400, then do something

Lastly, if you wish to have Craft send you emails, there is a built-in function for that
public function sendErrorMail($e, $myerror, $url = null)
{
    Craft::$app->mailer->send($this->getErrorMessage($e, $myerror, $url));

}

getErrorMessage just puts together the email contents and is reusable
    public $senderEmail = "website@doe.com"; // or get craft website email
    public $adminEmail = "admin@doe.com"; // or get craft admin unser email

    public function getErrorMessage($e, $myerror, $url = null)
    {
    if (!empty($e)) {
        $message = $e->getMessage();
    } else {
        $message = null;
    }

    {# might need to set temp view path with  $this->view->setTemplatesPath(ModuleName::$instance->getBasePath());#}
    
    $body = $this->view->renderTemplate('/templates/email/error.twig',
        array("exceptionMessage" => $message, "error" => $myerror, "url" => $url));

    $senderFull = array("john@doe.com" => Craft::t('module-handle','www.doe.com Website'));

    $message = new Message();
    $message->setFrom($senderFull);
    $message->setReplyTo($this->senderEmail);
    $message->setTo($this->adminEmail);
    $message->setSubject("Error on www.doe.com");
    $message->setHtmlBody($body);
    return $message;
}


---

