aGandrass
6 months ago
1431
4
Laravel

SendGrid API - Get X-Message-ID after sending mail

Posted 6 months ago by aGandrass

Hi, I am trying to get the x-message-id out of the Sendgrid api response after successfully sending an email. That's the Sendgrid api code:

  $email ->setFrom("[email protected]");
  $email ->setSubject("Testmail via Sendgrid API");
  $email ->addTo($clientMail);
  $email ->addContent("text/html", "<strong>and easy to do anywhere, even with PHP</strong>");
  $email ->setClickTracking(true, true);
  $email ->setOpenTracking(true, "--sub--");
  $apiKey = config('services.sendgrid.api-key');
  $sg     = new \SendGrid($apiKey);
  try {
    $response = $sg->send($email);
    dd($response);
  } catch (Exception $e) {
  }

With the dd command I get the following response:

^ SendGrid\Response {#295 ▼ #statusCode: 202 #body: "" #headers: array:13 [▼ 0 => "HTTP/1.1 202 Accepted" 1 => "Server: nginx" 2 => "Date: Fri, 03 Apr 2020 20:48:36 GMT" 3 => "Content-Length: 0" 4 => "Connection: keep-alive" 5 => "X-Message-Id: TZISGRBpQemYzLPS7t9IHQ" 6 => "Access-Control-Allow-Origin: https://sendgrid.api-docs.io" 7 => "Access-Control-Allow-Methods: POST" 8 => "Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl" 9 => "Access-Control-Max-Age: 600" 10 => "X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html" 11 => "" 12 => "" ] }

I am trying to get the headers[5] x-message-id but without success. I have tried this command: dd($response->headers); But I get the following error: Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Cannot access protected property SendGrid\Response::$headers

Anybody an idea? thank you very much Andre

Please sign in or create an account to participate in this conversation.