JackieLino
JackieLino

Reputation: 23

Cannot send multiple email Yii2

I working with framework Yii2 and I want send mail with Mailing. 1 email address sent successfully but array email address sent fail and Show error message:

Exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [[email protected],[email protected]] does not comply with RFC 2822, 3.6.2.'

My code send mail:

Yii::$app->mailer->setTransport([
                    'class' => 'Swift_SmtpTransport',
                    'host' => 'smtp.gmail.com',
                    'username' => $valueEmailSystem->username_mail,
                    'password' => $valueEmailSystem->password_mail,
                    'port' => $valueEmailSystem->port,
                    'encryption' => 'tls',
                ]);

$message = Yii::$app->mailer->compose();
            $message->setFrom([$valueEmailSystem->username_mail => 'TEST']);
            $str = implode(',', (array)$modelUser);
            $message->setBcc($str);
            $message->setSubject('Xác nhận yêu cầu');
            $strUserName = implode(',', (array)User::getNameById($pk));
            foreach ($valueContent as $key => $item) {
                $reValue = str_replace("[hoten]",$strUserName,$item->value);
                $message->setHtmlBody($reValue);
            }
            $message->send();

Help me! Thank all.

Upvotes: 0

Views: 176

Answers (1)

Michal Hynčica
Michal Hynčica

Reputation: 6144

The problem is that you are imploding the addresses into one string. The setBcc() method accepts one address as string or multiple addresses as array, for example like that:

$message->setBcc(['[email protected]', '[email protected]']);

Or if you want to specify names

$message->setBcc([
   '[email protected]' => 'First Person',
   '[email protected]' => 'Second Person',
]);

Documentation of method.

Upvotes: 0

Related Questions