I am looping through email bodies that are nothing but JSON output. I am trying to grab a single string emailAddress, but I am unsure of the syntax. I am able to get to the Message portion of the JSON, but anything else I try to access, I end up getting Illegal string offset.
So, if my JSON is:
[Type] => Notification
[MessageId] => gibberishhere
[TopicArn] => arn:aws:somethingsomethingsomething
[Message] => {
"notificationType":"Bounce",
"bounce":{
"feedbackId":"blahblahblahblahblah",
"bounceType":"Permanent",
"bounceSubType":"General",
"bouncedRecipients":[{
"emailAddress":"[email protected]",
"action":"failed",
"status":"5.1.1",
"diagnosticCode":"smtp; 550 5.1.1 user unknown"}],
"timestamp":"2020-11-02T16:37:13.000Z",
"remoteMtaIp":"ip.address.here",
"reportingMTA":"dsn; somethingsomething"},
"mail":{
"timestamp":"2020-11-02T16:37:13.029Z",
"source":"[email protected]",
"sourceArn":"arn:somethingsomethingdotcom",
"sourceIp":"ip.address.here",
"sendingAccountId":"somethingsomething",
"messageId":"numbersnumbersnumbers1234567890",
"destination":["[email protected]"]
}
}
[Timestamp] => 2020-11-02T16:37:13.677Z
[SignatureVersion] => 1
[Signature] => blahblahblah
[SigningCertURL] => blahblahblah
[UnsubscribeURL] => blahblahblah
And I have this to decode it:
$message = json_decode($message, true);
echo $message['Message'];
I get this output:
{
"notificationType":"Bounce",
"bounce":{
"feedbackId":"blahblahblahblahblah",
"bounceType":"Permanent",
"bounceSubType":"General",
"bouncedRecipients":[{
"emailAddress":"[email protected]", <---- I NEED THIS FIELD
"action":"failed",
"status":"5.1.1",
"diagnosticCode":"smtp; 550 5.1.1 user unknown"}],
"timestamp":"2020-11-02T16:37:13.000Z",
"remoteMtaIp":"ip.address.here",
"reportingMTA":"dsn; e226-55.smtp-out.us-east-2.amazonses.com"},
"mail":{
"timestamp":"2020-11-02T16:37:13.029Z",
"source":"[email protected]",
"sourceArn":"arn:somethingsomethingdotcom",
"sourceIp":"ip.address.here",
"sendingAccountId":"somethingsomething",
"messageId":"numbersnumbers1234567890",
"destination":["[email protected]"]
}
}
All I need is the emailAddress field. How far in do I need to go to grab it? I've tried
$message['Message']['bounce'], $message['Message']['emailAddress'], and several others, but they all return Illegal string offset.
$arr = json_decode($message['Message'], true);. Then$arrshould be a normal PHP array where you can fetch the value you want.$arr['bounce']['bouncedRecipients'][0]['emailAddress']. Check if that works.