Email is an essential communication tool for most computer users today. While graphical email clients provide an easy way to compose and send emails with attachments, power users often prefer the flexibility and speed of sending emails directly from the Linux command line.
In this comprehensive guide, we will explore several methods for sending email attachments from the Linux command line using built-in and third-party tools. Whether you are looking to automate sending attachments or simply prefer working in the terminal, this guide has you covered.
An Overview of Sending Emails from the Command Line
Before we dive into the specifics of sending attachments, let‘s do a quick review of how to send plain text emails from the terminal.
The main tool used for sending email from Linux is the mail command. Here is the basic syntax:
mail -s "Subject" recipient@example.com
The -s flag sets the subject line for the email. After running this command, you will be placed into an input mode where you can type out your email body. Finish the email by typing control+D on a blank line.
For example, to send a test email:
mail -s "Test email" my.email@domain.com
Dear recipient,
This is a test email sent from the Linux command line.
Sincerely,
Sender Name
Hit control+D, and your email will be queued and sent asynchronously by your system‘s local mail transfer agent.
This covers the basics of sending plain text email. Now let‘s look at methods for adding attachments.
Using mutt to Send Attachments
The mutt command is an incredibly powerful terminal-based email client for Linux. With just a little configuration, it can handle sending, receiving, and organizing emails right from the command line.
Here is how to use mutt to send an email with attachments:
-
Install Mutt – Most Linux distros have a mutt package available in their repositories. On Debian/Ubuntu:
sudo apt install mutt -
Compose the email body – The message body can be piped into mutt via stdin like so:
echo "Email message body" | mutt -s "Subject line" recipient@example.com -
Specify attachments – Use the
-aflag to specify file attachments:echo "Email message body" | mutt -s "Subject" -a file1.pdf -a document.doc recipient@example.comYou can attach as many files as you want by adding more -a flags.
And that‘s it! Mutt will construct and send a properly formatted multipart email with attachments.
Some key advantages of using Mutt:
- Handles converting attachments to MIME encoding automatically
- Supports attaching unlimited files
- Feature-rich email client with extensive configuration options
The main downside is needing to install the extra package rather than using built-in tools. But for frequent or complex email tasks, Mutt is hard to beat for CLI mail functionality.
Sending Attachments with the mail Command
The default mail command found in most Linux distributions can also send attachment emails in a pinch. The approach is not as streamlined as Mutt, but it works reliably with no extra dependencies.
Here is the basic syntax:
mail -s "Subject" -A attachment1.file -A attachment2.pdf recipient@example.com < message_body.txt
Let‘s break this usage down:
-ssets the subject line as before-Aspecifies each attachment file, allowing unlimited attachments- The message body is provided via stdin input redirection from a text file
The message body file should contain plain text only.
Here is a full example sending 2 attachments:
mail -s "Report attachments" -A figures.xls -A report.pdf myboss@company.com < message_text.txt
Where message_text.txt contains our email body text.
The key advantage of mail is that it comes pre-installed on just about any Linux system. The main downsides are:
- No way to pipe the message body directly into the command
- Requires creating a separate text file for the message body
- Handles MIME encoding automatically
Overall, mail works reliably for basic email attachments from the Linux command line.
Send Attachments with Postfix
On Linux servers running Postfix for sending mail, you can utilize some helpful Postfix commands to send attachments without any extra dependencies.
The main tool is postcat, which submits messages for delivery by Postfix.
To send an attachment-enabled email with Postfix:
-
Create a multipart message file – Postfix requires you to manually format a valid MIME multipart message with message headers, attachment encoding, etc. This is the downside to this approach. But for one-off messages, you can create a simple message file like this:
To: recipient@example.com From: sender@domain.com Subject: Email with attachment This is the email message body. --BOUNDARY Content-Type: text/plain Content-Disposition: attachment; filename="file.txt" Contents of the attached text file --BOUNDARY--Customize the To, From, Subject, and attachment contents for your specific email. The message is delimited with a boundary marker to indicate the beginning and end of the attached file.
-
Send the message with
postcat:postcat -v message-with-attachment.txtThe
-vflag prints out the message submission process for debugging.
Postfix has excellent stability and deliverability built-in. The main downsides of this approach are needing to understand MIME multipart message formatting, and lacking compatibility for multiple attachments. But for servers with Postfix already running, it can send attachment emails quite reliably.
Emailing Attachments with uuencode
The venerable uuencode utility dates back to early UNIX systems, but it still has unique value today for emailing binary file attachments from the Linux command line.
Here is an overview of using uuencode:
-
uuencode attachment files – This utility converts binary files into an ASCII text representation:
uuencode figures.xls figures.xls > attached.txtYou can concatentate files from multiple uuencode calls into one attachment file.
-
Mail encoded text file – Use normal
mailcommand to send turn attached.txt into an email:mail -s "File Attachments" recipient@domain.com < message_body.txt -
Decode on recipient side – The recipient can reverse the uuencode process using
uudecodeto restore the original file attachments.
The cool thing about uuencode is that it bundles the attachment right into the message body in a consistent way across all mail clients. The downsides are needing to manually encode/decode, and not supporting multipart messages.
But for simple command line file transfers via email, using uuencode and mail works quite nicely.
Automate Sending Attachments with Bash Scripting
One great benefit of sending email attachments from the Linux command line is the potential to automate the process with Bash scripting.
This allows you to schedule sending recurring reports, email notifications from cron jobs, sync updated files with remote systems, and more – all automated with no manual intervention.
Here is one example script that scans a folder for PDF files and emails them as attachments:
#!/bin/bash
# Config
REPORT_DIR=/var/analyses/daily
MAIL_LIST="admin@company.com accounting@company.com"
# Archive folder
PREV_REPORTS=/var/analyses/archive
# Mail subject
SUBJECT="Daily Analysis Reports - $(date +%F)"
# Function to process file attachments
send_reports() {
# Loop through files
for file in "$REPORT_DIR"/*.pdf; do
# Check if reports exist
if [ -f "$file" ]; then
# Send mail with attachments
echo "Automated analysis reports for $(date +%F)" | mutt -s "$SUBJECT" -a "$file" -- $MAIL_LIST
# Archive copied reports
mv "$file" $PREV_REPORTS
fi
done
}
# Call function
send_reports
Breaking this down:
- It scans a folder for PDF files
- Uses Mutt and pipes the message body
- Attaches any found PDFs
- Sends to a mailing list
- Then archives the reports
This allows automatically emailing the files as attachments on a recurring cron schedule.
The same approach can be used for processing files or output from other scripts and mailing the results with attachments. The key is procedurally generating attachments files, formatting the message, and sending with your preferred command line mail tool.
Troubleshooting Email Attachments
When first getting started sending attachments from the Linux command line, you might run into a few issues like:
-
Email sent but no attachments received – Double check attachment file paths passed to the -a arguments. Make sure the files exist on disk.
-
Attachments corrupted – Some non-text attachments like PDFs can get corrupted. Try alternative encoding like uuencode or base64 formats.
-
Emails not arriving – Test basic
mailcommand delivery to the recipient address. Check spam folder. Verify your smtplib configuration works properly. -
Encoding issues – If special characters or encodings like UTF-8 get mangled, make sure your STDIN piping and message handling is ASCII-compatible.
-
Permission errors – The user sending mail may not have access to attachment files. Check permissions on sensitive files.
Start out by verifying basic mail delivery with no attachments works correctly before debugging issues with included files.
Next Steps and Resources
Congratulations – you should now have several solid techniques for delivering attachments directly from the Linux command line.
Some directions for further exploring CLI mail capabilities:
- Learn how to send encrypted PGP messages from the terminal
- Configure
muttor NeoMutt as a fully-featured email client replacement - Use cron jobs to schedule automated report emails
- Tie command line mail functions into shell scripts and applications
The Linux mail and related utilities are quite robust. With a bit of practice, you can leverage command line messaging to automate all sorts of administrative and development workflows.
Here are some additional resources useful as references:
- Postfix documentation – Details on integrating with this popular MTA
- Mutt guide – Examples extending Mutt‘s capabilities
- Linux mail manpages – Man pages for mail, sendmail, uuencode, etc
- Cron tutorial – Learn cron scheduling for script automation
Whether sending occasional attachment emails or creating automated solutions, handling Linux command line mail tasks is a valuable skill to add to your toolbox.


