0

I am using Mailtrap for testing. I have this array that contains the email address (recipient) and unique data.

array:3 [
  0 => array:3 [
    "email" => "test1@email.com"
    "report" => "Report 1"
    "count" => "20"
  ]
  1 => array:3 [
    "email" => "test2@email.com"
    "report" => "Report 3"
    "count" => "10"
  ]
  2 => array:3 [
    "email" => "test3@email.com"
    "report" => "Report 4"
    "count" => "0"
  ]
]

Here's what I got so far. The array is stored in $items variable.

foreach ($items as $item) {

            Mail::send('emails.test', [ 'item' => $item ], function ($m) use($item) {

                $m->bcc('test0@mail.com');
                $m->to($item['email'])->subject($item['report']);

            });

        }

It sends the first 2 reports, but I get an error "too many emails per second". How can I avoid these error? or Is there a better approach?

Toffer
  • 79
  • 2
  • 12
  • This question already has answer [refer this link ](https://stackoverflow.com/questions/35304197/laravel-email-with-queue-550-error-too-many-emails-per-second) – Pranav Patel Jul 09 '19 at 08:30
  • Is the error a php exception? You could just add a try catch with a small timeout and then retry sending. – Shardj Jul 09 '19 at 08:32
  • As the mailtrap pricing page says: With the free plan you can only send 2 mails per 10 seconds. You will have to wait 10 seconds after every two mails. – LLJ97 Jul 09 '19 at 08:34
  • Just add `sleep(5)` inside your loop? – Don't Panic Jul 09 '19 at 09:15
  • @Don'tPanic I guess the only option for me is to add a delay. – Toffer Jul 09 '19 at 10:36

1 Answers1

1

As Mailtrap says, the free plan only allowes 10 emails to be received every 10 seconds.

https://mailtrap.io/pricing

You need to wait 10 sec before you send the next 2.

Patrick Schocke
  • 1,493
  • 2
  • 13
  • 25