3

I have a problem here. I created a Contact form in my webpage, which includes: name, surname, email and description. When I submit my form, the email sends, but the senders email is my email. F.e:

http://imageshack.com/a/img922/801/eVyutz.png

my.email@gmail.com shows not the email, which user entered in a form, but mine. And after that when I click Reply button and type a message back, I send it to myself. Why?

Here is 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 my 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? Thank you for any help!

I guess there will be something wrong with $email variable. Because when I use setFrom to email, it shows just my email.

HarryFlopper
  • 195
  • 1
  • 2
  • 10

1 Answers1

0

could be you have not assigned a proper value for $this->email (try check for this eg: with var_dump($this->email) and for $this->name and $this->username) you could use a proper param 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();
}
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I've tried to replace `setFrom` to `->setFrom([\Yii::$app->params['adminEmail'] => $this->name." ".$this->surname])`, but this didn't help. I'm chating with myself anyway :( – HarryFlopper May 13 '17 at 11:42
  • Do you really understand my problem correctly? Senders `name` and `surname` shows correctly, but instead of his `email`, which he has written in the form, it shows mine and cannot write him back – HarryFlopper May 13 '17 at 11:47
  • and $this->email ... what's contain ? .. what do you get with var_dump($this->email);? – ScaisEdge May 13 '17 at 14:13