无法在yii2中发送电子邮件

发布于 2025-02-09 07:24:10 字数 3970 浏览 0 评论 0原文

我正在使用官方 yiii2文档,以发送电子邮件<< /a>,我还检查了几个教程和Stackoverflow答案,但我不明白错误。

我希望发送一封电子邮件,供用户单击以设置新密码时,他们忘记了当前密码。

遵循文档中的指南,在发送表单时设置了配置

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'htmlLayout' =>  '@app/mail/layouts/html',
            // 'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            //'useFileTransport' => true,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'username',
                'password' => 'app_password',
                'port' => '587',
                'encryption' => 'ssl',
            ],
            'streamOptions' => [ 'ssl' => [ 
                'allow_self_signed' => true,                         
                'verify_peer' => false,                         
                'verify_peer_name' => false,                     
                ],
            ],

和控制器

public function actionForgot()
    {
            
        if(isset($_POST['Lupa']))
        {
            $getEmail=$_POST['Lupa']['email'];

            $getModel= Users::findOne(['email' => $getEmail]);
            // User::model()->findByAttributes(array('email'=>$getEmail));
            $getToken=rand(0, 99999);
            $getTime=date("H:i:s");
            // $getModel->token=md5($getToken.$getTime);
            $org="Some Team";
            $emailadmin="admin_email";
            $subjek="Reset Password";
            $setpesan="you have successfully reset your password<br/>
                <a href='http://localhost:8080/site/forgot?r=site/vertoken/view&token='>Click Here to Reset Password</a>";
                // ".$getModel->token."
            
            if(!$getModel) {
                Yii::$app->session->setFlash('error', 'Please fill in an registered email');
                // return Yii::$app->controller->redirect(Url::to(['site/login']));
            }
            else if($getModel->validate())
                {
                    $name='=?UTF-8?B?'.base64_encode($org).'?=';
                    $subject='=?UTF-8?B?'.base64_encode($subjek).'?=';
                    $headers="From: $name <{$emailadmin}>\r\n".
                        "Reply-To: {$model->email}\r\n".
                        "MIME-Version: 1.0\r\n".
                        "Content-type: text/html; charset=UTF-8";
                    $getModel->save();
                    Yii::app()->user->setFlash('forgot','link to reset your password has been sent to your email');
                    mail($getEmail,$subject,$setpesan,$headers);
                    Yii::$app->mailer->compose()
                    // $type, ['data' => $data]
                        ->setFrom($emailadmin)
                        ->setTo($getEmail)
                        ->setSubject($subjek)
                        ->send();
                    $this->refresh();
                }
            
        }
        $this->layout = 'forgot';
        return $this->render('forgot');
    }

,这是我在控制台上

控制台错误

引起错误的代码段,

<?php $form = ActiveForm::begin([
                        'id' => 'forgot-form',
                        'enableClientValidation'=>false,
                        'validateOnSubmit' => true, 
                    ]); ?>

这是导致从视图文件中

您可以帮助您吗?

I'm using the official Yii2 documentation for sending emails, I have also checked several tutorials and StackOverflow answers but I don't understand the error.

I wish to send an email for users to click through to set up a new password when they forget the current one.

Following the guide in the document I have set up the config

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'htmlLayout' =>  '@app/mail/layouts/html',
            // 'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            //'useFileTransport' => true,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'username',
                'password' => 'app_password',
                'port' => '587',
                'encryption' => 'ssl',
            ],
            'streamOptions' => [ 'ssl' => [ 
                'allow_self_signed' => true,                         
                'verify_peer' => false,                         
                'verify_peer_name' => false,                     
                ],
            ],

and the controller

public function actionForgot()
    {
            
        if(isset($_POST['Lupa']))
        {
            $getEmail=$_POST['Lupa']['email'];

            $getModel= Users::findOne(['email' => $getEmail]);
            // User::model()->findByAttributes(array('email'=>$getEmail));
            $getToken=rand(0, 99999);
            $getTime=date("H:i:s");
            // $getModel->token=md5($getToken.$getTime);
            $org="Some Team";
            $emailadmin="admin_email";
            $subjek="Reset Password";
            $setpesan="you have successfully reset your password<br/>
                <a href='http://localhost:8080/site/forgot?r=site/vertoken/view&token='>Click Here to Reset Password</a>";
                // ".$getModel->token."
            
            if(!$getModel) {
                Yii::$app->session->setFlash('error', 'Please fill in an registered email');
                // return Yii::$app->controller->redirect(Url::to(['site/login']));
            }
            else if($getModel->validate())
                {
                    $name='=?UTF-8?B?'.base64_encode($org).'?=';
                    $subject='=?UTF-8?B?'.base64_encode($subjek).'?=';
                    $headers="From: $name <{$emailadmin}>\r\n".
                        "Reply-To: {$model->email}\r\n".
                        "MIME-Version: 1.0\r\n".
                        "Content-type: text/html; charset=UTF-8";
                    $getModel->save();
                    Yii::app()->user->setFlash('forgot','link to reset your password has been sent to your email');
                    mail($getEmail,$subject,$setpesan,$headers);
                    Yii::$app->mailer->compose()
                    // $type, ['data' => $data]
                        ->setFrom($emailadmin)
                        ->setTo($getEmail)
                        ->setSubject($subjek)
                        ->send();
                    $this->refresh();
                }
            
        }
        $this->layout = 'forgot';
        return $this->render('forgot');
    }

When I send the form, this is the error I get on the console

console error

And this is the code snippet that causes the error

<?php $form = ActiveForm::begin([
                        'id' => 'forgot-form',
                        'enableClientValidation'=>false,
                        'validateOnSubmit' => true, 
                    ]); ?>

It's from the view file

Could you please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文