How do I send a file as an email attachment using Linux command line?
Stefan Bogdanescu
Founder & Senior Architect
How Do I Send a File as an Email Attachment Using the Linux Command Line?
As developers working in the command line environment, we frequently need to automate tasks that bridge different systems—like backing up data and notifying administrators. You’ve already mastered piping text files into mail utilities, but you’ve hit a common roadblock: sending actual file attachments. While piping text works fine for simple notifications, attaching binary or compressed files requires a different approach involving MIME (Multipurpose Internet Mail Extensions) encoding.
If you are running automated backup scripts on a Linux server and need to securely email those compressed .tar archives, this guide will show you the most effective command-line methods to achieve file attachments reliably.
Why Piping Text Fails for Attachments
You correctly identified that piping data using commands like cat mysqldbbackup.sql | mailx user@example.com works by sending the content of the file as the email body. The mailx utility is designed primarily to handle plain text messages. When you try to pipe a binary or compressed file (like a .tar), the resulting email body becomes corrupted garbage because it lacks the necessary MIME headers required for attachments.
To send an attachment, the mail program needs to invoke the appropriate MIME structure to wrap the file data correctly so that the receiving mail client knows how to handle the file separately from the message body.
Method 1: Using mailx or mail with File Input (The Simple Approach)
Some versions of mailx and related utilities offer a direct way to specify an attachment when reading from a file, though this support can vary depending on your mail server configuration. A common pattern involves using the -a flag (for attachment).
While less intuitive for complex MIME scenarios, if your system supports it, you can attempt to read the file and attach it:
# Assuming 'backup.tar.gz' is your compressed file
mailx -s "Nightly Backup Report" -a backup.tar.gz user@example.com < /dev/null
Caveat: This method often fails or results in poorly formatted emails if the underlying mail transport system isn't fully configured for MIME handling. For robust, automated file transfers, a dedicated tool is usually superior.
Method 2: The Robust Solution with mutt (The Developer's Choice)
For developers and power users dealing with complex file attachments and MIME encoding, the command-line mail client mutt is often the gold standard. mutt excels at handling MIME structures necessary for attaching files correctly across various email protocols.
Here is how you would use mutt to attach your compressed backup file:
# 1. Define the recipient and subject
RECIPIENT="backup@example.com"
SUBJECT="Automated MySQL Backup: $(date +%Y-%m-%d)"
FILE_TO_ATTACH="/path/to/your/mysqldbbackup.tar.gz"
# 2. Construct the command to send the email with an attachment
mutt -s "$SUBJECT" -a "$FILE_TO_ATTACH" "$RECIPIENT" < /dev/null
Breakdown of the mutt Command:
-s "$SUBJECT": Specifies the subject line for the email.-a "$FILE_TO_ATTACH": This is the crucial part. It instructsmuttto attach the specified file to the message.- "$RECIPIENT": The destination email address.
< /dev/null: Since we are only sending an attachment and not a large body of text, piping/dev/nullensures that no extraneous input is mistakenly treated as the email body.
This method delegates the complex job of creating the correct MIME boundaries for attachments to mutt, making it far more reliable for automated backend tasks than simple piping into mailx. This approach aligns with building robust systems, much like ensuring data integrity when handling database backups, a principle central to modern frameworks like those found in Laravel where reliable data flow is paramount.
Conclusion
While the simplicity of using cat | mailx is tempting for quick text messages, sending actual file attachments requires adherence to MIME standards. For automated scripting involving compressed files and reliability, switching to tools like mutt provides the necessary control over encoding and attachment handling. By adopting these robust command-line utilities, you ensure that your nightly backup notifications are not just sent, but securely delivered as functional email attachments every time.
Note: Blog content is currently available in English.