从 Mandrill Webhook 接收到的 JSON 字符串无效

Posted

技术标签:

【中文标题】从 Mandrill Webhook 接收到的 JSON 字符串无效【英文标题】:Invalid JSON String Received from Mandrill Webhook 【发布时间】:2015-05-23 15:49:06 【问题描述】:

从 Mandrill 收到的 JSON 字符串:

["event":"hard_bounce","_id":"5760ab383b1b4e358f10cfe759440dce","msg":"ts":1426810207,"_id":"5760ab383b1b4e358f10cfe759440dce","state":"bounced","subject ":"鸡尾酒会","email":"XXXXXX@XXX.com.XX","tags":[],"smtp_events":[],"resends":[],"_version":"Nes5DEa8tOq5Z6m_b9AWIA", "diag":"smtp;501 5.1.3 邮箱地址"XXXXXX@XXX.com.XX"(不可打印字符)中的语法错误","bgtools_code":10,"sender": "info-noreply@inviter.com","template":null,"bounce_description":"bad_mailbox","ts":1426822720]

更新:

这是从 mandrill 收到的另一个无效字符串

["event":"hard_bounce","_id":"cddb25d2023a486a89454d5c6cefc4c9","msg":"ts":1427904051,"_id":"cddb25d2023a486a89454d5c6cefc4c9","state":"bounced","subject ":"周年鸡尾酒会","email":"XXXXX@XXX.com","tags":[],"smtp_events":[],"resends":[],"_version":"CvawLhm-1KO4OY_FsZ3uSA" ,"diag":"smtp;550 No such User Here"","bgtools_code":10,"sender":"info-noreply@inviter.com","template":null," bounce_description":"bad_mailbox","ts":1427935354]

预期的 JSON 字符串:

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

验证 JSON 字符串 PHP 代码:

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

问题: 当我将 Received JSON 字符串发送到上述 php 函数时,它返回 FALSE。然后我发现JSON String With Double Quotes("diag":"smtp;501 5.1.3 Syntax error in mailbox address "XXXXXX@XXX.com.XX" (non-printable character)" )。

不仅仅是上面的字符串。我还有几个类似的字符串。例如,当用户在主题中给出双引号时,例如 (Hi "Madhu", How are you?) 我得到了同样的错误。

这是山魈的错误还是我这边的错误。如果是我这边,我该如何解决?

提前致谢。

【问题讨论】:

听起来像是 mandril 的错误。你自己对数组进行 json_encode 吗?或者曼德里尔会这样做吗?如果您从 mandril 收到此响应,那么这是他们系统上的错误。 @Jordy - 我从 mandrill 收到了这个字符串。我没有使用json_encode。 你能提供http请求和响应山魈吗?我同意@jordy,但在你写问题来支持之前,你应该确保它真的是 Mandrill 错误。 谢谢@Sergey Chizhik。我很确定它来自山魈。我已经发布了从山魈收到的回复。我的 Bounce webhook 正在发生这种情况。 【参考方案1】:

我可以这么说,这个 bug 来自 mandrill it self。我从山魈那里得到无效的 JSON。 “diag”键值内的双引号的主要问题。

这是我使用字符串替换的临时修复。

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

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

欢迎提出建议

谢谢。

【讨论】:

以上是关于从 Mandrill Webhook 接收到的 JSON 字符串无效的主要内容,如果未能解决你的问题,请参考以下文章

在 .NET 中验证 Mandrill 入站 Webhook 请求

Mandrill Webhook - 安全性

将 Mandrill JSON webhook 入站电子邮件附件映射到 C# 类

Google PubSub / Gmail Webhook:发送电子邮件时始终从 PubSub 接收多个 POST 请求

关于无法从开发者沙箱触发 Paypal REST API webhook 事件

如何在 .net 中处理 Mandrill WebHooks