0

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.

3
  • 1
    Looks like it was double encoded. Try decoding the message: $arr = json_decode($message['Message'], true);. Then $arr should be a normal PHP array where you can fetch the value you want. Commented Nov 2, 2020 at 20:32
  • That slightly helped, but I'm getting a big array of arrays. Commented Nov 2, 2020 at 21:34
  • Yes, that's what the data structure looks like. After decoding it, you should be able to access the value with: $arr['bounce']['bouncedRecipients'][0]['emailAddress']. Check if that works. Commented Nov 2, 2020 at 21:58

1 Answer 1

2

Working with an associative array is relatively easy, so as suggested in the comment:

$arr = json_decode($message['Message'], true);

Now all you need to do is reference the correct element, follow the path:

echo $arr['bounce']['bouncedRecipients'][0]['emailAddress'];

Which gives:

[email protected]

EDIT: How to reference element emailAddress - explain the [0] index

To get a good view of the array structure of $arr, you can use:

echo '<pre>';
print_r($arr);
echo '</pre>';

Which outputs:

Array
(
    [notificationType] => Bounce
    [bounce] => Array
        (
            [feedbackId] => blahblahblahblahblah
            [bounceType] => Permanent
            [bounceSubType] => General
            [bouncedRecipients] => Array
                (
                    [0] => Array
                        (
                            [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; e226-55.smtp-out.us-east-2.amazonses.com
        )

    [mail] => Array
        (
            [timestamp] => 2020-11-02T16:37:13.029Z
            [source] => [email protected]
            [sourceArn] => arn:somethingsomethingdotcom
            [sourceIp] => ip.address.here
            [sendingAccountId] => somethingsomething
            [messageId] => numbersnumbers1234567890
            [destination] => Array
                (
                    [0] => [email protected]
                )

        )

)

Now all you need to do is follow the 'path' to the element you'd like to reference. I.e. for emailAddress let's take the reverse 'route':

emailAddress lives in an array with key [0] (there you have it, key [0])
key [0] lives in an array with key [bouncedRecipients]
key [bouncedRecipients] lives in an array with key [bounce]
key [bounce] lives in the root of array $arr.

So there you have it, the path to your element:
['bounce']['bouncedRecipients'][0]['emailAddress']

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much. I was almost there. I just forgot the [0].
@berend Would it be too much trouble to explain the [0] index, so I fully understand? Thanks.
@IRGeekSauce added a small explanation, hope that helps, take it easy!
Perfect explanation. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.