How to send simultaneous e-mail with laravel

0

I need my system to send simultaneous e-mail to all the emails that are in the bd. Following the code below it only sends the 1st e-mail from the list and still sends the wrong one. Send pro spam. And the msm code I use in the form d contact that sends to inbox perfectly. Ms without the foreach.

public function sendEmail()
{
    $emails = $this->emailRepository->EmailByStatus();
    $description = 'teste';
    $subject = 'teste';

    $data = array('description'=>$description, 'subject'=>$subject);

       foreach ($emails as $email) 
       {

        $send = Mail::send('email.email-multiple', $data, function($message) use($emails, $description, $subject)
        {
            $message->to($email)->subject($subject);

                $message->from('email@fantasia');

        });

     }
        if(!$send)
        {
            return 0;
        }
        return 1;
}

I've tried it like this:

foreach($emails as $email)
{
     $message->to($email);
}

Tbm only sends the 1st and wrong tbm. And send it.

What's wrong with the above code?

    
asked by anonymous 23.05.2017 / 20:42

1 answer

0

No use you added $emails and not $email (without s ):

 use($emails, $description, $subject)

The right thing should probably be this:

function($message) use($email, $description, $subject)

So:

$send = Mail::send('email.email-multiple', $data, function($message) use($email, $description, $subject)
{
    $message->to($email)->subject($subject);
    $message->from('email@fantasia');
});

But if the body of the email and title are all the same, you could pass $emails (with "s") directly without needing foreach (by its code I suppose it is version 5.1 or 5.2):

public function sendEmail()
{
    $emails = $this->emailRepository->EmailByStatus();
    $description = 'teste';
    $subject = 'teste';

    $data = array('description' => $description, 'subject' => $subject);

    $send = Mail::send('email.email-multiple', $data, function($message) use ($emails, $description, $subject)
    {
        $message->to($emails)->subject($subject);
        $message->from('email@fantasia');
    });

    if(!$send)
    {
        return 0;
    }
    return 1;
}

If you do not want the recipients to see, then use Blind carbon copy or Blind copy

public function sendEmail()
{
    $emails = $this->emailRepository->EmailByStatus();
    $description = 'teste';
    $subject = 'teste';

    $data = array('description' => $description, 'subject' => $subject);

    $send = Mail::send('email.email-multiple', $data, function($message) use ($emails, $description, $subject)
    {
        $message->bcc($emails)->subject($subject);
        $message->from('email@fantasia');
    });

    if(!$send)
    {
        return 0;
    }
    return 1;
}
    
23.05.2017 / 21:51