【问题标题】:Laravel, how to record into DB if email wasn't delivered?Laravel,如果未发送电子邮件,如何记录到数据库中?
【发布时间】:2021-08-06 12:20:21
【问题描述】:

我有一个功能,可以在每次新注册时向客户发送电子邮件。并且我的客户想要记录发送的任何不成功的电子邮件(即如果电子邮件地址错误)。

我尝试使用 try-cacth 和 mail::failure。

 try{  
      Mail::send('mail.mail', compact(''), function ($message) use ($) {
                $message->from();
                $message->subject();
                $message->to();
            });
    }
    catch(\Swift_TransportException $e){
       
    }

邮件:失败

 if (Mail::failures()) {
     echo ('error');
 }

但如果网络故障,例如电子邮件主机已关闭,则此功能有效。如果由于找不到电子邮件地址/域而未发送电子邮件,如何记录/获取信息。

如果电子邮件未送达,我会在电子邮件中收到此信息。但是如何将ddetails记录到数据库中。

Address not found
Your message wasn't delivered to asna@asmas.cl because the domain asmas.cl couldn't be found. Check for typos or unnecessary spaces and try again.

【问题讨论】:

标签: php laravel email


【解决方案1】:

看看我下面的代码在发送短信后得到一个json响应,你的系统在发送电子邮件后也应该得到一个响应json。 json 携带一组数据。检查您希望的特定数据响应并在您的表格上相应地更新它。

      $responseJson = json_decode($response, true);
//    data returned --> [{"status":"200","response":"success","message_id":82682342,"recipient":"4113122"}]

      $status = ($responseJson[0])['status'];
      $responseDescripition = ($responseJson[0])['response'];

      Log::info('API status >>>>> ' . $status);
      Log::info('API Response Description >>>>> ' . $responseDescripition);

 if (($responseJson[0])) {
       DB::table('mess')->where('id', $request->input('template'))
               ->update([
               'sentstatus' => '1',
               'status' => $status,
               'responsecode' => $responseDescripition,
               'sent_on' => \Carbon\Carbon::now(),
               ]);
  }

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 2017-12-30
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多