From email with Swift_SmtpTransport

I’ve configured a mail connection with Swift_SmtpTransport (host, username, password, port,…). When I send an email with \Yii::$app->mailer->compose, I use setFrom to force a from email and name, but all mails are sent with the email defined in the transport configuration.

Is it possible to have a transport configuration with user/password and sent emails with another from email (from the same domain)?


Works without problems for me.

Please share your complete "mailer" configuration AND your compose & send email code…

Then it will be easier to track down what is going wrong.


'mailer' => [

    'class'            => 'yii\swiftmailer\Mailer',

    'viewPath'         => '@app/mail',

    'useFileTransport' => false,

    'transport' => [

        'class'      => 'Swift_SmtpTransport',

        'host'       => '',

        'username'   => '',

        'password'   => 'trololo',

        'port'       => '587',

        'encryption' => 'tls',



\Yii::$app->mailer->compose('visit_canceled_commercial_agent', ['model' => $model])

  ->setFrom([$company->email => $company->name])


  ->setSubject(Yii::t('app', 'Visit canceled'))


This looks correct to me.

Similar code is running at my end without any problems.

I think the reason for your problem is gmail:

Gmail is not allowing to send emails with "faked" sender address to avoid misusage / spammers… So when they detect someone wants so send emails from "" but is successfully authenticated as "" they will replace the "from" address with "" no matter what.

That would make sense.

Imagine everyone could fake ANY gmail address over the gmail servers.

Solution would be an own local mail server (Mercury for example) to test your / mailings.


Ok! I’ll try it. Thank you!