Why is swiftmailer not showing senders' email address in yii2?
I have a problem. I created a form Contact
on my web page, which includes: name
, surname
, email
and description
. When I submit my form, an email is sent, but the senders email is my email address. Fe:
http://imageshack.com/a/img922/801/eVyutz.png
my.email@gmail.com
it shows not the email that the user entered in the form, but mine. And after that, when I click Reply button
and type a message, I send it to myself. Why?
Here's what I did:
My mailer
:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'my.email@gmail.com',
'password' => 'password',
'port' => '465',
'encryption' => 'ssl',
'streamOptions' => [ 'ssl' =>
[ 'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
],
],
My actionContact()
:
public function actionContact()
{
$model = new Contact();
$model->scenario = Contact::SCENARIO_CREATE;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Success'));
}
else {
Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Failed'));
}
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
And mine model function
:
public function sendEmail($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name." ".$this->surname])
->setSubject($this->subject)
->setTextBody($this->text)
->send();
}
Please help me to solve this, what is wrong? Thanks for any help!
I think there is something wrong with the variable $email
. Because when I use setFrom
for email
, it only shows my letter.
source to share
You may not have assigned the correct value for $ this-> email (try checking this for example: with var_dump ($ this-> email) and for $ this-> name and $ this-> username) you can use the correct parameter, assigned in param.php
<?php
return [
'adminEmail' => 'my_mail@my_domain.com',
];
...
public function sendEmail($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([\Yii::$app->params['adminEmail'] => $this->name." ".$this->surname])
// ->setFrom([\Yii::$app->params['adminEmail'] => 'try fixed test if $this->name and username not work'])
->setSubject($this->subject)
->setTextBody($this->text)
->send();
}
source to share