How to send mail to multiple recipients in Yii2 mailer OR how to add setCc in yii2 mailer

Kalai S picture Kalai S · Apr 25, 2015 · Viewed 16.7k times · Source

How to send mail to multiple recipients in Yii2 mailer?

This code for a multiple recipient but not working.

$value = Yii::$app->mailer->compose()
            ->setFrom([$this->email => $this->name])
            ->setTo(array($model->email_1,$model->email_2))
            ->setSubject($model->status)
            ->setHtmlBody($model->description)
            ->send();

How to add setCc in yii2 mailer?

This code for adding setCc but this is also not working.

$value = Yii::$app->mailer->compose()
            ->setFrom([$this->email => $this->name])
            ->setTo($model->email_1)
            ->setCc($model->email_2)
            ->setSubject($model->status)
            ->setHtmlBody($model->description)
            ->send();

Answer

Ydakilux picture Ydakilux · Apr 28, 2015

I just tried the following code, and it's working. The only things strange in your code seems to be in the setFrom with an Array.

        Yii::$app->mailer->compose()
            ->setFrom('[email protected]')
            ->setTo(array('[email protected]', '[email protected]'))
            ->setCc(array('[email protected]'))
            ->setSubject('Sending Email')
            ->setTextBody('This is Plain text content')
            ->setHtmlBody('Please go to  <a href="http://google.com/">GOOGLE</a>')
            ->send();    

In the Swift mailer code there is the following comments :

 * Set the From address of this message.
 *
 * It is permissible for multiple From addresses to be set using an array.
 *
 * If multiple From addresses are used, you SHOULD set the Sender address and
 * according to RFC 2822, MUST set the sender address.

Hope it helps.