How to get Sendgrid Inbound Parse Webhook working in rails app on production?

Viewed 557

I’m trying to build a rails app that will process inbound mail. I got the app to work on my localhost machine using the Rails conductor and action mailbox. When an email gets sent, I’m able to save the contents of the email. But I’m having difficulty getting it to work on a production environment…I’m not sure how to configure my domain and settings to get it to work.

I’ve been following the instructions here: https://edgeguides.rubyonrails.org/action_mailbox_basics.html#sendgrid and https://sendgrid.com/docs/for-developers/parsing-email/setting-up-the-inbound-parse-webhook/

I included this in my rails credentials:

action_mailbox: ingress_password: mypassword

I have set up an MX record on google domains:

parse.[mydomain].com

enter image description here

I pointed to a Hostname and URL.

https://actionmailbox:mypassword@parse.[mydomain].com/rails/action_mailbox/sendgrid/inbound_emails

I send an email from my email account to

parse@parse.[mydomain].com

but I’m not able to test or track what is happening to this email. I don’t receive an error message back to my email as a reply, so I think that’s a good sign but I’m not sure whether it’s being processed or how to troubleshoot. I even put a puts ‘test’ in my replies_mailbox.rb file but I don’t see anything in the console when I tail logs on production.

Any advice on what next steps I can take?

1 Answers

When dealing with integration testing it's useful to split the issue into smaller ones, in order of email path

  1. Check if mx dns record has propagated, usually when you edit your zone - other dns servers may still respond with old records until zone TTL passes (it is usually set to several hours), use some remote dns checker
  2. Check sendgrid settings (including "Post the raw, full MIME message" which is expected by actionmailbox, so that sendgrid posts 'email' field)
  3. Check if the email is being dropped by spam filter in sengrid
  4. check if the request is present in your web server/reverse proxy logs (like nginx, if you use one)
  5. Try mimicking sendgrid's request to check if your app is accepting it (and if it is in logs), rails only reads params[:email], other fields are not necessary:
    curl -X POST "https://actionmailbox:mypassword@parse.[mydomain].com/rails/action_mailbox/sendgrid/inbound_emails" \
         -F email="From: foo <abc@localhost>\nTo: bar <bca@localhost>\nSubject: test\nMIME-Version: 1.0\n\nTest!"
    

I'd start with #5, to be sure your app is accepting email correctly and has logs, and then go up.

PS. puts might not appear in logs in production (or not where you expect it to appear) depending on you logging setup. Better way is to use Rails.logger.info

Related