我正在尝试创建和html页面作为电子邮件的正文。如何使用yiiMail将其传递给我的setBody()?

时间:2014-01-17 01:21:44

标签: php html email yii

我有一个带有以下代码的.php页面:

 <html>
<head></head>
<body>
    Hi <?php echo $user['first_name'].' '.$user['last_name']?>,
    <br><br>
    To reset your password, please go to the following page:
    <br>
    <?php 
            $url = Yii::app()->createAbsoluteUrl('user/reset/id/'.  $key);
            echo "<a href='{$url}'>{$url}</a>";
    ?>
    <br>
    Your password will be automatically reset, and a new password will be emailed to you.        
</body>

我希望能够把它放在我在这里的setBody()里面的Controller中创建的函数中:

  public function emailAll($url)
    {

        $this->set_mail_settings();
        $message = new YiiMailMessage;        

            $emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons WHERE party_id != 184")->queryRow();
            $email_ids = explode(",",$emails["em"]);
            $message->setBcc($email_ids);
            $message->setBody('To view, click here: '.$url);
            $message->subject = 'New Announcement Posted!';
            $message->addTo('dcp@gmail.com');
            $message->from = Yii::app()->params['adminEmail'];
            Yii::app()->mail->send($message);                      
    }

我想要在正文中的URL链接是从actionCreate()编码的,这里是:

 $currentid = Yii::app()->db->createCommand("select id from content where id = (select max(id) from content)")->queryRow();
                Yii::app()->session['announcement_message'] = 'You have successfully created an announcement.';

                $url = Yii::app()->createAbsoluteUrl('announcement/view/id/'.  $currentid["id"]);

                $this->emailAll($url);

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

我现在正试图弄清楚如何将带有html的.php页面发送到setBody()以及我传递的$ url。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

要执行此操作,请将邮件模板.php文件存储为相应视图文件夹中的视图,然后调用

$message->setBody(
    $this->renderPartial("_yourTemplateFileName",
             array("key"=>$key,'user'=>$user),true,false)
 );

RenderPartial可以处理文件并通过将第三个参数return设置为true来返回输出,第四个参数用于处理此上下文中不需要的Js。