SendGrid SMTP on cPanel/WHM: the Username Is ‘apikey’

Updated

configure domain using sendgrid

The SMTP username is the literal five-character string apikey and the password is a SendGrid API key. It is not your SendGrid login. Twilio SendGrid’s SMTP integration guide says it in one line: “Set your username to the string apikey. This setting is the exact string "apikey" and not the API key itself,” and “Set your password to the API key generated in step one.”

Putting your SendGrid username and password into Exim’s AUTH section gives authentication failures. Likewise, senders is an option on the manualroute router, which sits in PREROUTERS, not in “Section: POSTMAILCOUNT”. The blocks below show both.

Check the Basic Editor first

As of August 2026, cPanel documents a smarthost path that does not require hand-editing Exim. WHM’s Exim Configuration Manager docs: “You can use the Basic Editor to manage smarthost configuration for domains that use the same authentication information. If you used the Advanced Editor in the past for this, you may be able to use the Basic Editor now.” Try that before the block below. cPanel’s warning about the Advanced Editor is blunt — changes “can significantly impact Exim’s performance, and may render Exim nonfunctional” — and edits made straight to /etc/exim.conf “will only work temporarily”, because the file is rebuilt from /etc/exim.conf.local.

Advanced Editor blocks for the SendGrid relay

Section: AUTH. Note the leading hide and the literal apikey:

sendgrid_login:
  driver = plaintext
  public_name = LOGIN
  hide client_send = : apikey : SG.your-api-key-here
  client_condition = ${if def:tls_out_cipher}

hide is what cPanel’s own multi-domain smarthost example uses, and it matters. The Exim specification says that with -bP, “any option setting that is preceded by the word "hide" in the configuration file is not shown in full, except to an admin user. For other users, the output is as in this example: mysql_servers = <value not displayable>”. Without it, your API key is readable from a config dump. The leading colon in client_send is not a typo: Exim’s plaintext authenticator documents it as “the first string is empty, so no data is sent with the AUTH command itself,” and recommends client_condition = ${if def:tls_out_cipher} so the mechanism only fires on an encrypted connection.

Section: PREROUTERS — this is where senders lives, and the only place to restrict the relay to one domain:

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

Section: TRANSPORTSTART — keep hosts_require_tls. The measurement below is why:

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

What the SendGrid SMTP endpoint actually offers, measured in August 2026

SendGrid documents ports 25, 2525 and 587 “for an unencrypted or a TLS connection” and 465 “for a SSL connection”. I opened all four from this machine. Every one completed a TLS 1.3 handshake with a valid certificate:

=== port 25 ===
Protocol version: TLSv1.3
Peer certificate: CN=*.smtp.sendgrid.net
Verification: OK
250 AUTH=PLAIN LOGIN
=== port 465 ===
Protocol version: TLSv1.3
Peer certificate: CN=*.smtp.sendgrid.net
Verification: OK
=== port 587 ===
Protocol version: TLSv1.3
Peer certificate: CN=*.smtp.sendgrid.net
Verification: OK
250 AUTH=PLAIN LOGIN
=== port 2525 ===
Protocol version: TLSv1.3
Peer certificate: CN=*.smtp.sendgrid.net
Verification: OK
250 AUTH=PLAIN LOGIN

Now the part that decides whether hosts_require_tls is optional. This is port 587 before STARTTLS, in clear text:

220 SG ESMTP service ready at geopod-ismtpd-15
250-smtp.sendgrid.net
250-8BITMIME
250-PIPELINING
250-SIZE 31457280
250-SMTPUTF8
250-STARTTLS
250-AUTH PLAIN LOGIN
250 AUTH=PLAIN LOGIN

AUTH is advertised on the unencrypted channel. A client that skips STARTTLS will happily send AUTH PLAIN — your API key, base64-encoded, in the clear. That is not a SendGrid bug; it is why the transport must require TLS and why client_condition is worth the extra line. Port 465 is implicit TLS, so the question does not arise there.

The four TLS handshakes and the clear-text port 587 exchange above are re-run by a script that also re-fetches the SendGrid SMTP guide and the cPanel and Exim pages quoted on this page to match their wording; it sends no mail, changes no configuration and needs neither a cPanel server nor a SendGrid account.

Two things that will still bounce your mail

First, sender identity. Twilio’s sender verification docs: “To protect your sending reputation and to uphold legitimate sending behavior, Twilio requires that customers authenticate or verify their sender identities.” A relay that authenticates correctly can still refuse the message if the From domain is not set up. Second, SPF: your cPanel SPF record does not include SendGrid until you add it. Watch both in the Exim log — parsing bounces out of exim_mainlog is more informative than tail -f.

One housekeeping note: SendGrid is Twilio SendGrid now, and the branding has followed. https://sendgrid.com/, https://sendgrid.com/pricing/ and https://sendgrid.com/en-us/pricing all redirect to https://www.twilio.com/en-us/sendgrid as of August 2026. This page makes no pricing claims; if you are costing this out, read the price from Twilio’s page, not from a tutorial.

Not tested here: I could not apply this configuration or send a message through it. There is no WHM instance and no SendGrid account on this machine. The port probes above are real; the Exim blocks are assembled from cPanel’s and Exim’s own documentation and are unverified on a live server. Restart Exim and send one test message before you point production at it. If that restart has to happen from a deploy script rather than from the WHM screen, exim is one of the service names WHM API 1’s restartservice accepts — and it is the only start/stop-shaped call cPanel exposes.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.