Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Send Mail From Command Line Using Mutt

· 7 min read · Home Tech

In this article
  1. What Mutt Is and Why Use It
  2. Basic Installation and First Steps
  3. Sending a Basic Email
  4. Adding Attachments and Other Options
  5. Reading Email Content from a File
  6. Common Problems and Troubleshooting
  7. Security and Best Practices
  8. Alternatives to Mutt for Command-Line Email
  9. Frequently Asked Questions

Yes, you can send email directly from the command line using the mutt command, typically with a one-liner like echo "Body text" | mutt -s "Subject" recipient@example.com. This works on any Unix-like system (Linux, macOS, BSD) where mutt is installed and configured with an outgoing mail server.

What Mutt Is and Why Use It

Mutt is a text-based email client that has been around since the mid-1990s. Unlike graphical clients like Thunderbird or Outlook, mutt runs entirely in the terminal, which makes it ideal for scripting, automation, and working on remote servers over SSH. Its command-line mode (mutt invoked with arguments) lets you send messages without ever opening an interactive interface.

People use mutt for sending alerts from cron jobs, emailing logs from backup scripts, notifying themselves of long-running process completions, or simply because they prefer keyboard-driven workflows. Because mutt is a full email client, it also supports attachments, multiple accounts, and reading mail, but the command-line send feature is what most automation scripts need.

Basic Installation and First Steps

Mutt is available in the package repositories of nearly every Linux distribution and on macOS via Homebrew. To install it:

  • Debian/Ubuntu: sudo apt install mutt
  • RHEL/CentOS/Fedora: sudo dnf install mutt (or yum on older systems)
  • macOS: brew install mutt
  • FreeBSD: pkg install mutt

After installation, you need a minimal configuration file, ~/.muttrc, that tells mutt how to send mail. The most common approach is to use an SMTP server. For example, to use Gmail's SMTP server with an app password:

set smtp_url = "smtps://your_email@gmail.com@smtp.gmail.com:465/"
set smtp_pass = "your_app_password"
set from = "your_email@gmail.com"
set realname = "Your Name"

Replace the placeholders with your actual credentials. Note that many providers require an app-specific password rather than your normal account password. For a local sendmail or Postfix server, you can simply set set sendmail = "/usr/sbin/sendmail" and skip SMTP settings entirely.

Sending a Basic Email

The simplest command sends a message with a subject and body to one recipient:

echo "This is the email body." | mutt -s "Test Subject" user@example.com

Here, -s specifies the subject, and the recipient address is the final argument. The body comes from standard input (the pipe). If you want to type the body interactively, omit the pipe and mutt will open your default editor (usually vi or nano) for you to compose the message.

To send to multiple recipients, just list them separated by spaces:

echo "Hello all" | mutt -s "Notice" user1@example.com user2@example.com

You can also copy (CC) or blind-copy (BCC) recipients using -c and -b flags:

echo "Body" | mutt -s "Subject" main@example.com -c cc@example.com -b bcc@example.com

Adding Attachments and Other Options

Attachments are handled with the -a flag. You can attach one or more files, and the recipient list must come after the attachment list. A common mistake is putting the attachment after the recipient, which mutt interprets as another recipient. Correct syntax:

echo "See attached file." | mutt -s "Report" -a /path/to/file.pdf -- recipient@example.com

The double dash -- tells mutt that everything after it is a recipient, not an attachment. This is especially important when the filename starts with a dash.

Other useful options include:

  • -b – specify a BCC address
  • -c – specify a CC address
  • -e – execute a mutt command after startup, e.g., -e "set from=..." to override config
  • -F – use an alternative config file
  • -s – set the subject (if it contains spaces, quote it)
  • -n – do not read the system-wide configuration

Reading Email Content from a File

Instead of piping a short string, you can send the contents of a file as the body using input redirection:

mutt -s "Log output" admin@example.com < /var/log/backup.log

This reads the file and sends its entire contents as the message body. For very large files, be aware of size limits imposed by your mail server. You can also combine a file body with attachments.

If you need to include dynamic data, you can generate the body with a command and pipe it:

df -h | mutt -s "Disk usage" admin@example.com

This sends the output of df -h as the email body. This pattern is extremely useful for monitoring scripts.

