收件人在yii 1.1中发送邮件时收到相同的邮件两次

时间:2016-05-05 06:20:09

标签: email yii

我正在尝试一次向收件人发送邮件。我的代码是这样的:

$message = new YiiMailMessage;
$message->view = "mail";
$params = array('sendEmail'=>$values);
$message->subject = $values['subject'];
$message->setBody($params, 'text/html');
$message->from =$values['email_from'] ;
$message->addTo($values['email_to']);
if(Yii::app()->mail->send($message)){
    Yii::app()->user->setFlash('success','Success sending email. ');
    $this->redirect(array('mail/admin'));

}else{
    Yii::app()->user->setFlash('error','Error while sending email. ');
}

收件人收到邮件,但收到的邮件是两次。

1 个答案:

答案 0 :(得分:0)

每次创建新记录时,您可能正在使用创建操作来通知用户。验证Yii :: app() - > mail-> send($ message)在您的操作中出现一次,例如:

public function actionCreate()
{
    $model= new MyModel;

    // Uncomment the following line if AJAX validation is needed
     $this->performAjaxValidation($model);

    if(isset($_POST['SomeForm']))
    {
        $model->attributes=$_POST['SomeForm'];
        if($model->validate()){
        if($model->save()){

            $message = new YiiMailMessage;
            $message->setBody('<h1>mets-blog.com</h1>','text/html');
            $message->subject = 'Service';
            $message->addTo('mets@blog.com');
            $message->from = 'your@email.com' you want
            Yii::app()->mail->send($message);

            $this->redirect(array('view','id'=>$model->id));
        }
        }
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

也许您通过渲染视图在控制器中调用两次动作。