New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
SMTP and APIUpdated 4 Nov 2026

Send Email from the Linux Terminal: mail, mailx, sendmail and msmtp

How to send email from a Linux shell with mail, mailx, sendmail and msmtp, how they relate to a local MTA and an external SMTP relay, with a working msmtp configuration, cron usage, testing and troubleshooting.

VR
Written by Valentina Reyes
E-commerce Email Specialist
Reviewed by Aarav Mehta
  • Published11 May 2024
  • Read time7 min
  • Length1,334 words
Share this guide

On Linux, mail and mailx are front ends that compose a message, and sendmail is the standard interface to whatever mail transfer agent is installed. None of them delivers mail on its own across the internet reliably. For servers that only need to send, the simplest dependable setup is msmtp: a small SMTP client that you configure once with your relay's host, port and credentials, and that then provides a sendmail command for every other tool to use.

This guide explains how the pieces fit, gives a working msmtp configuration for an authenticated relay on port 587 or 465, shows how to send with each command, and covers cron jobs, testing and the errors you are most likely to see.

Front endsmail and mailx compose messages and hand them to sendmail
Back endA sendmail-compatible binary: a full MTA such as Postfix, or a lightweight client such as msmtp
Recommended for send-only serversmsmtp with TLS to an authenticated relay
Credentials fileMode 600, or fetched with passwordeval from a keyring or encrypted file

How the commands fit together

Confusion arises because the names overlap. mail and mailx are user-level programs, with several implementations (BSD mailx, Heirloom mailx and S-nail) that differ in options. sendmail is a compatibility interface: Postfix, Exim, OpenSMTPD, msmtp and others each provide a binary at /usr/sbin/sendmail that accepts a message on standard input. Scripts, cron and most software call it. The package names vary by distribution, so install by searching your package manager for msmtp, msmtp-mta and your preferred mailx package.

ToolRoleNeeds
mail / mailxCompose and send from the shell, read local mailA sendmail-compatible command (or its own SMTP options in S-nail)
sendmailStandard submit interface; reads a full message on stdinAn MTA or msmtp behind it
msmtpLightweight SMTP client; forwards to a relayA config file with relay and credentials
Postfix or EximFull MTA with queue and retriesMore setup; see Postfix relay configuration
curlOne-off sending from scriptsSee curl SMTP examples

Choose a full MTA when the host handles queues, many messages or inbound mail. Choose msmtp when the machine only needs to push occasional messages such as cron output and alerts. Note that msmtp has no queue: if the relay is unreachable, the send fails and the caller must handle it. For why a local queue matters, see mail queues.

Configure msmtp for an authenticated relay

Create ~/.msmtprc for one user or /etc/msmtprc for the system, and restrict it with chmod 600; msmtp refuses to use a world-readable file that contains a password. The trust file path below is the Debian and Ubuntu location, and other distributions use a different bundle path, so adjust it.

defaults
auth           on
tls            on
tls_trustfile  /etc/ssl/certs/ca-certificates.crt
logfile        ~/.msmtp.log

account        relay
host           smtp.migosmtp.com
port           587
tls_starttls   on
from           [email protected]
user           smtp-username
passwordeval   "gpg --quiet --for-your-eyes-only --decrypt ~/.msmtp-pass.gpg"

account default : relay

For implicit TLS on port 465, set port 465 and tls_starttls off. The passwordeval line runs a command and uses its output as the password, which keeps the secret out of the file; a plain password line also works if the file is mode 600. See choosing between ports 25, 465 and 587 if you are unsure which port to use.

Send with msmtp and sendmail

A message piped to msmtp must contain its own headers. The -t option tells it to take recipients from the To, Cc and Bcc headers.

printf 'From: Alerts <[email protected]>\nTo: [email protected]\nSubject: Disk warning\n\nVolume /var is 91 percent full.\n' | msmtp -t

# Same via the sendmail interface (with msmtp-mta installed)
printf 'Subject: Test\n\nHello from sendmail.\n' | sendmail -t -i -f [email protected] [email protected]

With sendmail -t, recipients are read from the headers, and -i stops a lone dot on a line from ending the message. The -f option sets the envelope sender, which is where bounces return; see envelope from and Return-Path versus From for why that matters.

Send with mail and mailx

# Simple message, body from stdin
echo 'Backup finished at 02:14' | mail -s 'Nightly backup' [email protected]

# With a From address (option names vary between mailx implementations)
echo 'Backup finished' | mail -s 'Nightly backup' -r [email protected] [email protected]

