1

I am using yii2 advanced contact us form. but unable to get email of user which is filled in email field of contact form..

    public function sendEmail($email)
        {    
            return Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([$this->email => $this->name])  
//echo "emailll<pre>";print_r($this->email);exit;  
//here I am getting the correct email which is filled in contact form
//but when I received mail. nowhere I got That email id...
// In from: username@exmple.com which i set as username
// In To : admin@example.com which i set as admin email id           
                ->setSubject($this->subject)
                ->setTextBody($this->body)
                ->send();
        }

how to get that email?

here view/contact.php

<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>

            <?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>

            <?= $form->field($model, 'email') ?> **<--I want this email id to be printed on received mail-->**

            <?= $form->field($model, 'subject') ?>

            <?= $form->field($model, 'body')->textarea(['rows' => 2])//->label('Message') ?>

            <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
                'template' => '<div class="row"><div class="col-lg-5">{image}</div><div class="col-lg-5">{input}</div></div>',
            ]) ?>

            <div class="form-group">
                <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
            </div>

        <?php ActiveForm::end(); ?>
Shailesh Yadav
  • 205
  • 3
  • 12

2 Answers2

1

I think you may get it that way:

 public function sendEmail($email)
    {    
        return Yii::$app->mailer->compose()
            ->setTo($email)
            ->setFrom([$this->email => $this->name])         
            ->setSubject($this->subject)
            ->setTextBody($this->body . ' Email from: ' . $this->email)
            ->send();
    }
Anastasia S
  • 149
  • 2
  • 13
  • No, we can’t do it that way if you want to get the property in text of the mail. – Anastasia S Nov 08 '17 at 04:21
  • but it will make sense if i got it in setfrom. – Shailesh Yadav Nov 08 '17 at 04:26
  • .->setFrom([$this->email => $this->name]) ....$this->email should be email id of visitor who fills contact us from.... Its fine if I dont get email id from which mail is actually sent, but want email id from contact form – Shailesh Yadav Nov 08 '17 at 04:33
  • 1
    $this->email is the property of contactForm model that was filled by user. Watch the ContactForm model and how the properties set (I think with load function). – Anastasia S Nov 08 '17 at 04:44
0

To use Mailer, you should configure it in the application configuration like the following:

[
'components' => [
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'localhost',
            'username' => 'username',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],
    // ...
],
],
ekaratas
  • 92
  • 5