2014-01-16 19 views
0

在整個這一整天中,我一直致力於向數據庫內的所有用戶發送批量電子郵件功能。我實際上想要使用html將內容添加到消息的正文中。我只想身體這樣說:Yii框架。我試圖將內容放入我的setBody()

「你好,

點擊鏈接在這裏訪問新的內容

謝謝你。」

我的問題是如何將新創建的數據轉換爲可放入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'); 

UPDATE:

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('[email protected]'); 
      $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']; 
    } 
+0

你在哪裏聲明'$ message'? – adamS

+0

@adamS我更新了我的問題。請檢查 –

回答

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'); 
+0

是的,但是如何將創建的文件ID轉換爲鏈接?一旦我做了actionCreate(),我想獲得這個鏈接,這樣我就可以把它放在body中。 –

+0

使用類似'file_put_contents($ filename,$ content)'的東西。只要它位於網絡可訪問的文件夾中,就可以鏈接到該文件,並且您可以在該消息中包含該鏈接。 –

+0

我正在考慮這樣做: Yii :: app() - > createAbsoluteUrl('user/reset/id /'。$ key); 但我失去了找到我的關鍵哈哈。 –