How to send email simultaneously with Windows

Asked

Viewed 514 times

0

I need my system to simultaneously send emails to all the emails in the comic book. Following the code below it only sends the first email from the list and still sends wrong. Sends to spam. And the code msm I use in the contact form d 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;
}

Tbm tried it like this:

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

Tbm only sends the 1st and wrong tbm. And qnd sends.

What’s wrong with the code above?

  • How do I delete the question? kkkk Mr Admins, can close. I switched to from.

1 answer

1

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

 use($emails, $description, $subject)

This should probably be it:

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

Thus:

$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 equal, you could pass $emails (with "s") directly without needing the foreach (by your code I assume it’s 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 want the recipients not to see each other then use bcc (Blind Carbon copy or Hidden copy), thus:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.