Yii向多个收件人发送电子邮件

时间:2017-09-29 07:34:10

标签: php email yii

你好我在这里使用yiimail。我想向多个收件人发送电子邮件

这是我的代码

$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
// $mail->setTo(array($emailReceiver));
$mail->setTo($model->receiver1);
$mail->setCC($mailCC);
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);

$mailCC从用户获取输入值,从数据库获取$model->receiver1。如果用户为$mailCC输入了2个其他用户,则只发送给第一封电子邮件,而不是两者都发送。

例如:

$model->receiver1=email1@mail.com
$mailCC = array("email2@mail.com", "email3@mail.com") //this is from user input

电子邮件只会发送到email1@mail.com & email2@mail.com

我试过

    $mailcc = explode(",", $model->EMAIL_RECEIVER);
    $mail = new YiiMailMessage;
    $mail->from = Yii::app()->params['senderEmail'];
    // $mail->setTo(array($emailReceiver));
    $mail->setTo($model->receiver1);
    $mail->setCC(array($mailCC)); //this one with array
    $mail->Subject = $model->SUBJECT;
    $mail->Body = $model->BODY_EMAIL;
    Yii::app()->mail->send($mail);

但它会返回此错误

preg_match() expects parameter 2 to be string, array given

我哪里做错了?

2 个答案:

答案 0 :(得分:0)

你可以试试这个

$mail->addCC($mailCC[0]);
$mail->addCC($mailCC[1]);

希望它能奏效。这是我完整的运行代码

    $message = new YiiMailMessage;
    $message->view = 'registrationFollowup';

    //userModel is passed to the view
    $message->setBody(['userModel' => "test"], 'text/html');

    $message->setTo("xxxx@gmail.com");
    $message->addCC('yyyy@gmai.com');
    $message->addCC('zzz@gmai.com');
    $message->from = "pqr@gmail.com";
    $status = Yii::app()->mail->send($message);
    print_r($status); //print the number of recipient, which 3 

答案 1 :(得分:0)

我完成了这个问题 这是我的代码

$mailcc = explode(",", $model->EMAIL_RECEIVER);
$mail = new YiiMailMessage;
$mail->from = Yii::app()->params['senderEmail'];
foreach($mailcc as $to){
    $mail->addTo(trim($to));
}
$mail->Subject = $model->SUBJECT;
$mail->Body = $model->BODY_EMAIL;
Yii::app()->mail->send($mail);

我们应该爆炸分隔符,循环收件人并更改为addTo not setTo

感谢你们提供了一些解决方案