Last active
December 21, 2021 03:33
-
-
Save samhernandez/941879ada30e0be5b29006c07d7d34f8 to your computer and use it in GitHub Desktop.
Craft CMS 3 - stop sending all emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .env file | |
# 1 to kill, 0 or empty for no effect | |
KILL_ALL_EMAIL=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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