【问题标题】:Laravel won't send email to multi receiverLaravel 不会向多个收件人发送电子邮件
【发布时间】:2020-01-06 08:36:31
【问题描述】:

我正在尝试发送多封电子邮件,但它返回此类错误:

预期响应代码为 354,但得到代码“550”,消息为“550 5.7.0” 未采取请求的操作:每秒发送的电子邮件过多”

代码

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

更多

我的$rereciverss 数据如下所示:

[{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}

基本上我需要发送电子邮件给他们发送电子邮件,但它返回我上面提到的错误。

有什么想法吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    根据你的错误说

    每秒发送的电子邮件过多

    您可以在电子邮件中添加一些延迟,然后重试或使用Job throttling 来处理这种情况。 看看sleep() 函数

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

    【讨论】:

    • 30 是什么?第二个??
    • @mafortis 是的,但这个数字取决于您使用的电子邮件服务。
    【解决方案2】:

    请试试这个。

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

    【讨论】:

    • 我喜欢验证电子邮件的想法,但它会发送像 To: <employee@employee.com, non-employee@employee.com> 这样的电子邮件标头,这不是我最喜欢相互分享用户电子邮件的方式。
    • 这不只是延迟将新电子邮件添加到$emails 数组,然后一次性发送它们吗?
    • 是的,延迟现在通过睡眠起作用,但如果我使用$emails[],它会立即发送所有用户的电子邮件,我更喜欢每个用户只看到他们自己的电子邮件,请参阅此处了解我的观点@987654321 @
    【解决方案3】:

    在这里使用队列是最好的决定并调度它。

    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

    【讨论】:

    • 对于电子邮件服务来说,队列工作人员仍然可能太快地运行作业。
    猜你喜欢
    • 1970-01-01
    • 2019-04-22
    • 2022-01-02
    • 2012-05-18
    • 2017-12-05
    • 2015-08-29
    • 1970-01-01
    相关资源
    最近更新 更多