Operating System Debian

Send Email Through Gmail SMTP With Exim4 on Debian 13

Set up Exim4 on Debian 13 as a send-only relay that hands all outgoing mail to Gmail's SMTP server, authenticated with a Google app password.

Many hosting networks block outbound port 25, so a server cannot deliver mail directly to other mail servers. The usual workaround is to hand every outgoing message to an authenticated relay (a "smarthost") on the submission port. This guide configures Exim4 on Debian 13 (trixie) to send all mail through smtp.gmail.com on port 587 with a Google app password. The same steps work on Debian 12 (bookworm). At the end, cron jobs, monitoring scripts and web applications on the server can send mail with the local sendmail command and it will go out through your Google account.

Requirements

  • A Debian 13 or Debian 12 server and a user with sudo rights.
  • Outbound TCP port 587 allowed by your provider and your firewall.
  • A Gmail or Google Workspace account with 2-Step Verification turned on.

Create a Google app password

Google no longer accepts your normal account password over SMTP. The "less secure apps" switch that older tutorials relied on is gone; Google's Workspace documentation states that less secure apps stopped being supported on May 1, 2025. For a mail server that only speaks user-name-and-password SMTP, the supported method is an app password: a 16-character code that is separate from your account password and can be revoked at any time.

  1. Turn on 2-Step Verification for the Google account. App passwords are only available when it is enabled.
  2. Open https://myaccount.google.com/apppasswords, sign in, enter a name such as "exim on server1" and create the password.
  3. Copy the 16 characters now. Google shows the code only once.

The app passwords page may be missing if 2-Step Verification is set up for security keys only, if the account belongs to a work or school organization, or if the account uses Advanced Protection. Changing the Google account password revokes all app passwords, so you will have to create a new one afterwards.

Install Exim4

The light daemon package is enough for relaying:

sudo apt update
sudo apt install exim4-daemon-light

Configure Exim4 as a smarthost client

Debian generates the real Exim configuration from a short settings file, /etc/exim4/update-exim4.conf.conf. You can fill it in through the menu-driven tool or edit it by hand.

Option A: the configuration dialog

sudo dpkg-reconfigure exim4-config

Answer the questions as follows:

QuestionAnswer
General type of mail configurationmail sent by smarthost; no local mail
System mail namethe server's host name, for example server1.example.com
IP-addresses to listen on for incoming SMTP connections127.0.0.1 ; ::1
Other destinations for which mail is acceptedleave empty
Visible domain name for local usersyour domain, for example example.com
IP address or host name of the outgoing smarthostsmtp.gmail.com::587
Keep number of DNS-queries minimal (Dial-on-Demand)?No
Split configuration into small files?No

The double colon in smtp.gmail.com::587 is intentional: it separates the host name from the port number. Listening only on 127.0.0.1 and ::1 keeps the server from accepting mail from the internet.

Option B: edit the settings file

Open /etc/exim4/update-exim4.conf.conf with sudo nano and set these values, leaving the other lines as they are:

dc_eximconfig_configtype='satellite'
dc_other_hostnames=''
dc_local_interfaces='127.0.0.1 ; ::1'
dc_readhost='example.com'
dc_smarthost='smtp.gmail.com::587'
dc_minimaldns='false'
dc_use_split_config='false'
dc_hide_mailname='true'

Store the Gmail credentials

Exim reads smarthost logins from /etc/exim4/passwd.client. Each line has the form server:login:password. Add two lines, with the app password written without spaces:

sudo nano /etc/exim4/passwd.client
smtp.gmail.com:deploy@gmail.com:abcdefghijklmnop
*.google.com:deploy@gmail.com:abcdefghijklmnop

The second line is a safety net. Exim looks up the entry by the name of the host it ends up talking to, and when the smarthost name is a DNS alias that can be a different name under google.com. A leading *. matches any host in that domain.

The file contains a password, so make sure only root and the Exim user can read it:

sudo chown root:Debian-exim /etc/exim4/passwd.client
sudo chmod 640 /etc/exim4/passwd.client

Require TLS and apply the configuration

Exim uses STARTTLS automatically when the remote server offers it, and Debian's configuration does not send a plain-text password over an unencrypted connection. To make Exim refuse to deliver at all when encryption cannot be negotiated, set the macro Debian provides for the smarthost transport:

echo 'REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS = *' | sudo tee -a /etc/exim4/exim4.conf.localmacros

Regenerate the configuration and restart the service:

sudo update-exim4.conf
sudo systemctl restart exim4

Send a test message

Send a message with Exim's sendmail interface. The -v option prints the SMTP conversation so you can watch the TLS handshake and the authentication:

printf 'Subject: Exim relay test\n\nSent through smtp.gmail.com.\n' | sudo sendmail -v deploy@example.com

Then check the log. A successful delivery is a line containing => with T=remote_smtp_smarthost:

sudo tail -n 20 /var/log/exim4/mainlog

Set the sender address for local users

Mail from system accounts would otherwise carry addresses such as root@server1.example.com. The file /etc/email-addresses maps local users to real addresses; Exim rewrites the envelope sender and the From, Reply-To and Sender headers accordingly:

root: deploy@gmail.com
deploy: deploy@gmail.com

To receive mail addressed to root (cron output, for example) in a real mailbox, add an alias in /etc/aliases:

root: deploy@gmail.com

Troubleshooting

  • Authentication fails. The app password is wrong, contains spaces, was revoked, or the login is not the full email address. Create a new app password and update passwd.client.
  • Connection timed out. Port 587 is blocked on the way out. Check your firewall and your provider's policy.
  • Messages stay in the queue. List them with sudo exim4 -bp, read the reason in /var/log/exim4/mainlog, and retry everything with sudo exim4 -qff after fixing the cause.
  • You prefer port 465. Use smtp.gmail.com::465 as the smarthost and add REMOTE_SMTP_SMARTHOST_PROTOCOL = smtps to /etc/exim4/exim4.conf.localmacros, because port 465 expects TLS from the first byte.

The format of the files used here is described in the exim4-config_files(5) manual page, and Google documents app passwords in its account help.

Frequently asked questions

Can I use OAuth instead of an app password?

Google prefers OAuth for applications that support it, but the stock Debian Exim4 configuration authenticates to a smarthost with a user name and password from passwd.client. An app password is the practical choice for a server that sends unattended mail. If app passwords are not available for your account, use a different relay service that accepts SMTP credentials.

Is there a sending limit?

Yes. Google caps how much an account can send per day through smtp.gmail.com; for Google Workspace its documentation gives 2,000 messages per day. This setup suits notifications and low-volume application mail, not newsletters.

Why does the recipient see my Gmail address as the sender?

Messages are submitted with your Google login, so Gmail sends them as that account. If you need another From address, add it under "Send mail as" in the Gmail settings of the account first.

Does this server accept incoming mail?

No. With the listener bound to 127.0.0.1 and ::1, only programs on the server itself can submit mail, and nothing is delivered to local mailboxes.

Share story:

Continue reading other guides on How VPS.