I built a website which has a contact form. I use yii2 mailer and smtp gmail setup for sending email. This is my smtp setup:
'host' => 'smtp.gmail.com',
'username' => '[email protected]',
'password' => 'mysecretpassword',
'port' => '587',
'encryption' => 'tls',
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
And this is how I send the email in my controller:
Yii::$app->mailer->compose()
->setFrom(["[email protected]" => "My Name"])
->setTo(['[email protected]' => 'Henry'])
->setSubject("The Subject")
->setHtmlBody("<html><body><h1>Hello</h1></body></html>")
->send();
It is sent successfully, but I check the email header and found this:
From: My Name <[email protected]>
What I want is this:
From: My Name <[email protected]>
If I click reply, it will reply to [email protected] which is the username of my smtp gmail setup instead of [email protected]
How can I use [email protected] in my From header?
CodePudding user response:
According to this question you must set setFrom
as indexed array and add setReplyTo
call:
Yii::$app->mailer->compose()
->setFrom(["[email protected]", "My Name"])
->setReplyTo('[email protected]')
->setTo(['[email protected]' => 'Henry'])
->setSubject("The Subject")
->setHtmlBody("<html><body><h1>Hello</h1></body></html>")
->send();
Also it's possible that Gmail itself allows to send from
only for validated emails addresses and not random ones. Check here