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

aGandrass's avatar

SendGrid Webhooks Setup

Hi, I am trying to set up my SendGrid Webhooks for receiving all responses. With SendGrid sometimes you get two or more events per Webhook sent:

[ { "email": "[email protected]", "event": "processed", "send_at": 0, "sg_event_id": "cHJvY2Vzc2VkLTU3NzAyODgtNEJqQWpUb2lTc3V0Y21Ta0hsS0pCQS0w", "sg_message_id": "4BjAjToiSsutcmSkHlKJBA.filterdrecv-p3iad2-8ddf98858-sn8p2-20-5E8CE244-F.0", "smtp-id": "[email protected]", "timestamp": 1586291268 }, { "email": "[email protected]", "event": "delivered", "ip": "167.89.100.176", "response": "250 2.0.0 OK 1586291271 o59si2549636eda.39 - gsmtp", "sg_event_id": "ZGVsaXZlcmVkLTAtNTc3MDI4OC00QmpBalRvaVNzdXRjbVNrSGxLSkJBLTA", "sg_message_id": "4BjAjToiSsutcmSkHlKJBA.filterdrecv-p3iad2-8ddf98858-sn8p2-20-5E8CE244-F.0", "smtp-id": "[email protected]", "timestamp": 1586291271, "tls": 1 } ]

I have tried to use the following code, but now it saves 7 times (lines) the first array into my database:

public function handleSendgrid(Request $request) { foreach ($request as $value) { if(isset($request[0]["paymentuniqid"])) { $sg_payment_id = $request[0]["paymentuniqid"]; } else { $sg_payment_id = NULL; } if(isset($request[0]["sg_message_id"])) { $sg_message_id = $request[0]["sg_message_id"]; } else { $sg_message_id = NULL; } if(isset($request[0]["event"])) { $sg_event = $request[0]["event"]; } else { $sg_event = NULL; } if(isset($request[0]["category"])) { $sg_category = $request[0]["category"]; } else { $sg_category = NULL; } if(isset($request[0]["sg_event_id"])) { $sg_event_id = $request[0]["sg_event_id"]; } else { $sg_event_id = NULL; } if(isset($request[0]["response"])) { $sg_response = $request[0]["response"]; } else { $sg_response = NULL; } if(isset($request[0]["reason"])) { $sg_reason = $request[0]["reason"]; } else { $sg_reason = NULL; }

  $logbook = Logbook::create([
    '_paymentuniqid' => $sg_payment_id,
    'sg_event'       => $sg_event,
    'sg_category'    => $sg_category,
    'sg_event_id'    => $sg_event_id,
    'sg_response'    => $sg_response,
    'sg_reason'      => $sg_reason
  ]);
}

  \DB::table('offsessionpayments')
      ->where('paymentuniqid', $sg_payment_id)
      ->update([
        'paymentstatus' => $sg_event,
      ]);

http_response_code(200);

}

Does anybody know how to get every line from each array? Thank you Andre

0 likes
3 replies
aGandrass's avatar

I have tried a couple of other ways, but can't get it work out. As a response from a Sendgrid Webhooks I get this response:

[{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"processed","category":"cat facts","sg_event_id":"4zoQvwDKpfvYRMMUAN_0hQ==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"deferred","category":"cat facts","sg_event_id":"ysdEduSgoooB1jNjpNQ0PQ==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"400 try again later","attempt":"5"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"delivered","category":"cat facts","sg_event_id":"7nyjeK_8XRhRDnwdg-oBJw==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","response":"250 OK"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"open","category":"cat facts","sg_event_id":"WoZrxeI1EjT8AjOgCgANAA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"click","category":"cat facts","sg_event_id":"4gL6XuCeLAbMo7UlX_5OVg==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"bounce","category":"cat facts","sg_event_id":"2SKTPf2jApRcOYBqg16t-w==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"500 unknown recipient","status":"5.0.0"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"dropped","category":"cat facts","sg_event_id":"-vSk7y8kMVqE0d1GHFm-RA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","reason":"Bounced Address","status":"5.0.0"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"spamreport","category":"cat facts","sg_event_id":"_wKrPIj8JBgmcCCvwqW31g==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"unsubscribe","category":"cat facts","sg_event_id":"wFjlfA6ZZJf0IsoU7B1Sng==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_unsubscribe","category":"cat facts","sg_event_id":"ISVTQDh18E4yPkIJE_tkMA==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10},{"email":"[email protected]","timestamp":1586372566,"smtp-id":"\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e","event":"group_resubscribe","category":"cat facts","sg_event_id":"CYtS7YoDj4mTju1FzGZJ8Q==","sg_message_id":"14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0","useragent":"Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","ip":"255.255.255.255","url":"http://www.sendgrid.com/","asm_group_id":10}]

How can I get all "event" values. Should be 11 event values. Thank to you Have a great evening Andre

mix5003's avatar
mix5003
Best Answer
Level 46

change foreach ($request as $value) to foreach ($request->all() as $value)

and replace all $request[0] to $value

Please or to participate in this conversation.