2014-01-16 127 views
1

一些背景知識,以從數據庫中獲得的電子郵件,嘗試使用Yiimail

我創建一個簡單的函數,電子郵件,每當一個博客的管理員創建一個新的公告的所有用戶。我想使用sql查詢收集所有的電子郵件,並將它們全部輸入到郵件正文中,或者一次一個郵件地循環發送(雖然這可能會花費更長的時間)。

到目前爲止,這是我的代碼:

public function emailAll() 
    { 
     $this->set_mail_settings(); 
     $message = new YiiMailMessage;   

     $request = Yii::app()->db->createCommand(" 
            SELECT email FROM persons WHERE party_id 
           ")->queryRow(); 

       $message->subject = 'Star Cruises - Login Information'; 
       $message->addTo('[email protected]'); 
       $message->from = Yii::app()->params['adminEmail']; 
       Yii::app()->mail->send($message); 
       exit(); 

     } 

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']; 
    } 

只要使用電子郵件的emailAll()函數被調用。

我的問題是$請求。我不知道如何收集所有電子郵件並將它們放入$ message-> addTo();

更新:我是這樣做很好,直到我到了這一點:

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!'; 
       //$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']; 
    } 

然後我得到了這條線從YiiMail.php一個錯誤:

189   $msg = 'Sending email to '.implode(', ', array_keys($message->to))."\n". 

它指出:array_keys ()期望參數1是數組,給出null。

我明白它想要什麼,但我不明白爲什麼發生這個錯誤?

回答

1

出於安全原因,您可以使用以下查詢獲取所有電子郵件ID並使用BCC而不是To

$emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons WHERE party_id = $party_id")->queryRow(); 
$email_ids = explode(",",$emails["em"]); 
$message->setBcc($email_ids); // use bcc to hide email ids from other users 
$message->addTo("[email protected]"); //as To is required, set some dummy id or your own id. 
+0

會不會「WHERE party_id = $ party_id」只收到一封郵件? –

+0

您在問題中添加了一個條件。如果不需要,那麼當我嘗試explode()時,刪除'where condition' –

+0

,我得到以下錯誤 explode()期望參數2是字符串,數組給定。 是不是$電子郵件已經是一個字符串? –