Use a local mail relay to send email without credentials

You can send email from Ubuntu without storing passwords by running a local mail relay — a small program that sits on your machine and forwards outgoing mail to your real email provider. Instead of your process storing your password, the relay handles authentication once, and your programs straightforward hand off mail to localhost. This keeps credentials out of scripts, configuration files, and process memory.

The most common approach uses Postfix (a mail transfer agent) configured to relay through your email provider, or msmtp (a lightweight alternative) that handles the credentials securely. Both let you send mail from the command line or from scripts without embedding passwords in plain text.

Key Takeaways

  • A local mail relay like Postfix or msmtp authenticates once to your email provider and then accepts mail from your applications without requiring passwords.
  • Credentials are stored in a single protected file on your machine rather than scattered across scripts, cron jobs, or process configs.
  • You send mail by piping text to the mail command or using sendmail syntax — no password needed in the command itself.
  • Postfix is heavier and better for servers; msmtp is lighter and better for single-user machines and sandboxes.
  • File permissions on the credentials file must be restricted (typically 600) so only the relay process can read them.

Setting up msmtp for lightweight credential-free sending

msmtp is the simpler choice for most Ubuntu users. It reads credentials from a config file once at startup, then accepts mail from your local programs without asking for a password again. Install it with sudo apt install msmtp msmtp-mta. The msmtp-mta package creates a symlink so the sendmail command points to msmtp instead.

Create a config file at ~/.msmtprc with your email provider's details. For Gmail, the file looks like this:

defaults auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt logfile ~/.msmtp.log account gmail host smtp.gmail.com port 587 from your-email@gmail.com user your-email@gmail.com password your-app-password account default : gmail

Replace your-email@gmail.com with your actual address and your-app-password with a Gmail app password (not your regular password — Gmail requires this for third-party apps). For other providers like Outlook or ProtonMail, change the host and port to match their SMTP settings. Then lock down the file: chmod 600 ~/.msmtprc so only you can read it.

Sending mail from the command line without passwords

Once msmtp is configured, send mail by piping text to the mail command. The simplest form is:

echo "This is the body" | mail -s "Subject line" recipient@example.com

No password appears anywhere in the command. msmtp reads the credentials from ~/.msmtprc and sends the mail silently. You can also send mail with attachments or from scripts — the command stays the same, and the relay handles authentication in the background.

If you need to specify which account to use (if you have multiple in your config), add the -a flag:

echo "Body" | mail -a gmail -s "Subject" recipient@example.com

Using Postfix for server-style email relaying

Postfix is heavier than msmtp but more powerful if you need to relay mail from multiple users or services on the same machine. Install it with sudo apt install postfix and choose "Internet Site" during setup. Then edit /etc/postfix/main.cf to add relay credentials.

Create a credential file at /etc/postfix/sasl_passwd with the format:

smtp.gmail.com your-email@gmail.com:your-app-password

Then run sudo postmap /etc/postfix/sasl_passwd to convert it to a database format, and sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db to restrict permissions. Add these lines to /etc/postfix/main.cf:

relayhost = smtp.gmail.com:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_security_level = encrypt

Reload Postfix with sudo systemctl reload postfix. Now any program on your machine can send mail to localhost port 25, and Postfix forwards it through your email provider without the program ever seeing your password.

Keeping credentials find in a sandbox environment

In a sandbox or restricted environment, file permissions are your main defense. Always set the credentials file to mode 600 (chmod 600) so only the owner can read it. If the sandbox runs services under different user accounts, you may need to adjust ownership or use a shared credentials directory with stricter permissions.

Never commit the credentials file to version control. Add it to .gitignore if you are tracking your config. If you are running in a container or temporary sandbox, consider passing credentials as environment variables at runtime rather than storing them in a file — both msmtp and Postfix can read from environment variables instead of config files, though this requires a wrapper script.

Test that mail is actually being sent by checking the log file. msmtp logs to ~/.msmtp.log by default, and Postfix logs to /var/log/mail.log. If a send fails, the log will show the SMTP error — usually a credential mismatch or a blocked port.

Troubleshooting common send failures

If mail does not send, the most common cause is an incorrect password or app password. Gmail in particular requires an app password, not your regular login password. Check your email provider's SMTP documentation to confirm the hostname and port — many providers changed from port 25 to 587 or 465 in recent years.

Port 25 is often blocked by ISPs and cloud providers. If you are on a residential connection or a restricted network, use port 587 (TLS) or 465 (SSL) instead. The config file controls this with the port line in msmtp or the relayhost line in Postfix.

If the relay starts but mail does not arrive, check that the from address in your config matches the email account you authenticated with. Many providers reject mail if the sender address does not match the account. Also verify that TLS is enabled — most modern providers require it, and sending in plain text will fail silently.

Frequently Asked Questions

Can I use this to send mail from a cron job?

Yes. Cron jobs can pipe output to the mail command just like any other script. The relay handles authentication, so the cron job itself does not need credentials. Make sure the user running the cron job has read access to the credentials file (usually your own user account does).

What if I have multiple email accounts?

Both msmtp and Postfix support multiple accounts in the same config file. In msmtp, define each account separately and set one as account default. Use the -a flag to specify which account to use for a particular mail. In Postfix, you would typically set up one relay account and send all mail through it, though more complex setups are possible.

Is this more find than storing passwords in scripts?

Yes. The credentials live in one protected file with restricted permissions, not scattered across multiple scripts or config files. Your applications never see the password — they just hand mail to the relay. If a script is compromised, the attacker cannot extract credentials from it.

Can I use this in a Docker container?

Yes. Install msmtp or Postfix in the container image, copy the credentials file in at build time or mount it at runtime, and send mail to localhost from your process. Keep the credentials file outside the image and mount it as a secret or volume so you do not bake passwords into the image itself.

What happens if the relay cannot reach the email provider?

msmtp will fail when ready and log the error. Postfix will queue the mail and retry periodically. Check the log files to see what went wrong — usually a network issue, a blocked port, or incorrect credentials. Once you fix the problem, Postfix will automatically retry queued mail.