Search code examples
emaildnsgoogle-appssendgridmx-record

Configure MX for SendGrid Inbound Parse and G Suite (Google Apps) on multiple environments


I have an app that runs on staging at stg.mydomain.com and production on mydomain.com. The core of the app is the ability for people to run surveys via email, where people can reply to the email with their answers. This is handled via SendGrid's Inbound Parse.

What I now want to do is configure Mailchimp so I can email my customers from something like [email protected], and get their replies there too. So I'm looking into setting up G Suite (formerly Google Apps) to do this.

I understand what I probably need to do is move Inbound Parse to a subdomain like mail.mydomain.com – but I'm not sure how this would work with the staging environment. Would it be mail.stg.mydomain.com?

Basically I need emails to [email protected] to come to my inbox, but emails to [email protected] or [email protected] to go to my app via SendGrid.

How do I configure my CNAME and MX records to make this work?


Solution

  • After a bit of trial and error and re-reading SendGrid's documentation, I ended up moving Inbound Parse to a subdomain (mail), which then freed up the root for Google Apps.

    Now I have this configuration in Cloudflare for MX:

    MX    mail            mx.sendgrid.net
    MX    mail.stg        mx.sendgrid.net
    MX    mydomain.com    aspmx.l.google.com
    MX    mydomain.com    ...l.google.com
    

    Now when I send an email from the app I include the mail subdomain and continue to use an environment variable for setting the domain name to either stg or the root.

    mail(to: email_address,
             from: "#{company} <questions@mail.#{Figaro.env.domain}>",
             reply_to: "#{uuid}@mail.#{Figaro.env.domain}")
    

    Lastly, in SendGrid, I changed the whitelabeled domain and the Inbound Parse configuration to match mail.mydomain.com.