简体   繁体   English

通过联系表格在 YII2 中发送电子邮件发送到另一个地址

[英]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我在web.php进行了配置,例如

'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:当我检查 Yii Debugger 时,日志消息显示:

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. 好的,我想出了你的建议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 允许安全性较低的应用访问您的Gmail帐户

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. 如果使用本地主机,则应“注释” swiftmailer设置。例如,对于XAMPP,您需要进行相关设置,而无需swiftmailer。 Configure in (php.ini file - sendmail.ini ). 在中配置(php.ini文件-sendmail.ini)。

And for other programs, the necessary settings ... 对于其他程序,必要的设置...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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