简体   繁体   中英

Laravel won't send email to multi receiver

I am trying to send multi email but it returning such errors:

Expected response code 354 but got code "550", with message "550 5.7.0 Requested action not taken: too many emails per second"

Code

$reciverss = array_merge($admins, $workers); // finalizing my queries and joining two arrays in one

// getting input data
$details = array(
  'body' => $request->input('body'),
  'subject' => $request->input('subject'),
);

// getting receivers email addresses from my merged array and try to send input data to them as email
foreach($reciverss as $receive){
  Mail::to($receive->email)->send(new PICMAIL($details));
}

More

Here is how my data of $rereciverss looks like:

[{email: "admin@admin.com", username: "admin", userId: 1},…]
  0: {email: "admin@admin.com", username: "admin", userId: 1}
  1: {email: "employee@employee.com", username: "employee", userId: 2}
  2: {email: "non-employee@employee.com", username: "non employee", userId: 3}

Basically I need to get emails out to send them email but it returning error I mentioned above.

Any idea?

As per your error says

too many emails per second

you can add some delay in the emails and try it again or use Job throttling to handle this case. have a look at the sleep() function

foreach($reciverss as $receive){
  sleep(30);
  Mail::to($receive->email)->send(new PICMAIL($details));
}

Please try this.

#emails=[];
foreach($reciverss as $receive){
  sleep(30);
  $emails[]=$receive->email;
}
if(count($emails)){
  Mail::to($emails)->send(new PICMAIL($details));
}

use a queue is the best decision here & Dispatch that.

foreach($reciverss as $receive){

  MailQueueClass::dispatch(with_your_data);

}
<?php

namespace App\Jobs\Email;

use App\Mail\InvoiceCreated;
use App\Models\Order\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;

class SendInvoice implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private your_variable;

    /**
     * Create a new job instance.
     *
     * @param $order_id
     */
    public function __construct($your_variable1,$your_variable2)
    {
        $this->your_variable1 = $your_variable1;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
       Mail::to($your_variable1)->send(new PICMAIL($your_variable2));
    }
}

for details https://laravel.com/docs/5.8/queues

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