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

时间:2014-04-22 11:36:37

标签: 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有一个受保护的方法,之前是delete()

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

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

2 个答案:

答案 0 :(得分:3)

我设法以下列方式解决了这个问题。我在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');
        }
}

答案 1 :(得分:0)

protected function beforeDelete()
{

Yii::import('application.extensions.phpmailer.JPhpMailer');
$mail = new JPhpMailer;
$mail->IsSMTP();
$mail->Host = 'smpt.163.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourname@163.com';
$mail->Password = 'yourpassword';
$mail->SetFrom('yourname@163.com', 'yourname');
$mail->Subject = 'PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress($this->email, $this->username);
if($mail->Send())
{

       return parent::beforeDelete();

}



}