简体   繁体   中英

sending emails in YII2 via contact form sending to another address

I have a problem when sending email messages via the form from the contact tab. I had configure in web.php like

'mailer' => [                
    'class' => 'yii\swiftmailer\Mailer',               
    'transport' => [                   
        'class' => 'Swift_SmtpTransport',                   
        'host' => 'smtp.gmail.com',                  
        'username' => 'email@gmail.com',                  
        'password' => 'PASSWORD',                   
        'port' => '587',                  
        'encryption' => 'tls'               
    ],
    'useFileTransport' => false,
]

but when I send a completed form, I do not receive any messages on my inbox. I do not get any error either. When I check in Yii Debugger, log messages show that:

13:14:10.377    info    yii\mail\BaseMailer::send   Sending email "dasdasdasd" to "admin@example.com"
13:14:10.377    info    yii\swiftmailer\Mailer::sendMessage Sending email "dasdasdasd" to "admin@example.com"

Why it shows that it sent a message to a different address than what I gave? Where is problem?

SiteController:

public function actionContact() {
    $model = new ContactForm();
    if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
        Yii::$app->session->setFlash('contactFormSubmitted');

        return $this->refresh();
    }
    return $this->render('contact', [
        'model' => $model,
    ]);
}
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>

    <?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
    <?= $form->field($model, 'email') ?>
    <?= $form->field($model, 'subject') ?>
    <?= $form->field($model, 'body')->textarea(['rows' => 6]) ?>
    <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
        'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
    ]) ?>

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

<?php ActiveForm::end(); ?>

ok, I figured it out with your suggestion rob006. It should be changed from

if ($model->load(Yii::$app->request->post()) &&
$model->contact(Yii::$app->params['adminEmail'])

to

if ($model->load(Yii::$app->request->post()) && $model->contact($_POST['ContactForm']['email']))

Greetings

Allow less secure apps to access your Gmail account

To disable this security feature:

  1. Click here to access Less Secure App Access in My Account .
  2. Next to “Allow less secure apps: OFF,” select the toggle switch to turn ON.

This setting may not be available for:

Source link

Yii::$app->mailer->compose()
    ->setFrom('<fromUsername>@<yourDomain>')
    ->setTo('<user@Email>')
    ->setSubject('Уведемление с сайта <yourDomain>') // тема письма
    ->setTextBody('Текстовая версия письма (без HTML)')
    ->setHtmlBody('<p>HTML версия письма</p>')
    ->send();

If you are using localhost, you should "Comment" the swiftmailer settings.For example, for XAMPP, you need to do related settings and you do not need swiftmailer. Configure in (php.ini file - sendmail.ini ).

And for other programs, the necessary settings ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM