How to setup Mailjet on exim for cPanel dnsonly on Google Cloud

Use a 3rd party company or setup your own email relay server

Google’s own documentation suggests a few 3rd party companies that will act as the middle man to deliver your emails. As of this writing: SendGrid, Mailgun and Mailjet are options.

This post is specific to the later of those providers. Mailjet offers a free tier plan that allows you to send up to 25,000 email transactions per month (signup here).

After you implement and configure it, which I will explain later in this post, you will get access to a dashboard to view your mail activity and review your logs.

Mailjet’s own documentation did not have any examples on how to configure cPanel’s exim mail transport agent, so I read the exim documentation to get an idea of what I would need to set this up.

Exim mail relay in the cPanel environment concentrates all of its settings in a file called exim.conf (/etc/exim.conf). In a normal cPanel fully fledged hosting server, you would have access to an “Exim Configuration editor” within the control panel itself. However, cPanel DNSonly is free and they took away this feature so I had to do all my edits manually which is okay, but if you are not comfortable logging into the server via ssh and editing files in VIM you may have a problem.

What EXIM needs to have in it’s configuration explained:

First we need to add the authentication credentials for the login. In /etc/exim.conf search for “begin authenticators” immediately below copy and paste my code and make sure to add your API key and password.

mailjet_login:
  driver = plaintext
  public_name = LOGIN
  client_send = : {YOUR_API_KEY_GOES_HERE} : {YOUR_PASSWORD}

Now we need to tell exim how to route mail and the rules to bound emails to (basically anything that is not local mail to use the new transport we will be creating on step 3). Search for “begin routers” paste my code and make sure to edit “in-v3.mailjet.com” with the server where your mailjet account got provisioned. You can obtain this from mailjet’s “configure my SMTP”

send_via_mailjet:
 driver = manualroute
 domains = ! +local_domains
 transport = mailjet_smtp
 route_list = "* in-v3.mailjet.com::2525 byname"
 host_find_failed = defer
 no_more

Now we need to create a new email “transport” to rely on mailjet for sending (aka be our middle man), search for “begin transports” and add the following…paste my code and make sure to edit “in-v3.mailjet.com” with the server where your mailjet account got provisioned

mailjet_smtp:
  driver = smtp
  hosts = in-v3.mailjet.com
  hosts_require_auth = <; $host_address
  hosts_require_tls = <; $host_address

Now its time to restart exim to load the changes. Restart the service, on CentOS 7.3 the command would be

systemctl restart exim.service

If you correctly followed the directions, adding the 3 needed sections to exim.conf and restarted the service now you should be able to try sending yourself a test email. You can do so from the command line like so:

# echo "Subject: desantolo rocks!" | sendmail -v [email protected]

The “-v” will make this transaction be verbose so you can troubleshoot it. You should see a successful email send to the mail relay mailjet server. Important: the email will not be in your inbox yet! You have to approve the sender (the email most likely was sent from [email protected]). I won’t get into the details of how to whitelist because on your mailjet.com dashboard you will see an error message that will say the same thing and point you to their documentation that is simple enough to follow to get your sender whitelisted (in this case [email protected])

Why this setup may not be good for you

Figuring this out was fun, but it also made me realize the drawbacks of depending on a 3rd party email provider like Mailjet, SendGrid or any others. During my testing mailjet.com wanted me to verify that I owned the domain that mail was being sent from, in this case the server’s hostname (recall my sendmail tests above – [email protected]).

Although for my purposes all my email notifications will be coming from [email protected] if you are thinking of hosting multiple domains, it may be better to deploy your own mail server relay instead of using 3rd party services like mailjet due to the security measures explained above and the involved process in getting them whitelisted (you need to upload a text file to your website root’s directory so they can make an HTTP request to verify you own or manage the domain).

One response to “How to setup Mailjet on exim for cPanel dnsonly on Google Cloud

  1. Pingback: Overriding Google Compute Engine hostname from getting reset | deSantolo.com

Leave a Reply