无法在yii2中发送电子邮件
我正在使用官方 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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论