Common Problems and Troubleshooting

Several issues frequently arise when sending mail via mutt from the command line:

  • "Error sending message: Child process exited with code 1" – This usually means mutt couldn't connect to the SMTP server or authentication failed. Check your smtp_url and smtp_pass in .muttrc. For Gmail, ensure you've enabled an app password and that 2-Step Verification is on.
  • Certificate verification errors – If your SMTP server uses a self-signed certificate or an outdated TLS version, mutt may refuse to connect. You can temporarily disable verification with set ssl_verify_host = no and set ssl_verify_dates = no, but this is insecure and should only be used for testing.
  • Recipient not recognized – If you use -a for attachments, ensure you place the -- separator before the recipient email addresses. Otherwise mutt treats the attachment path as a recipient.
  • Mutt opens an editor instead of sending – When you don't provide a body via stdin, mutt opens your editor. To avoid this, always pipe something (even an empty string) or use -e 'set edit_headers=no' to skip the editor.
  • SMTP port blocked – Some networks block port 25 or 587. If you use a non-standard port, specify it in the smtp_url, e.g., smtps://user@smtp.example.com:465/.

Security and Best Practices

When using mutt for automated email, keep these security points in mind:

  • Never hardcode passwords in scripts – Store the SMTP password in a file with restricted permissions (e.g., chmod 600 ~/.muttrc) or use an environment variable referenced in the config.
  • Use TLS/SSL – Always use smtps:// or smtp://...?tls=on to encrypt the connection. Plaintext SMTP on port 25 sends your credentials in clear text.
  • Be careful with HTML – Mutt sends plain text by default. If you need HTML, you must set set content_type="text/html" and ensure the body is valid HTML. Avoid sending unsolicited bulk email; that's both unethical and can get your server blacklisted.
  • Logging – Mutt doesn't log sent messages by default. If you need an audit trail, consider using set record="~/.sent" to save copies to a file, or rely on your SMTP server's logs.

Alternatives to Mutt for Command-Line Email

Mutt is not the only way to send email from the terminal. Depending on your needs, you might consider:

  • mailx / mail – The classic mail command is simpler but less flexible. It's available on most systems and works well for basic sends: echo "Body" | mail -s "Subject" user@example.com.
  • sendmail – Directly invoking sendmail (or Postfix's sendmail wrapper) gives you low-level control but requires you to format the message headers yourself.
  • curl with SMTP – curl --url 'smtps://smtp.example.com:465' --mail-from ... can send mail without any email client, useful if you already use curl.
  • Python's smtplib – For complex scripting, Python's standard library offers full SMTP control and is often easier to debug.

Mutt remains a strong choice because it balances simplicity with powerful features like attachments, multiple accounts, and a configurable interface that works both interactively and from scripts.

Frequently Asked Questions

How do I send an email with mutt without opening an editor?

Pipe the body to mutt using echo or cat. For example: echo "Hello" | mutt -s "Subject" user@example.com. If you need to send an empty body, use echo "" | mutt ....

Can I send HTML emails with mutt?

Yes. Add set content_type="text/html" to your .muttrc or use -e 'set content_type=text/html' in the command line, then pipe HTML content as the body. Be sure the HTML is well-formed and includes the necessary tags.

Why does mutt say "No recipients specified" even though I gave an address?

This usually happens when the email address is preceded by a dash or when you use -a without the -- separator. Use -- before the recipient list to prevent mutt from interpreting the address as an option.

How do I attach multiple files in one command?

List all file paths after -a, separated by spaces: mutt -s "Subject" -a file1.txt file2.pdf -- recipient@example.com. The -- is essential to separate attachments from recipients.

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

Yes. Just call the mutt command in your crontab. Make sure the environment variables (like HOME) are set correctly, and consider using absolute paths for any attachments or config files. It's also wise to redirect stderr to a log file to debug issues.

What is the difference between mutt and mailx?

Mutt is a full-featured email client with interactive and command-line modes, supporting attachments, multiple accounts, and extensive configuration. Mailx is a simpler BSD- or POSIX-style mail utility that only sends and reads mail in a basic way. For most scripting, either works, but mutt offers more control and features.