php - 尝试使用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('sendTo@someone.com');
 $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() 函数。

我的问题是 $request. 我不知道我怎样收集所有的电子邮件并把它们放入 $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('blah@blah.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'];
 }

然后,我从 YiiMail.php 中得到了一个错误:


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

声明:array_keys() 期望参数 1为 array,空值为。

我知道它想要什么,但我不明白为什么发生这种错误?

时间: 作者:

你可以使用以下查询获取所有电子邮件 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("noreply@yourdomain.com");//as To is required, set some dummy id or your own id.

作者:
...