Override Mailer to change the message before send

I’m overriding yii\swiftmailer\Mailer and I want to change the message content before send the email.

How can I do it? I don’t find any way to get the message body and change it.

make sure you are using your CustomMailer in config for mailer component


// change this according your app structure

namepace app\mailers;

class CustomerMailer extends \yii\swiftmailer\Mailer {

	public function compose($template, $options)


		// you can do you modifications here



Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])





This is from the PassswordResetRequestForm model from the default advanced app

return Yii::$app->mailer->compose(

            ['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user]


        ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])


        ->setSubject('Password reset for ' . Yii::$app->name)


This is without subclassing.

The configuration is:

        '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,


There is a directory common\mail\layouts with two files in it: html.php & text.php.

So not sure what ‘passwordResetToken-html’ ‘*-text’ means.