Search code examples
emaildnssendgrid

SendGrid - Send mails from customers own domain


I'm working on a product where we use SendGrid.com so send system e-mails to our customers' end users. These e-mails origniate from our own domain - let's call it ourdomain.example. This is done by going through SendGrid's authenticated domain flow to set up DNS records to validate the domain.

Several of our customers have asked if we can send the system e-mails from their own domain, e.g. they would like if e-mails sent from the system was sent on behalf of @customerdomain.example.

The question is - how do I set this up in SendGrid so that we can deliver DNS settings to the customers?

I really don't want this to be a manual process as we might have hundreds of customers who want to use their own domain. I've tried reaching out to SendGrid support, but they basically keep linking to this page: https://docs.sendgrid.com/ui/account-and-settings/how-to-set-up-domain-authentication. This is what we've done for our own domain, but this isn't really a viable solution if we need to handle hundreds of domains from different customers.

Does anyone know if the process can be automated via the SendGrid API? Something like this perhaps:

  • The customer creates an account with us (domain: customerdomain.com)
  • We call SendGrid's API saying "create domain validation for domain customerdomain.example"
  • We get back the DNS entries the customer (owner of customerdomain.example) needs to enter into their DNS setup
  • We start sending e-mails with the From address set to [email protected]

Maybe I'm looking in all the wrong places, but I simply can't figure out how to do this the right way.

Any help will be greatly appreciated!


Solution

  • After trying a few different approaches, we eventually got this working by using the SendGrid API in the following manner:

    1. For each custom domain, authenticate this using the Domains API (https://docs.sendgrid.com/api-reference/domain-authentication/authenticate-a-domain) This will give you a domain ID as well as a DNS object containing the CNAME and DKIM values to set for the domain. Ask your customer to apply these to the domain's DNS settings.
    2. Once the DNS changes have been applied, verify them by calling the Validate method on the domains API: https://docs.sendgrid.com/api-reference/domain-authentication/validate-a-domain-authentication

    If the domain was successfully validated, you can then use the domain as a sender domain through SendGrid.

    I hope this helps someone else experiencing trouble with this.