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

前端 未结 10 1174
梦谈多话
梦谈多话 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:05

    I've tested it using the following code:

    $emails = ['myoneemail@esomething.com', 'myother@esomething.com','myother2@esomething.com'];
    
    Mail::send('emails.welcome', [], function($message) use ($emails)
    {    
        $message->to($emails)->subject('This is test e-mail');    
    });
    var_dump( Mail:: failures());
    exit;
    

    Result - empty array for failures.

    But of course you need to configure your app/config/mail.php properly. So first make sure you can send e-mail just to one user and then test your code with many users.

    Moreover using this simple code none of my e-mails were delivered to free mail accounts, I got only emails to inboxes that I have on my paid hosting accounts, so probably they were caught by some filters (it's maybe simple topic/content issue but I mentioned it just in case you haven't received some of e-mails) .

    0 讨论(0)
  • 2020-11-28 06:07

    If you want to send emails simultaneously to all the admins, you can do something like this:

    In your .env file add all the emails as comma separated values:

    ADMIN_EMAILS=admin1@site.com,admin2@site.com,admin3@site.com
    

    so when you going to send the email just do this (yes! the 'to' method of message builder instance accepts an array):

    So,

    $to = explode(',', env('ADMIN_EMAILS'));
    

    and...

    $message->to($to);
    

    will now send the mail to all the admins.

    0 讨论(0)
  • 2020-11-28 06:08

    it works for me fine, if you a have string, then simply explode it first.

    $emails = array();

    Mail::send('emails.maintenance',$mail_params, function($message) use ($emails) {
        foreach ($emails as $email) {
            $message->to($email);
        }
    
        $message->subject('My Email');
    });
    
    0 讨论(0)
  • 2020-11-28 06:12

    the accepted answer does not work any longer with laravel 5.3 because mailable tries to access ->email and results in

    ErrorException in Mailable.php line 376: Trying to get property of non-object

    a working code for laravel 5.3 is this:

    $users_temp = explode(',', 'first@example.com,second@example.com');
        $users = [];
        foreach($users_temp as $key => $ut){
          $ua = [];
          $ua['email'] = $ut;
          $ua['name'] = 'test';
          $users[$key] = (object)$ua;
        }
     Mail::to($users)->send(new OrderAdminSendInvoice($o));
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 06:13

    You can loop over recipientce like:

    foreach (['taylor@example.com', 'dries@example.com'] as $recipient) {
        Mail::to($recipient)->send(new OrderShipped($order));
    }
    

    See documentation here

    0 讨论(0)
提交回复
热议问题