1

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

2 Answers 2

3

I'd recommend using a service like Sentry or Bugsnag 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

1
  • Brilliant, thanks. I always thought Sentry was for JS errors for some reason, so didn't think about that. Commented Feb 5, 2021 at 10:00
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 = "[email protected]"; // or get craft website email
    public $adminEmail = "[email protected]"; // 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("[email protected]" => 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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.