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)?

Hi!

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.

Regards




'mailer' => [

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

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

    'useFileTransport' => false,

    'transport' => [

        'class'      => 'Swift_SmtpTransport',

        'host'       => 'smtp.gmail.com',

        'username'   => 'tralalala@gmail.com',

        'password'   => 'trololo',

        'port'       => '587',

        'encryption' => 'tls',

    ],

],






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

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

  ->setTo($model->commercialAgent->email)

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

  ->send();



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 "faker@gmail.com" but is successfully authenticated as "tralalala@gmail.com" they will replace the "from" address with "tralalala@gmail.com" 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.

Regards

Ok! I’ll try it. Thank you!