Yii框架。我正在尝试将内容放入我的setBody()

时间:2014-01-16 11:43:23

标签: php yii

在整个一天中,我一直在努力向数据库内的所有用户发送批量电子邮件功能。我实际上想用html将内容添加到消息正文中。我只想让身体说出类似的话:

“你好,

点击在此处链接以访问新内容

谢谢。“

我的问题是如何将新创建的数据转换为可以放入setBody()函数的链接?

这是actionCreate()的代码:

    public function actionCreate()
{
    if(get_access('Announcement','add') || get_access_advance('sub-admin','add'))
    {
    $model=new Announcement;
    $AnnouncementId = "";

    $attachmentModel=new Attachments;

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

    $files = CUploadedFile::getInstancesByName('attachments');

    $arrParam = SystemParameters::model()->getSystemParameters(
        array('announcement_content_length')
    );

    $videos = Content::model()->findAllByAttributes(array(
                            'content_id' => $model->id, 'node_type_id' => 10
                        ));

    if(isset($_POST['Announcement']))
    {
  $content = $_POST['Announcement']['content'];
  if(trim($content)=='<br>'){
      $_POST['Announcement']['content'] = '';
  }

        $model->attributes=$_POST['Announcement'];
        //echo '<pre>';print_r($_POST['Announcement']);echo '</pre>';exit;
        if(isset($_POST['Announcement']['country']) AND is_array($_POST['Announcement']['country']))
  $model->country = implode('|',$_POST['Announcement']['country']);
        #else $model->country = 0;
    else {
        if(isset($_POST['Announcement']['country'])){
            $model->country = $_POST['Announcement']['country'];
        } else {
            $model->country = 0;
        }
    }
        $bfs = SystemParameters::model()->getSystemParameters(
            array('Announcement_file_size')
        );
        $filelimit = $bfs["Announcement_file_size"];
        $uploaded = 0;
        foreach ($files as $l=>$f) {
            $uploaded = $uploaded + $f->size;
        }

        if ($uploaded > $filelimit){
            Yii::app()->user->setFlash('error', "Attachments size exceeds file limit!");
        } 
        else {
            if($model->save())
            {
                if($files != null)
                {
                    foreach ($files as $list=>$file)
                    {
                        $attachment=new Attachments;
                        $attachment->filename=$file;
                        $attachment->content_id=$model->id;
                        $attachment->save();
                        $attachment->filename->saveAs(Yii::getPathOfAlias('webroot').'/images/announcements/'.$model->id.$attachment->filename);
                    }
                } if(!empty($_POST['video_link'])) {
                        $videoContent = new Content;
                        $videoContent->party_id = Yii::app()->user->id;
                        $videoContent->node_type_id = '10';
                        $videoContent->content_id = $model->id;
                        $videoContent->date_created = date('Y-m-d H:i:s');
                        $videoContent->content = $_POST['video_link'];
                        $videoContent->save();
                }
                Yii::app()->session['announcement_message'] = 'You have successfully created an announcement.';

                $this->emailAll();

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

            }

        }

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

    $this->render('create',array(
        'model'=>$model,
        'attachmentModel'=>$attachmentModel,
        'images'=>$files,
        'params'=>$arrParam,
        'videos'=>$videos,
    ));
}
else
{
    $this->redirect(Yii::app()->createAbsoluteUrl('news/index',array('page'=>'1')));
}
}

我希望能够把它放在这里:

 $message->setBody('Sample');

更新:

  public function emailAll()
    {

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

            $emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons")->queryRow();
            $email_ids = explode(",",$emails["em"]);
            $message->setBcc($email_ids);
            $message->setBody('Sample');
            $message->subject = 'New Announcement Posted!';
            $message->addTo('no-one@nowhere.com');
            $message->from = Yii::app()->params['adminEmail'];
            Yii::app()->mail->send($message);                      
    }

 private function set_mail_settings()
    {            
        $sysParam = SystemParameters::model()->getSystemParameters(
        array("smtp_host", "smtp_port", 'smtp_user','smtp_password')
    );

        Yii::app()->mail->transportOptions['username'] = $sysParam['smtp_user'];
        Yii::app()->mail->transportOptions['password'] = $sysParam['smtp_password'];
        Yii::app()->mail->transportOptions['host'] = $sysParam['smtp_host'];
        Yii::app()->mail->transportOptions['port'] = $sysParam['smtp_port'];
    }

1 个答案:

答案 0 :(得分:1)

YiiMailMessage包装了SwiftMailer,所以如果你想添加HTML,你可以这样做:

$html_version = '<p>Click <a href="#">link here</a> ...</p>';
$text_version = "Click link here ...\nhttp://example.com";

$message = new YiiMailMessage;
$message->setBody($text_version);
$message->addPart($html_version, 'text/html');