Madhu Kudala
Madhu Kudala

Reputation: 13

Invalid JSON String Received from Mandrill Webhook

JSON String Received From Mandrill:

[{"event":"hard_bounce","_id":"5760ab383b1b4e358f10cfe759440dce","msg":{"ts":1426810207,"_id":"5760ab383b1b4e358f10cfe759440dce","state":"bounced","subject":"Cocktail Party","email":"[email protected]","tags":[],"smtp_events":[],"resends":[],"_version":"Nes5DEa8tOq5Z6m_b9AWIA","diag":"smtp;501 5.1.3 Syntax error in mailbox address "[email protected]" (non-printable character)","bgtools_code":10,"sender":"[email protected]","template":null,"bounce_description":"bad_mailbox"},"ts":1426822720}]

Update:

Here is one more invalid string received from mandrill

[{"event":"hard_bounce","_id":"cddb25d2023a486a89454d5c6cefc4c9","msg":{"ts":1427904051,"_id":"cddb25d2023a486a89454d5c6cefc4c9","state":"bounced","subject":"Aniversary Cocktail Party","email":"[email protected]","tags":[],"smtp_events":[],"resends":[],"_version":"CvawLhm-1KO4OY_FsZ3uSA","diag":"smtp;550 No Such User Here"","bgtools_code":10,"sender":"[email protected]","template":null,"bounce_description":"bad_mailbox"},"ts":1427935354}]

Expected JSON String:

[
    {
        "event": "hard_bounce",
        "_id": "5760ab383b1b4e358f10cfe759440dce",
        "msg": {
            "ts": 1426810207,
            "_id": "5760ab383b1b4e358f10cfe759440dce",
            "state": "bounced",
            "subject": "Cocktail Party",
            "email": "[email protected]",
            "tags": [],
            "smtp_events": [],
            "resends": [],
            "_version": "Nes5DEa8tOq5Z6m_b9AWIA",
            "diag": "smtp;501 5.1.3 Syntax error in mailbox address '[email protected]' (non-printable character)",
            "bgtools_code": 10,
            "sender": "[email protected]",
            "template": null,
            "bounce_description": "bad_mailbox"
        },
        "ts": 1426822720
    }
]

Validate JSON String PHP Code:

function validateMandrillJSONString($JSONString){
    $JSONString = json_decode($JSONString);
    if(is_array($JSONString)){
        if(empty($JSONString)){
            return FALSE;
        }
        else{
            return TRUE;
        }
    }
    else{
        return FALSE;
    }
}

Question: When I sent the Received JSON string to the above PHP function it's returning me FALSE. Then I found that there is an issue in JSON String With Double Quotes("diag":"smtp;501 5.1.3 Syntax error in mailbox address "[email protected]" (non-printable character)").

Not only The above string. I have few more strings similar to this. For example when user gave double quotes in the Subject like (Hi "Madhu", How are you?) I am getting the same error.

Is it a bug from mandrill or from my side. If it is from my side how can I fix this?

Thanks in advance.

Upvotes: 1

Views: 717

Answers (1)

Madhu Kudala
Madhu Kudala

Reputation: 13

I can say that, This bug is from mandrill it self. I am getting invalid JSON from mandrill. The main problem with double quotes inside the value of the key "diag".

Here is my temp fix using string replace.

//Data received from mandrill
$queueData = '[{"event":"hard_bounce","_id":"5760ab383b1b4e358f10cfe759440dce","msg":{"ts":1426810207,"_id":"5760ab383b1b4e358f10cfe759440dce","state":"bounced","subject":"Cocktail Party","email":"[email protected]","tags":[],"smtp_events":[],"resends":[],"_version":"Nes5DEa8tOq5Z6m_b9AWIA","diag":"smtp;501 5.1.3 Syntax error in mailbox address "[email protected]" (non-printable character)","bgtools_code":10,"sender":"[email protected]","template":null,"bounce_description":"bad_mailbox"},"ts":1426822720}]';

$queueData = str_replace(':""',':"""',$queueData); // Temp Fix
$queueData = str_replace('""','"',$queueData); // Temp Fix

Any suggestions are welcome

Thank you.

Upvotes: 0

Related Questions