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.
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.
| Tool | Role | Needs |
|---|---|---|
| mail / mailx | Compose and send from the shell, read local mail | A sendmail-compatible command (or its own SMTP options in S-nail) |
| sendmail | Standard submit interface; reads a full message on stdin | An MTA or msmtp behind it |
| msmtp | Lightweight SMTP client; forwards to a relay | A config file with relay and credentials |
| Postfix or Exim | Full MTA with queue and retries | More setup; see Postfix relay configuration |
| curl | One-off sending from scripts | See 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 : relayFor 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.shTo 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
- Check the connection and TLS first:
msmtp --serverinfo --host=smtp.migosmtp.com --port=587 --tls=on --tls-starttls=onprints the server's capabilities and certificate details. - Send a message with debugging:
msmtp --debug -t < message.txtshows the SMTP conversation. Redact credentials before sharing the output. - Check
~/.msmtp.logfor the result of each send. - On the receiving side, open the full headers and confirm SPF and DKIM pass. See reading email headers.
- Test a bad recipient to see how your script handles a rejection.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| msmtp: password file or config is accessible by others | Permissions too open | chmod 600 on the config and any password file |
| authentication failed (535) | Wrong user or password | Re-check credentials. See fixing 535 errors |
| TLS certificate verification failed | Wrong tls_trustfile path or missing CA bundle | Point tls_trustfile to the CA bundle of your distribution |
| cannot connect or timeout | Outbound port blocked | Try 587 or 465; see connection timeouts |
| mail command works but nothing is sent | No sendmail-compatible command installed | Install msmtp-mta or an MTA and test sendmail directly |
| Relay rejects the sender | From domain not allowed or unauthenticated | Use an authenticated domain. See relay denied errors |
| Message in spam | No SPF, DKIM or DMARC for the From domain | Set 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.
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.