Trainee Trainee - 3 years ago 151
PHP Question

Yii send email to multiple recipients

hello im using yiimail here. i want to sent an email to multiple recipients

here is my code

$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
get input value from user and
$model->receiver1
from database. if user input 2 other user for
$mailCC
, this only send to the first email, not both.

ex:

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


the email will only send to
email1@mail.com & email2@mail.com


i've tried

$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);


but it return this error

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


where did i do wrong?

Answer Source

i got this problem done here's my code

$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);

we should explode separator, loop recipients and change to addTo not setTo

thanks to u guys that have give some solution

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download