# Attach a file with S-nail / newer mailx
echo 'Report attached' | mail -s 'Report' -a /var/reports/report.pdf [email protected]

The flags differ between implementations: -r and -a are common but not universal, so read man mail on your system. With S-nail you can bypass sendmail and point the program at a relay with the mta variable, but putting a password in a command line or a shared rc file is weaker than msmtp's protected config. For attachments you can control precisely, build the message with a tool that creates MIME correctly; see multipart MIME explained.

Cron and system mail

Cron mails job output to the owner when a MAILTO variable is set or to the local user by default, using the system's sendmail command. If you install msmtp-mta or alias /usr/sbin/sendmail to msmtp, cron mail leaves the machine through your relay. Keep the From address valid and authenticated, otherwise receivers may reject it.

# crontab -e
[email protected]
0 2 * * * /usr/local/bin/backup.sh

To avoid noisy mail, make jobs silent on success and print only errors. Also configure /etc/aliases or the msmtp aliases option so mail to root goes to a monitored address; for details of the sender side, see server mail scripts.

Test and verify

  1. Check the connection and TLS first: msmtp --serverinfo --host=smtp.migosmtp.com --port=587 --tls=on --tls-starttls=on prints the server's capabilities and certificate details.
  2. Send a message with debugging: msmtp --debug -t < message.txt shows the SMTP conversation. Redact credentials before sharing the output.
  3. Check ~/.msmtp.log for the result of each send.
  4. On the receiving side, open the full headers and confirm SPF and DKIM pass. See reading email headers.
  5. Test a bad recipient to see how your script handles a rejection.

Troubleshooting

SymptomLikely causeFix
msmtp: password file or config is accessible by othersPermissions too openchmod 600 on the config and any password file
authentication failed (535)Wrong user or passwordRe-check credentials. See fixing 535 errors
TLS certificate verification failedWrong tls_trustfile path or missing CA bundlePoint tls_trustfile to the CA bundle of your distribution
cannot connect or timeoutOutbound port blockedTry 587 or 465; see connection timeouts
mail command works but nothing is sentNo sendmail-compatible command installedInstall msmtp-mta or an MTA and test sendmail directly
Relay rejects the senderFrom domain not allowed or unauthenticatedUse an authenticated domain. See relay denied errors
Message in spamNo SPF, DKIM or DMARC for the From domainSet up authentication; see authenticating your sending domain

Choosing a setup for production hosts

A single script on one server is easy; a fleet is where habits matter. Give every host the same msmtp configuration managed by your configuration tool, with one sending identity per role (for example, one address for monitoring and another for application mail), so you can tell sources apart in the logs and revoke one without touching the others. Use a dedicated From address on a domain you control instead of root@hostname, which most receivers will not trust and which often fails alignment checks.

Decide who reads the mail. Alerts that go to an unmonitored mailbox are worse than no alerts, because they create false confidence. Test the whole path regularly: a daily heartbeat message that your monitoring expects to receive will tell you when the relay credentials expire or an outbound port is closed. If your servers send customer-facing mail as well, keep that traffic separate from operational mail; see separating mail streams.

Security notes and next steps

  • Never put relay passwords in world-readable files or in crontabs.
  • Use a credential limited to sending, and rotate it if exposed.
  • Do not run an open relay: keep any local MTA bound to localhost unless you need otherwise. See open relays.
  • Rate limit scripts that can loop, because a bug can send thousands of messages.

Relay your server mail Point msmtp or Postfix at the MigoSMTP relay on port 587 or 465.

Explore the SMTP relay

References and further reading

Frequently asked questions

What is the difference between mail, mailx and sendmail?

mail and mailx compose messages, while sendmail is the standard submit interface provided by an MTA or a client such as msmtp.

What is the easiest way to send email from a Linux server?

Install msmtp, configure it with your relay host, port 587 or 465 and credentials, and use msmtp or its sendmail wrapper.

Why does the mail command do nothing?

Usually no sendmail-compatible program is installed or configured, so the message is queued locally or dropped. Install msmtp or an MTA.

How do I protect the SMTP password in msmtp?

Set the config file to mode 600 and use passwordeval to fetch the password from an encrypted file or keyring.

Does msmtp queue messages?

No. If the relay is unreachable the send fails, so use a full MTA such as Postfix if you need a queue with retries.

Topics#linux#msmtp#mailx#sendmail#cron#command line
Was this guide helpful?Your answer helps us improve our documentation.

Start sending in minutes.

Create an account, add your domain and send your first email.

Get started Talk to sales