Implementing SendGrid with Exim on cPanel

5/5 - (16 votes)

Updated 3/9/21

Setting up your cPanel server to send through a third-party mail server is very easy to do, if you understand the basics of how cPanel builds its Exim configs. You never want to edit your exim.conf file directly – your changes will be wiped out any time a cPanel update runs or someone makes a change via the Exim Configuration Editor in WHM.  Below is a quick guide on setting up a custom mail router with a provider like SendGrid, which in turn will route all outbound email through the external mail service.

The below instructions cover how to do this via command line.  If you prefer WHM, simply go to WHM -> Exim Configuration Manager -> Advanced Editor and alter the sections indicated.

First, open up your /etc/exim.conf.local file in an editor and look for the @AUTH@ section.  Modify it to look like this:

@AUTH@
sendgrid_login:
driver = plaintext
public_name = LOGIN
client_send = : apikey : <API_KEY>

Of course, replace <API_KEY> with the API key you generated in your Sendgrid account (under Settings).  If you already have something in the AUTH section, simply add this block of text below it.

Now look for the @PREROUTERS@ section, and modify it to look like this:

@PREROUTERS@
send_via_sendgrid:
driver = manualroute
domains = ! +local_domains
transport = sendgrid_smtp
route_list = "* smtp.sendgrid.net::587 byname"
host_find_failed = defer
no_more

The last modification should be to the @TRANSPORTSTART@ section:

@TRANSPORTSTART@
sendgrid_smtp:
driver = smtp
hosts = smtp.sendgrid.net
hosts_require_auth = smtp.sendgrid.net
hosts_require_tls = smtp.sendgrid.net

Now save the file, and apply the changes:

/scripts/buildeximconf

service exim restart

To test whether things are working, send an email out from your server, and look for it in /var/log/exim_mainlog. You should see something like this in your log entry:

2013-10-08 19:37:29 1VTjeS-0000Ac-O3 -> my@email R=send_via_sendgrid T=sendgrid_smtp H=smtp.sendgrid.net [x.x.x.x] X=TLSv1:DHE-RSA-AES256-SHA:256

If you have SPF records, you’ll need to add the hostname of the SMTP server to the record itself to allow the third-party mail server to send email on behalf of your domain.  Sendgrid will provide the hostname you should use.

24 Comments

  1. Pingback: Implementing Mandrill with Exim on cPanel :: The cPanel Admin

  2. Gaurav Reply

    What if multiple user want to apply their own Sendgrid setting for their accounts while many of them want to stick with default option from WHM?

    Waiting for your answer 😉

  3. eve Reply

    I’m getting a error on the log that says.
    “no IP address found for host smtp.sendgrid.net”
    and
    “lookup of host “smtp.sendgrid.net” failed in send_via_sendgrid router”

    I guess is for the SPF records, am I right?

      1. eve Reply

        Hi, I forgot to say I’m using google cloud. In spite of that I found what was wrong.
        The port should be 2525 instead of 587 because of google cloud firewall rules.

  4. Pingback: Bypass SMTP Relay For A Single Domain In Exim 4 - [ Insert Title Here ]

  5. Ben Walandow Reply

    Hello, im so thankful for your great tutorial..

    Im facing a problem right now:
    All emails sent from outlook not saved in “Sent Item” Server..
    *Checked Outlook Settings> Options> Mail > Save copies of messages in the Sent Item Folder*

    If i try to send from webmail, all great, work properly..
    The email was sent, and theres a copy in sent item..

    But when im setting the mail in my Outlook,,
    The email was sent successfully, but not in sent item server..
    *Checked My outgoing server (SMTP) required authentication*

    Theres no problem in receiving email in webmail or outlook..

  6. Pingback: Implementing Mandrill with Exim on cPanel - The cPanel Admin

  7. John Reply

    Hey, about your tutorial, if i have about 10domains on my WHM and i want the just 3 or 4 this domains is allow to send email using the sendgrid SMTP, how i implement

    the ( senders = *@ ) to use this 4domains only?

    How i do this, put the 4domains there and works fine?

    Another question, i put inside the or remove the and put : *@YourDomainHere

    ?
    Thanks!

  8. Jon H Reply

    How do we make it so mail sent through the web forms across all accounts on the server go through sendgrid? The info you have works for mail accounts in email clients, but php still sends emails from web forms.

    1. Vanessa Vasile Reply

      By default, the instructions in this guide will send all email from Sendgrid. Keep in mind that if email from your web forms are being sent to an email address on the same server as the form, it will not use any sort of relay – it will deliver locally on the server.

  9. BRENT A OSWALD Reply

    I didn’t see a crucial step: editing /etc/exim.conf.localopts to set the server to use sendgrid as a smarthost. This may be something new since the original documentation is very old.

Leave a Reply

Your email address will not be published. Required fields are marked *

Log in