How-To

SASL authentication failed in Postfix with Gmail: how to fix it

Postfix logs "SASL authentication failed" when Gmail rejects the credentials it was sent, or when Postfix has no SASL plugin to log in at all. The usual fix is an app password in a sasl_passwd file, then postmap and a reload. Missing packages and a mismatched relayhost cause most of the rest.

Sohail HussainSohail Hussain7 min read

Short answer

  • Gmail rejects the normal account password for Postfix logins. Turn on 2-Step Verification, create an app password, put it in your sasl_passwd file, run postmap on it, and reload Postfix.
  • If the log says no worthy mechs found, the problem is local: install the SASL plain-text plugin package. If Gmail answers 530 5.7.0, Postfix never sent credentials, so check that the relayhost and the sasl_passwd key match exactly.

Your mail log shows something like this, and queued mail is stuck:

postfix/smtp[1234]: SASL authentication failed; server smtp.gmail.com[...] said: 535-5.7.8 Username and Password not accepted

The quickest way to fix it is to work out which variant of the error you have, because the cause differs for each.

Why it happens

The message comes from Postfix's SMTP client (postfix/smtp) after it tried to log in to the relay. Read the text after said:, since that is what Gmail told you. Find it in /var/log/mail.log (Debian, Ubuntu), /var/log/maillog (RHEL family) or with journalctl -u postfix.

Log textLikely causeFix
535-5.7.8 Username and Password not acceptedWrong or revoked password, or the normal account password was usedCreate a new app password
534-5.7.9 Application-specific password requiredAccount needs an app passwordTurn on 2-Step Verification, create one
530 5.7.0 Authentication RequiredPostfix sent no credentialsFix relayhost and sasl_passwd match, run postmap
no worthy mechs found or no mechanism availableMissing SASL plugin on your serverInstall the plain-text SASL module

In order of how often they occur: a normal password used in place of an app password, a config or postmap mistake, missing SASL libraries, and an account-side block. The exact Google policy for third-party SMTP access has changed more than once, so check Google's own help pages for your account type before assuming an app password is still offered to you.

Step-by-step fixes

Fix 1: use an app password

Google does not accept a normal account password for SMTP logins from apps like Postfix. An app password works for personal accounts with 2-Step Verification on.

  1. Sign in to the Google account that will send the mail.
  2. Turn on 2-Step Verification in the account's security settings. App passwords are not offered without it.
  3. In the Google account, search for "App passwords" and create one named something like postfix.
  4. Copy the 16-character password. Google may show it in groups of four; remove the spaces.

Work accounts and Advanced Protection

Google Workspace admins can restrict app passwords, so if the option is missing, ask your admin. Accounts in Google's Advanced Protection Program cannot use app passwords at all.

In those cases, look at the SMTP relay service in Google Admin (search for "SMTP relay service"), which can authenticate by IP address instead of a password. Menu labels change, so rely on the search route.

Fix 2: correct the Postfix configuration

Add these to /etc/postfix/main.cf:

relayhost = [smtp.gmail.com]:587
smtp_tls_security_level = encrypt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

The square brackets stop Postfix from doing an MX lookup on smtp.gmail.com. On RHEL-family systems the CA file is usually /etc/pki/tls/certs/ca-bundle.crt.

Create /etc/postfix/sasl_passwd with one line:

[smtp.gmail.com]:587    yourname@gmail.com:abcdefghijklmnop

The left side must match relayhost exactly, brackets and port included. A mismatch is a common reason Postfix sends no credentials and Gmail answers 530 5.7.0.

Lock the file down, build the lookup table and reload:

sudo chown root:root /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo systemctl reload postfix

Forgetting postmap is the classic mistake. Postfix reads the compiled sasl_passwd.db, not the text file, so edits do nothing until you rerun it.

Port 465 also works with implicit TLS, but it needs smtp_tls_wrappermode = yes together with smtp_tls_security_level = encrypt. If unsure, stay on 587.

Fix 3: install the missing SASL libraries

