简体   繁体   中英

Sendgrid parse not working, the json is always empty

I am trying to parse email send to a domain and then dump it to database via sendgrid inbound parse webhook. For this I am referencing https://sendgrid.com/docs/for-developers/parsing-email/setting-up-the-inbound-parse-webhook/link .

What I have done so far:

1) create a mx record in domain:

email.domainname.com.   14400   IN  MX  Priority: 10
                                        Destination: mx.sendgrid.net

2) Sendgrid: Setting -> Inbound parse -> Add host and url. It ask me to verify domain first. So, I did as it ask

//subdomain - domain
email domainname.com
//checked default whitelabel and automated security is on

Then successfully verified the domain. 3) Finally, add host and destination url

//subdomain - domain
empty domainname.com
//destination url
http://www.domainname.com/jin/parse.json
//checked both options (check for spam and post raw data)

Now, whenever I send email to info@domainname.com It does not write any data at parse.json file. Am I in the right direction? Upto this point, I am supposing that data will be written to that parse.json file. Or Am i need to write extra code in php somewhere to catch that? Please, let me know what I am missing. Any helpful link or any direction to get this working.

Update:

It looks like I am starting to receive post data to the desired destination but don't get how to parse this email part:


"email":"Received: by mx0129p1mdw1.sendgrid.net with SMTP id eHtBZW56Gk Sat, 16 Feb 2019 06:33:24 +0000 (UTC)\nReceived: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by mx0129p1mdw1.sendgrid.net (Postfix) with ESMTPS id 9791F8664E2 for <email@domain.com>; Sat, 16 Feb 2019 06:33:24 +0000 (UTC)\nReceived: by mail-ed1-f48.google.com with SMTP id h58so9639427edb.5 for <email@domain.com>; Fri, 15 Feb 2019 22:33:24 -0800 (PST)\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed\/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=u7geNXNTSgSScusvNgz8ieJB7\/U4yjCfRB1gcO3nu4w=; b=XBkSHrHfxFUxG2oLm1btnV1ReETBlzSB5+qsRsEDRXjyCnG37jKWyguEnzvfy4BFqr pBpINHNBItlxBsrOb2YMEWfFo9pXGpUJIGUsGG6NbXiNARItV96EpmFL34e51myWm39O DyOX0Dsk9sYJxwXthELcm9ey91rZvOHUpb\/hi74Fo7O\/IPZiSvBnlpT+ekBqy3wPguuq 7GipFYWQpym\/4YPsFeJjDHI0LfwJHrKTz7gQxi93e7saRPS4bpJmoG73CrpHuckZDIYc hZcQy2i\/ZJRP7RB\/iwgVWN8lKr66vY8od6YuN1LgXzRKCKEva6+KRDmnefZeXoXuq0Q3 fcaQ==\nX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed\/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=u7geNXNTSgSScusvNgz8ieJB7\/U4yjCfRB1gcO3nu4w=; b=H1rtTSirT9cm5PUZbn+dTbvkzA7bTighuUk6TMB3YAJEzRmU0UdopwaadGeEDnYfFM eJEt4HQ2R0RFwsqIbuV4wH2mKI2+tgjnVt4IxabyjGZFqO+F\/AG0LO8jKENQ6YfUhFdU j2JtlAR0C8eviUslAJsd6qcQh7U0VL3ZHDgCjiwDywAn56iinIEcAXEmwvp0Eae4EPET 1chJgpa2mZvmr4XQLGOPzcQkiF8sU3V3GtDQ34iT8HXx0eW1fg4VknSJV6X7uCqD9v33 VWDUyKGkRhbPZFwOmcEN9x0y94i2C8i+AZySQpTWdFHk46GxChpTjo9v7ndvpC4bi+Iy lteg==\nX-Gm-Message-State: AHQUAubsqSNqLHwm0GRRSzhRktzsmb5g2r3Ych6v15dNuFGIX7KkqvjN 5MDlXdyoY119vrCRn95FmmT6mVHp+ZJCV9R1rHotvA==\nX-Google-Smtp-Source: AHgI3IY2AJhyYFw7IzYhufYBq6sv3ckH36J1fox71nzUqqSmqmDOEUJrD6qUTWUSCOk9QxT3FyhGClTjzBmHWLNS21E=\nX-Received: by 2002:a50:98c5:: with SMTP id j63mr10306468edb.273.1550298803271; Fri, 15 Feb 2019 22:33:23 -0800 (PST)\nMIME-Version: 1.0\nFrom: Jon Doe <sender@gmail.com>\nDate: Sat, 16 Feb 2019 12:18:10 +0545\nMessage-ID: <CACX9xt=O4jQDJ8AWcUVxLti=gwFipaYV9NV7hpGQOkZx6FfYjQ@mail.gmail.com>\nSubject: Test Header\nTo: email@domain.com\nContent-Type: multipart\/alternative; boundary=\"00000000000076fcf90581fd11c8\"\n\n--00000000000076fcf90581fd11c8\nContent-Type: text\/plain; charset=\"UTF-8\"\n\nTest Message\n\n--00000000000076fcf90581fd11c8\nContent-Type: text\/html; charset=\"UTF-8\"\n\n<div dir=\"ltr\">Test Message<\/div>\n\n--00000000000076fcf90581fd11c8--\n",

