Saliou MBALO
Saliou MBALO

Reputation: 169

Send Mail laravel to multiple recipients by using foreach

Good evening everyone, I have some issues in my project. I'm trying to send mail to each collaborator who celebrates his birthday. The data are taken from my database. The problem is that: if I have for example 2 or 3 collaborators who celebrate their birthday in the same day, only the first in the list receive a mail. My question how send to all concerned collaborator a mail. My code:

public function handle()
{
    $collaborateurs = Card::listCard();

    foreach ($collaborateurs as $collaborateur) {
    return  Mail::to($collaborateur->adresse_email)->send(new SendEmail($collaborateur));

    }
}

class CarteAnnif {

public static function listCard(){

    $collaborateurs = Organigramme::whereMonth('date_de_naissance', now()->month)
                    ->whereDay('date_de_naissance', now()->day)
                    ->get();

    $collaborateursConcernes = [];

    foreach ($collaborateurs as $collaborateur) {

        $date_de_naissance = Carbon::createFromFormat('d/m/Y', $collaborateur->date_de_naissance)->format('d-m');
        $date_de_naissance = strtotime($date_de_naissance);

        $today = date('d-m');
        $today = strtotime($today);

        if($date_de_naissance == $today ){

            $collaborateursConcernes[] = $collaborateur;
            
        }
    }
    
    return collect($collaborateursConcernes);
}

}


public $collaborateur;

public function __construct($collaborateur)
{
    $this->collaborateur = $collaborateur;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    $this->view('admin.emails.send_card_to_collaborateurs')
         ->subject("Joyeux anniversaire");

    return $this->from('[email protected]')->view('admin.emails.send_card_to_collaborateurs');
}

}

I hope I have been clear otherwise I remain available for any other additional information Thanks in advance!

Upvotes: 0

Views: 663

Answers (1)

STA
STA

Reputation: 34838

The return ends a function, so your function will stop after the first loop. Just delete the retun :

foreach ($collaborateurs as $collaborateur) {
      Mail::to($collaborateur->adresse_email)->send(new SendEmail($collaborateur));
}

Upvotes: 1

Related Questions