If the log says no worthy mechs found or no mechanism available, Postfix has no SASL plugin that can do PLAIN or LOGIN. This has nothing to do with Google, which is why the password looks right and the error persists.

# Debian / Ubuntu
sudo apt install libsasl2-modules

# RHEL / Rocky / Alma
sudo dnf install cyrus-sasl-plain

Reload Postfix and resend.

Fix 4: check whether Gmail blocked the sign-in

If the config is right and the app password is fresh, look at the account:

  1. Check the Google account's inbox for a security alert or blocked sign-in notice.
  2. Confirm the app password was not revoked. Changing the account password can revoke app passwords, so make a new one.
  3. Confirm the address in sasl_passwd is the account that owns the app password. A typo causes 535 too.
  4. If the server sits in a data center or a new country, Google's risk checks can trigger. People report that approving the alert from a normal browser session sometimes clears it.

If Google has suspended the account, no Postfix setting will help; that is between you and Google.

Test it without waiting for the queue

Send a message and watch the log in a second terminal:

echo "test body" | mail -s "postfix gmail test" you@example.com
sudo tail -f /var/log/mail.log

A working setup ends with status=sent (250 2.0.0 OK ...). To test the credentials independently of Postfix, connect with openssl s_client -starttls smtp -connect smtp.gmail.com:587 and try AUTH PLAIN with a base64 string by hand. If that fails, the problem is the credentials, not Postfix.

Retry deferred mail

After fixing the cause, run sudo postqueue -f to retry deferred mail. Messages that already bounced as permanent failures are not retried.

Checklist before you try again

  • 2-Step Verification is on and the app password was created after any password change.
  • The app password has no spaces and belongs to the address in sasl_passwd.
  • The left side of sasl_passwd matches relayhost exactly, including brackets and port.
  • You ran postmap after the last edit and reloaded Postfix.
  • A SASL plugin package is installed.
  • smtp_tls_CAfile points to a file that exists on your distribution.

Limits of using Gmail as a relay

Gmail has daily sending limits and rewrites the From address to the authenticated account unless other addresses are set up as aliases. That suits server alerts and cron mail; for customer-facing or bulk mail, use a provider built for it. If relayed mail lands in spam, the email header analyzer shows what the receiving server saw, and the SMTP glossary entry covers how relay hops work.

If nothing works

Contact Google through the account's help pages (or your Workspace admin, if it is a work account); Postfix itself has a public mailing list for configuration questions. Include the exact log line from mail.log, timestamps, whether the account is personal or Workspace, whether 2-Step Verification and an app password are set up, and the output of postconf -n with the password removed.

Postfix on {server-os} cannot authenticate to smtp.gmail.com:587.
Log line: SASL authentication failed; server smtp.gmail.com[...] said: 535-5.7.8 Username and Password not accepted
First seen: {date and time, timezone}
Account type: {personal / Workspace}; 2-Step Verification: on; app password created {date}
Already tried: new app password, postmap and reload, libsasl2-modules installed
Attached: postconf -n output (password removed)

Common questions

Can I still use my normal Gmail password?

No; Google rejects it for SMTP logins from apps like Postfix, which gives you 535-5.7.8 or 534-5.7.9. Use an app password or, on Workspace, the SMTP relay service.

Why does editing sasl_passwd change nothing?

Postfix reads the compiled sasl_passwd.db. Run postmap /etc/postfix/sasl_passwd after every edit, then reload.

Why do I get 530 5.7.0 instead of 535?

530 5.7.0 means Postfix sent no credentials at all. Check that smtp_sasl_auth_enable = yes is set and that the sasl_passwd key matches relayhost exactly.

Is the app password the same as my account password?

No. It is a separate 16-character password tied to one account, and Google can revoke it when the account password changes.

strategypostfix-to-gmailerror-codes
Share this article
Sohail Hussain

Sohail Hussain

Founder & CEO at Mailneo

Building Mailneo — AI-powered email marketing for growing businesses.

Ready to supercharge your email marketing?

Start sending smarter emails with AI-powered campaigns. No credit card required.

Get Started Free