Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aGandrass's avatar

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

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

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

There is probably an accessor.

try dd($response->headers());

aGandrass's avatar

Thank you Snapey, it works! :) Now I get the following response:

array:13 [▼ 0 => "HTTP/1.1 202 Accepted" 1 => "Server: nginx" 2 => "Date: Sat, 04 Apr 2020 17:43:14 GMT" 3 => "Content-Length: 0" 4 => "Connection: keep-alive" 5 => "X-Message-Id: 6T_EJ2NWQ7iEJcMJUrL0Wg" 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 have tried to access array 5 to get X-Message-ID. But instead of just getting the id itself, I get this: "X-Message-Id: 6T_EJ2NWQ7iEJcMJUrL0Wg". That's the entire value inside the array 5.

Do you know how I can get only the ID itself? Thanks again Andre

Snapey's avatar

just split it off with string helpers

BUT the worry is that its not forced to always be header 5, unless you find a better way, you should walk through the headers until you find one that starts X-message-ID

Please or to participate in this conversation.