Search code examples
yii2swiftmailercodeception

Yii2 BaseMailer test environment not saving or sending


I'm running some unit tests, one of the tests checks that emails are generated and sent.

I have checked the docs, to force Yii to save the emails to a file rather than send I have configured mailer component as follows:

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // send all mails to a file by default. You have to set
        // 'useFileTransport' to false and configure a transport
        // for the mailer to send real emails.
        'useFileTransport' => true
    ],

When running the tests I see the Yii::info message coming from the BaseMailer send() function.

[yii\mail\BaseMailer::send] 'Sending email "Direct Debit Payment Notification" to "[email protected]"'

However the email doesn't get saved anywhere, should be runtime/mail, it is not sent anywhere either.

I have tried to set useFileTransport at runtime using:

$mailer = Yii::$app->mailer;
$mailer->useFileTransport = true;
$mailer->composer... 

But nothing changes, any help is appreciated.


Solution

  • Codeception override your mailer settings ([1] [2]) to use custom mailer, which does not send or save anything. It makes sense - you don't want to send bunch of emails during testing.

    You can use Codeception custom assertions or methods to test sent emails:

    $I->seeEmailIsSent(3); // Test that three emails was sent
    
    $messages = $I->grabSentEmails();
    $I->assertEquals('admin@site,com', $messages[0]->getTo());