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

北城余情 提交于 2019-12-22 04:09:08

问题


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

回答1:


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.




回答2:


Worked for me:

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



回答3:


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();
    }



回答4:


You only need to enclose in brackets:

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

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



来源:https://stackoverflow.com/questions/29862124/how-to-send-mail-to-multiple-recipients-in-yii2-mailer-or-how-to-add-setcc-in-yi

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