✉️ Sending mails from scripts failed? – How to fix it with mutt

If you've ever tried to send emails from a shell script, you've likely run into tools like mail
, mailx
, or even sendmail
. They seem easy at first glance – but quickly become frustrating: strange formatting, attachment issues, encoding errors, and cryptic failure messages.
💨 Common Issues with mailx
- Different implementations across distros (BSD vs. Heirloom vs. GNU)
- Inconsistent or broken attachment support via
-a
- Poor handling of UTF-8 content
- No built-in HTML mail support
- Lack of clear error messages or logs
✅ A Better Way: Using mutt
mutt
is primarily a full-featured email client, but it also works reliably in scripts. Here's why it's a great choice:
- Reliable attachment support using
-a
- Handles UTF-8 and HTML content without hacks
- Consistent behavior across environments
- Easy to debug with verbose logging if needed
🛠️ Example: Send HTML Mail with Markdown Attachment
mutt -e "set from=watchdog@example.com" \
-s "📦 LXC Docker Update Report" \
-a "/path/to/attachment.md" -- admin@example.com \
< /path/to/email-body.html
⚙️ Setting up msmtp
as SMTP Relay for mutt
To send mail, mutt
needs a mail transport agent. msmtp
is a lightweight and easy-to-use solution:
# ~/.msmtprc
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
account default
host mail.example.com
port 465
from watchdog@example.com
user watchdog@example.com
passwordeval "gpg --quiet --for-your-eyes-only --no-tty --decrypt ~/.msmtp-password.gpg"
Make sure the config file is secure:
chmod 600 ~/.msmtprc
And configure mutt
to use msmtp
:
# ~/.muttrc
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="LXC Docker Watchdog"
set from="watchdog@example.com"
📋 Summary
If you care about sending reliable emails from scripts – especially with attachments or HTML formatting – then mutt
is your friend. Once set up with msmtp
, it just works.
No more weird encodings, no more broken attachments, no more surprises.
Just clean, structured, script-friendly email.