TechBuzzPro

Postfix Gmail Relay: Step-by-Step Setup Guide

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To configure Postfix to send mail through Gmail, set up SMTP authentication with your Google account, point Postfix at smtp.gmail.com on port 587 with TLS, and use an App Password rather than your normal Google password. This lets a local mail server relay outgoing messages through Gmail's SMTP servers, which is useful when your ISP blocks port 25 or when you want Gmail's deliverability and spam filtering for a low-volume server. The exact menu labels in Google Account settings can differ slightly by version and region, but the core path is the same.

What You'll Need

  • A Linux server with Postfix installed (most distributions include it or offer it in the package manager).
  • A Google account with 2-Step Verification enabled. App Passwords are only available when 2-Step Verification is on.
  • An App Password generated for Postfix. Do not use your regular Google password.
  • Root or sudo access to edit Postfix configuration files and restart the service.
  • Outbound access to port 587 (submission) from your server. Some hosts block this; check with your provider if unsure.

Step-by-Step

  1. Enable 2-Step Verification on the Google account. Sign in to your Google Account, open the Security section, and turn on 2-Step Verification if it is not already active. App Passwords are unavailable without it.
  2. Generate an App Password. In the same Security section, find App Passwords (it may be under "How you sign in to Google"). Choose "Mail" as the app and "Other" as the device, name it something like "Postfix relay", and copy the 16-character password Google shows. You will not see it again, so store it safely.
  3. Install the SASL authentication package. Postfix needs a SASL library to authenticate with Gmail. On Debian/Ubuntu install libsasl2-modules; on RHEL/CentOS/Fedora install cyrus-sasl-plain. Without this, authentication fails even with correct credentials.
  4. Create the SASL password map. Create a file such as /etc/postfix/sasl/smtp_passwd containing one line: [smtp.gmail.com]:587 your.address@gmail.com:your16charapppassword. Then run postmap /etc/postfix/sasl/smtp_passwd to build the lookup table. Set permissions so only root can read it, for example chmod 600.
  5. Edit /etc/postfix/main.cf. Add or update these settings: relayhost = [smtp.gmail.com]:587, smtp_use_tls = yes, smtp_sasl_auth_enable = yes, smtp_sasl_password_maps = hash:/etc/postfix/sasl/smtp_passwd, smtp_sasl_security_options = noanonymous, smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt (adjust the CA bundle path for your distribution), and smtp_tls_security_level = encrypt. The CA file lets Postfix verify Gmail's certificate.
  6. Set a valid sender address. Gmail rewrites or rejects mail whose From address it does not recognise as yours. Configure myorigin, myhostname, and if needed smtp_generic_maps so that outgoing mail uses your Gmail address or an address you have added as an alias in Gmail. A common approach is to set myorigin = your.domain and use a generic map to rewrite local users to the Gmail address.
  7. Restart Postfix and test. Run systemctl restart postfix, then send a test message with echo "test" | mail -s "Postfix test" your.address@gmail.com. Check /var/log/mail.log (or /var/log/maillog) for a status=sent line and look for the message in the Gmail inbox.
  8. Confirm the relay is actually used. If the log shows delivery directly rather than through smtp.gmail.com, check that relayhost is set and that no transport map overrides it. Also confirm the server can reach port 587 with a quick connection test.

Common Mistakes

  • Using the normal Google password instead of an App Password. This always fails with authentication errors.
  • Forgetting to install the SASL plain module, which produces "no mechanism available" errors.
  • Leaving the password file world-readable, or forgetting to run postmap after editing it.
  • Sending from an address Gmail does not recognise. Gmail may reject the message or rewrite the sender, which breaks SPF and DMARC checks downstream.
  • Using port 465 without the correct wrapper mode. Port 587 with STARTTLS is the standard submission setup; port 465 expects implicit TLS and needs different settings.
  • Not restarting or reloading Postfix after configuration changes.

Tips

  • Keep the App Password out of version control and world-readable files. Restrict it to root.
  • Use a dedicated Google account for server mail if volume is low, and keep an eye on sending limits. Gmail is not designed for bulk mail.
  • Set a meaningful myhostname and a real myorigin so headers look sane to recipients.
  • Test with a message to an external address, not just to yourself, to confirm the relay works end to end.
  • If you manage several servers, generate a separate App Password for each so you can revoke one without breaking the others.

Troubleshooting

If mail does not go out, start with the log. Look for lines mentioning smtp.gmail.com and read the status code. Authentication failures usually mean a wrong App Password, a missing SASL module, or a password map that was not built with postmap. Connection timeouts often mean your host blocks port 587; try a connection test from the server and ask your provider if it is filtered. Certificate errors point to a missing or wrong CA bundle path. If Gmail rejects the sender, check that the From address matches the account or an alias, and adjust your generic maps. If messages are delivered but land in spam, review SPF and DKIM for your sending domain; Gmail relay does not fix domain reputation by itself. Finally, confirm Postfix restarted cleanly and that no other transport rule is bypassing the relayhost.

Frequently Asked Questions

Can I use my normal Gmail password?

No. Google requires an App Password for SMTP authentication when 2-Step Verification is enabled, and normal passwords are rejected for this kind of access.

Which port should I use?

Port 587 with STARTTLS is the standard submission port and the recommended choice. Port 465 uses implicit TLS and needs different Postfix settings.

Why does Gmail rewrite my From address?

Gmail only allows sending as addresses it recognises as yours. Add the address as an alias in Gmail or rewrite outgoing senders to your Gmail address.

Is this suitable for bulk email?

No. Gmail has sending limits and is intended for low-volume personal or small server use. For bulk mail, use a dedicated transactional email provider.

Do I need to open any firewall ports?

Only outbound access to port 587 is needed. No inbound ports have to be opened for relaying through Gmail.

What if the App Password option is missing?

App Passwords only appear when 2-Step Verification is enabled. Turn that on first, then return to the Security section.