Laravel不会将电子邮件发送给多个接收者。

huangapple go评论139阅读模式
英文:

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:

  1. [{email: "admin@admin.com", username: "admin", userId: 1},…]
  2. 0: {email: "admin@admin.com", username: "admin", userId: 1}
  3. 1: {email: "employee@employee.com", username: "employee", userId: 2}
  4. 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?

英文:

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

  1. $reciverss = array_merge($admins, $workers); // finalizing my queries and joining two arrays in one
  2. // getting input data
  3. $details = array(
  4. 'body' => $request->input('body'),
  5. 'subject' => $request->input('subject'),
  6. );
  7. // getting receivers email addresses from my merged array and try to send input data to them as email
  8. foreach($reciverss as $receive){
  9. Mail::to($receive->email)->send(new PICMAIL($details));
  10. }

More

Here is how my data of $rereciverss looks like:

  1. [{email: "admin@admin.com", username: "admin", userId: 1},…]
  2. 0: {email: "admin@admin.com", username: "admin", userId: 1}
  3. 1: {email: "employee@employee.com", username: "employee", userId: 2}
  4. 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?

答案1

得分: 3

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

  1. foreach($reciverss as $receive){
  2. sleep(30);
  3. Mail::to($receive->email)->send(new PICMAIL($details));
  4. }
英文:

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

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

答案2

得分: 0

请尝试这样做。

  1. #emails=[];
  2. foreach($reciverss as $receive){
  3. sleep(30);
  4. $emails[]=$receive->email;
  5. }
  6. if(count($emails)){
  7. Mail::to($emails)->send(new PICMAIL($details));
  8. }
英文:

Please try this.

  1. #emails=[];
  2. foreach($reciverss as $receive){
  3. sleep(30);
  4. $emails[]=$receive->email;
  5. }
  6. if(count($emails)){
  7. Mail::to($emails)->send(new PICMAIL($details));
  8. }

答案3

得分: 0

使用队列是这里的最佳决策,并进行调度。

  1. foreach($reciverss as $receive){
  2. MailQueueClass::dispatch(with_your_data);
  3. }
  1. <?php
  2. namespace App\Jobs\Email;
  3. use App\Mail\InvoiceCreated;
  4. use App\Models\Order\Order;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Queue\SerializesModels;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Mail;
  11. class SendInvoice implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. private your_variable;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @param $order_id
  19. */
  20. public function __construct($your_variable1,$your_variable2)
  21. {
  22. $this->your_variable1 = $your_variable1;
  23. }
  24. /**
  25. * Execute the job.
  26. *
  27. * @return void
  28. */
  29. public function handle()
  30. {
  31. Mail::to($your_variable1)->send(new PICMAIL($your_variable2));
  32. }
  33. }

有关详情,请查阅 https://laravel.com/docs/5.8/queues

英文:

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

  1. foreach($reciverss as $receive){
  2. MailQueueClass::dispatch(with_your_data);
  3. }
  1. &lt;?php
  2. namespace App\Jobs\Email;
  3. use App\Mail\InvoiceCreated;
  4. use App\Models\Order\Order;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Queue\SerializesModels;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Mail;
  11. class SendInvoice implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. private your_variable;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @param $order_id
  19. */
  20. public function __construct($your_variable1,$your_variable2)
  21. {
  22. $this-&gt;your_variable1 = $your_variable1;
  23. }
  24. /**
  25. * Execute the job.
  26. *
  27. * @return void
  28. */
  29. public function handle()
  30. {
  31. Mail::to($your_variable1)-&gt;send(new PICMAIL($your_variable2));
  32. }
  33. }

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

huangapple
  • 本文由 发表于 2020年1月6日 16:36:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定