Implementing SendGrid with Exim on cPanel
- Written by Vanessa Vasile
- Published in Howto, Mail
- 24 Comments
- Permalink
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
Pingback: Implementing Mandrill with Exim on cPanel :: The cPanel Admin
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 😉
You could probably store their creds in a file and load it from exim.conf
Hello Vanessa,
Do you have an example of how I would do this please?
Not currently but we will take a look at this and update the article.
Wonderful, thanks for this article, worked nicely!
What hostname should i add on my SPF records after do this configuration?
The hostname of your server.
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?
From your server, see if you can ping smtp.sendgrid.net.
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.
You rock! I was pulling my hair. After I saw your post I remembered reading about this. Thanks!
It is possible to place the chuck and sendgrid together?
sending random emails?
Can you clarify your question please
Pingback: Bypass SMTP Relay For A Single Domain In Exim 4 - [ Insert Title Here ]
I have several domains on the server. I just want one server to use sendgrid. Would I just add senders *@.com?
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..
Pingback: Implementing Mandrill with Exim on cPanel - The cPanel Admin
If like me, you are using this for all your domains on your WHM server, the part missing is how to add SPF to all of your domains without having to manually configure them all.
I found this really helpful, from cPanel:
https://forums.cpanel.net/threads/external-mail-server-and-spf.207622/
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!
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.
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.
can you please tell how to make it work with Two Factor Authentication enabled. It is giving following error:
sendgrid_login authenticator failed H=smtp.sendgrid.net [167.89.115.18] 535 Authentication failed: Basic authentication is not allowed with 2FA enabled. To fix, see https://sendgrid.com/docs/for-developers/sending-email/authentication/#basic-authentication.
Waiitng for your support
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.