user2788110
user2788110

Reputation: 45

Grab Custom Arguments from SendGrid Event Webhook PHP

I'm trying to pull custom arguments when I send email through SendGrid and pull them into my database when they are saved through Event Notification.

I have event notification setup so that after SendGrid processes email, it saves it into my DB, I just can't figure out how to pull the custom arguments I'm sending.

I send the argument like this:

$mail->addCustomArg("campaign", "welcome69");

And then am running into issues trying to receive it (after SendGrid posts it)

$data = file_get_contents("php://input");
$events = json_decode($data, true);

foreach ($events as $event) {
$sg_event_id = $event['sg_event_id'];
$unique_args = $event['unique_args'];
}

In the bottom code snipet, the variable $unique_args works, the second one does not. Any help is greatly appreciated.

Upvotes: 1

Views: 1367

Answers (1)

Oluwafemi Sule
Oluwafemi Sule

Reputation: 38922

What Sendgrid POSTs to your hook is a JSON Object. You can retrieve unique_args directly by writing something like

$unique_args = $events['unique_args']; // this is an associative array.

You can walkthrough the associative array using a foreach loop this way:

foreach($unique_args as $field => $value) {
}

Upvotes: 2

Related Questions