如何在管理员收到的电子邮件中获取联系我们表格的电子邮件字段(yii2)

时间:2017-11-02 06:25:33

标签: yii2 contactus

我正在使用yii2高级联系我们表格。但是无法收到填写在联系表格的电子邮件领域的用户的电子邮件..

    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();
        }

如何收到该电子邮件?

这里是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(); ?>

2 个答案:

答案 0 :(得分:1)

我想你可能会这样:

 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();
    }

答案 1 :(得分:0)

要使用Mailer,您应该在应用程序配置中对其进行配置,如下所示:

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