Skip to content

Instantly share code, notes, and snippets.

@samhernandez
Last active December 21, 2021 03:33
Show Gist options
  • Save samhernandez/941879ada30e0be5b29006c07d7d34f8 to your computer and use it in GitHub Desktop.
Save samhernandez/941879ada30e0be5b29006c07d7d34f8 to your computer and use it in GitHub Desktop.
Craft CMS 3 - stop sending all emails
# .env file
# 1 to kill, 0 or empty for no effect
KILL_ALL_EMAIL=1
<?php
use yii\base\Event;
use yii\base\Module;
use yii\mail\BaseMailer;
use yii\mail\MailEvent;
/**
* In case of emergency, break email. :)
*
* - Partial implementation just for example
* - Assumes you know how to set up a custom module
*
* @see https://craftcms.com/docs/3.x/extend/module-guide.html
*/
class MyModule extends Module
{
public function init()
{
// Option to kill all email sending with an
// environment variable.
Event::on(
BaseMailer::class,
BaseMailer::EVENT_BEFORE_SEND,
function(MailEvent $event) {
if (boolval(App::env('KILL_ALL_EMAIL'))) {
$event->isValid = false;
}
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment