Laravel Mail::send() sending to multiple to or bcc addresses

前端 未结 10 1211
梦谈多话
梦谈多话 2020-11-28 05:26

I can\'t seem to successfully send to multiple addresses when using Laravel\'s Mail::send() callback, the code does however work when I only sp

10条回答
  •  执笔经年
    2020-11-28 06:13

    I am using Laravel 5.6 and the Notifications Facade.

    If I set a variable with comma separating the e-mails and try to send it, I get the error: "Address in mail given does not comply with RFC 2822, 3.6.2"

    So, to solve the problem, I got the solution idea from @Toskan, coding the following.

            // Get data from Database
            $contacts = Contacts::select('email')
                ->get();
    
            // Create an array element
            $contactList = [];
            $i=0;
    
            // Fill the array element
            foreach($contacts as $contact){
                $contactList[$i] = $contact->email;
                $i++;
            }
    
            .
            .
            .
    
            \Mail::send('emails.template', ['templateTitle'=>$templateTitle, 'templateMessage'=>$templateMessage, 'templateSalutation'=>$templateSalutation, 'templateCopyright'=>$templateCopyright], function($message) use($emailReply, $nameReply, $contactList) {
                    $message->from('myemail@somecompany.com', 'Some Company Name')
                            ->replyTo($emailReply, $nameReply)
                            ->bcc($contactList, 'Contact List')
                            ->subject("Subject title");
                });
    

    It worked for me to send to one or many recipients.

提交回复
热议问题