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

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

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?

英文:

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?

答案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

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

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));
}

答案2

得分: 0

请尝试这样做。

#emails=[];
foreach($reciverss as $receive){
  sleep(30);
  $emails[]=$receive->email;
}
if(count($emails)){
  Mail::to($emails)->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));
}

答案3

得分: 0

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

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));
    }
}

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

英文:

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

foreach($reciverss as $receive){

  MailQueueClass::dispatch(with_your_data);
  
}
&lt;?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-&gt;your_variable1 = $your_variable1;
    }

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

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:

确定