一点背景,
我正在创建一个简单的功能,只要博客管理员创建新公告,就会向所有用户发送电子邮件。我想使用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为数组,给定为null。
我明白它想要什么,但我不明白为什么会出现这个错误?
答案 0 :(得分: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("noreply@yourdomain.com"); //as To is required, set some dummy id or your own id.