简体   繁体   中英

How to send email to multiple recipients laravel

I am trying to create an application where one of the features is to send email to multiple addresses, and it has not been easy. I used a foreach loop, but whenever an error occurs, most especially if the recipient mail address is not valid the application stops.

This is my code

public function sendemails(Request $request){

     $email = $this->emailSetUp($request);

     $emails = $email->recipients;
     foreach ($emails as $value) {
        try{
            $mail =  Mail::to($value)->send(new SendEmail($email));
        }catch( \Swift_RfcComplianceException $e){
            $pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
            preg_match_all($pattern, $e, $matches);
            $match = implode(',',$matches[0]);
            $mail_t_del = Email::where('email','LIKE', '%'.$match)->first();

            $mail_t_del->delete();
           Log::info($match .' '. 'deleted');

           $email = $this->emailSetUp($request);  
           $mail =  Mail::send(new SendEmail($email));      
       }
   }

}

How can i

  1. Send the message to multiple recipients and also make it fail proof, by continuing after the exception has been thrown.
  2. How can i track the progress of the application ie know the emails that has been sent and the on that has been rejected.

You can pass an array of emails.

see here for details

https://laravel.com/api/5.5/Illuminate/Contracts/Mail/Mailer.html#method_to

Also I suggest that you create a custom request to check to make sure that emails are valid before processing.

You can see how to do that here:

https://laravel.com/docs/5.7/requests

- Send the message to multiple recipients

pass all the emails in your mail class

Example:

 Mail::to($users)->send(new SendEmail($yourData));

put your codes in try catch

so that you can ignore exception and continue to send other mails

- Track the progress

If you want to get which mails are sent or not

then handle it on catch statement exception and log it in file or DB

To send an email to multiple recipients without displaying the sent email to all recipients, try this: This method will loop through the emails supplied for form or database. in this example the user IDs come from form requests, then, email is retrieved from the database for selected users.

foreach($request->get_emails as $i => $a){
        $user = User::find($a);
        Mail::send('emails.template', ['name' => $user['name'], 'body' => $request->body], function ($m) use($user) {
            $m->from('info@example.com', 'Sender Name');
            $m->to($user['email'], $user['name'])->subject('this is the subject');
        });
    }

Also, you may choose to display emails to all recipient. To do that, try:

    $emails = ['eamil1@example.com', 'email2@example.com','email3@example.com'];

    Mail::send('emails.template', [], function($m) use ($emails)
    {    
        $m->to($emails)->subject('This is the email subject');    
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM