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

試著忘記壹切 提交于 2019-12-05 02:36:38

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('addrs1@gmail.com')
            ->setTo(array('addrs1@gmail.com', 'addrs2@hotmail.com'))
            ->setCc(array('addrs3@gmail.com'))
            ->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.

Worked for me:

            ->setTo([
                    'john.doe@gmail.com' => 'John Doe',
                    'jane.doe@gmail.com' => 'Jane Doe',
            ])

Try solution:

$mail = Yii::$app->mailer->compose($mail_type, $params)
        ->setFrom([ self::$_sender => self::$_senderName ])
        ->setSubject($subject);
    foreach(self::$_to as $receiver){
        $mail->setTo($receiver)
            ->send();
    }
open-ecommerce.org

You only need to enclose in brackets:

['pedro@something.com', 'maria@something.com']

It is well documented in the Class reference yii\swiftmailer\Message.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!