yii - 在 Yii 中删除用户时如何实现电子邮件确认

标签 yii email-confirmation

在我的项目中,我需要实现以下功能: - 当用户决定删除其帐户时,在删除之前,应向该用户发送一封带有“$deletionUrl”的电子邮件,以便通过电子邮件确认该决定。 我正在使用 Yiimailer 扩展,它工作正常。但是,我不确定我应该在哪里以及如何放置这些关于删除用户的条件。这是我的操作删除:

public function actionDelete($id) 
{
    $this->loadModel($id)->delete();
    if (!isset($_GET['ajax'])) {
        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
}

我在网上查了一下,发现CActiveRecord有一个 protected 方法beforeDelete()

protected function beforeDelete()
{
    if($this->hasEventHandler('onBeforeDelete'))
    {
        $event=new CModelEvent($this);
        $this->onBeforeDelete($event);
        return $event->isValid;
    }
    else
        return true;
}

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeDelete-detail

但不确定如何使其适应我的情况。还有其他方法吗?

最佳答案

我设法通过以下方式解决了这个问题。我在 UserController 中的 actionDelete 是:

    public function actionDelete($id) {
    $model = $this->loadModel($id);
    $deletionUrl= Yii::app()->createAbsoluteUrl('user/confirm',array('aHash'=>$model->aHash));


       $message = new YiiMailer();
       $message->setView('contact');
       $message->setBody($deletionUrl);
       $message->setData(array('message' => '

        You have received this email because you requested a deletion of your account.
        If you did not make this request, please disregard this
        email. You do not need to unsubscribe or take any further action.
        </br>
        <hr>

        We require that you confirm  your request to ensure that
        the request made  was correct. This protects against
        unwanted spam and malicious abuse.

        To confirm deletion of your account, simply click on the following link:
        '.$deletionUrl.' <br> <br>
        (Some email client users may need to copy and paste the link into your web
        browser).','name' => 'yourname@123.com', 'description' => 'Please   click on the link below in order to confirm your request:'));
       $message->setLayout('mail');
       $message->IsSMTP();
       $message->setSubject ('Request for account deletion');
       $message->Host = 'smtp.123.com';
       $message->SMTPAuth = true;    
       $message->Username = 'yourname@123.com';                            
       $message->Password = 'yourpassword';   
       $message->setFrom('yourname@123.com', 'yourname');
       $message->setTo($model->aEmail);
       if (  $message->send())
     {
        $this->render ('removeuser');
     }
}

我在 UserController 中的 actionConfirm():

    public function actionConfirm ()
{   
   $model = User::model()->findByAttributes(array('aHash' => $_GET['aHash']));
    if ($model === null)
        throw new CHttpException(404, 'Not found');
    else
        {
        $this->loadModel($model->aUserID)->delete();
        $model->save();
        $this->render('afterdelete');
        }
}

关于yii - 在 Yii 中删除用户时如何实现电子邮件确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218356/

相关文章:

php - 从 WSDL 生成 PHP 代码(用于服务替换)

url - 无法在 yii 中获取请求 GET 参数

asp.net - 身份框架测试确认电子邮件 token 是否已过期

php - WordPress 如何在转义 html 标签时使用换行符

ruby-on-rails-3 - 导轨 : email confirmation RESTful (PUT) URL in mailer

jquery - Yii 和 EGMap 扩展(Google map )在复选框更改时刷新 map

yii - 比较 yii 中的问题

python - 我如何发送电子邮件并添加确认链接以单击它?

php - yii2 : Setting rules for csv file upload and retaining original filename