1818use yii \base \ExitException ;
1919use yii \base \Security ;
2020use yii \base \UserException ;
21+ use yii \mail \BaseMailer ;
22+ use yii \mail \MailerInterface ;
23+ use yii \mail \MailEvent ;
2124use yii \mail \MessageInterface ;
2225use yii \web \Application ;
2326use yii \web \ErrorHandler ;
@@ -30,6 +33,19 @@ class Yii2 extends Client
3033{
3134 use Shared \PhpSuperGlobalsConverter;
3235
36+ const MAIL_METHODS = [
37+ self ::MAIL_CATCH ,
38+ self ::MAIL_EVENT_AFTER ,
39+ self ::MAIL_EVENT_BEFORE ,
40+ self ::MAIL_IGNORE
41+ ];
42+
43+ public const MAIL_CATCH = 'catch ' ;
44+ public const MAIL_EVENT_AFTER = 'after ' ;
45+ public const MAIL_EVENT_BEFORE = 'before ' ;
46+ public const MAIL_IGNORE = 'ignore ' ;
47+
48+
3349 const CLEAN_METHODS = [
3450 self ::CLEAN_RECREATE ,
3551 self ::CLEAN_CLEAR ,
@@ -64,6 +80,10 @@ class Yii2 extends Client
6480 */
6581 public $ configFile ;
6682
83+ /**
84+ * @var self::MAIL_CATCH|self::MAIL_IGNORE|self::MAIL_AFTER|self::MAIL_BEFORE $mailMethod method for handling mails
85+ */
86+ public $ mailMethod ;
6787 /**
6888 * @var string method for cleaning the response object before each request
6989 */
@@ -267,7 +287,15 @@ public function startApp(?\yii\log\Logger $logger = null): void
267287 unset($ config ['container ' ]);
268288 }
269289
270- $ config = $ this ->mockMailer ($ config );
290+ match ($ this ->mailMethod ) {
291+ self ::MAIL_CATCH => $ config = $ this ->mockMailer ($ config ),
292+ self ::MAIL_EVENT_AFTER => $ config ['components ' ]['mailer ' ]['on ' . BaseMailer::EVENT_AFTER_SEND ] = fn (MailEvent $ event ) => $ this ->emails [] = $ event ->message ,
293+ self ::MAIL_EVENT_BEFORE => $ config ['components ' ]['mailer ' ]['on ' . BaseMailer::EVENT_BEFORE_SEND ] = function (MailEvent $ event ) {
294+ $ this ->emails [] = $ event ->message ;
295+ return true ;
296+ },
297+ self ::MAIL_IGNORE => null // Do nothing
298+ }
271299 Yii::$ app = Yii::createObject ($ config );
272300
273301 if ($ logger instanceof \yii \log \Logger) {
0 commit comments