I have recently (last few months) set up our own inbound parse, an important note to handle is the correct MX set-up:

When setting up your MX record, it is to specify what domain name your email will be using. If you expect to receive email from info@domainname.com, then your MX record needs to show as domainname.com (not mail.domainname.com).

Note - the rest of your MX record appears correct.

You can test if the post works correctly by using a Web Api testing tool that has always proved helpful in the past: https://requestbin.fullcontact.com/ .

When you do use this tool (it will provide you with your own unique Url to post to), see if you can pass a basic string in the body of the post. If the tool shows your post, along with the string that you passed, then you know your post is working. This is the first step I would take in diagnosing where the post is failing. Once this is working, and you are able to see all of the post data (including headers, parameters, and posted data) then figuring out how to make the rest work should be relatively straightforward. Let us know how this goes.

Edit (2/16/19): 2 Suggested approaches on how to validate if the Inbound Parse post is being received by your Web API, and if it is able to parse the data:

First Suggestion:

  1. Create your Web API PHP page that will receive the Inbound Parse post (eg myPhpPageToReceiveInboundParse.php.)

  2. Provide your Web API URL to SendGrid for the Inbound Parse Host. Based on the above example, your Web API URL would be: https://yourDomain.com/myPhpPageToReceiveInboundParse.php .

  3. Note: To keep this example as simple as possible, do not check the SendGrid Inbound Parse "Additional Options" (eg do not check the "Check incoming emails for spam" or "Post the raw, full MIME message").

  4. Copy/Paste this code for your newly created Web API Page:

     $url = 'http://requestbin.fullcontact.com/xxxxxxxx'; $myvars = 'myvar1=' . $to . '&myvar2=' . $from. '&myvar3=' . $subject . '&myvar4=' . $body; $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_POST, 1); curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars); $response = curl_exec( $ch ); 
  5. Note: In the above code, you will need to substitute your own unique identifier (in "xxxxxxxx") for your requestbin URL for this line: $url = ' http://requestbin.fullcontact.com/ xxxxxxxx '; (eg http://requestbin.fullcontact.com/123a4b56 , assuming your unique identifier is "123a4b56).

  6. Send yourself an email. Remember to send it using the "Receiving Domain" you selected when setting up your Inbound Parse in SendGrid. (eg test@yourDomain.com).

  7. Check your requestbin posts (eg http://requestbin.fullcontact.com/123a4b56?inspect )

Result: You should see the $myvars fields under the FORM/POST PARAMETERS in requestbin.

If you see this, you know that the Inbound Parse post from SendGrid is being received by your Web API page, and that you are able to parse the data coming from SendGrid.

Second suggestion: Review the suggested PHP code in SendGrid's documentation: https://sendgrid.com/docs/for-developers/tracking-events/php-code-example/ . This will help guide the code your write for your Web API page.

====== END OF EDIT (2/16/19)

See if this corrects the issue, and if it does not, please provide additional set-up information related to you are setting up your web api. Otherwise, you should start seeing data being received. Good luck.

Are you sending mail to domainname.com , or to gmail.com ? If you're sending to gmail.com , it will go to GMail's servers, not SendGrid's. As long as you're sending to email@domainname.com , and there's an MX record on domainname.com pointing to sendgrid.net , and you have the active Parse config, it should be getting picked up by SendGrid.

The other half is making sure you have your parser setup right. You mentioned looking for JSON data; the Sendgrid Parse documentation specifically says:

parses the contents and attachments then POSTs multipart/form-data to a URL that you choose

Do you have your endpoint setup to receive & parse the multipart/form-data , with either all the fields named as defined in the SendGrid documentation or otherwise set up to accept everything ? Sendgrid isn't sending you JSON data, so if you're only looking for JSON in the POST body, you won't see anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM