Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to send a file as an email attachment using the Linux command line?
In order to send a file as an attachment to an email using Linux, we can use command line email clients or the standard Linux mail command. This is particularly useful for automation, scripting, and server environments where GUI email clients are not available.
There are multiple command line email clients available to achieve the attachment task, but the most common and widely used is mutt.
Using Mutt Email Client
Mutt is a command line based email client that allows us to send and read emails from command line in Linux based systems. It also supports important protocols like IMAP and POP which broadens the use-cases and application of mutt.
Some of the key features that Mutt has are −
Easy to configure and install
Allows us to send attachments with emails
Allows message threading
Supports multiple languages
MIME support for various file types
GPG/PGP encryption support
Installing Mutt
In order to make use of mutt, we first need to install it on our local systems. Consider the commands shown below for different versions that will help in installing mutt on the local machine.
For Linux Based Systems (Ubuntu/Debian) −
apt-get install mutt
For CentOS/RHEL/Fedora Systems −
yum install mutt # or for newer versions dnf install mutt
For Mac OS −
brew install mutt
Sending Email with Attachment using Mutt
Now once mutt is installed, we can use the following command to send an email with attachment −
echo "Message Body" | mutt -a "/path/to/file_to_attach" -s "subject of the message" -- recipient@domain.com
Let's break down the above command to understand it better −
mutt− The command to invoke the mutt email client-a− Flag to denote that we are attaching a file, followed by the file path-s− Flag to specify the subject line of the email--− Separator to indicate end of optionsrecipient@domain.com− The destination email address
Multiple Attachments with Mutt
To send multiple attachments, use multiple -a flags −
echo "Multiple files attached" | mutt -a file1.txt -a file2.pdf -a image.jpg -s "Multiple Attachments" -- recipient@domain.com
Using the Mail Command
An alternate way is to make use of the mail command utility that Linux provides. The mail command is simpler but offers fewer features compared to mutt.
mail -s "Backup" -a mysqldbbackup.sql sample@email.comIn this command −
-s− Specifies the subject line-a− Attaches the specified file− Reads the message body from a text file
Comparison
| Feature | Mutt | Mail Command |
|---|---|---|
| Multiple attachments | Yes (multiple -a flags) | Limited support |
| MIME support | Full support | Basic support |
| Configuration options | Extensive | Basic |
| Learning curve | Moderate | Simple |
| Protocol support | IMAP, POP, SMTP | SMTP only |
Conclusion
Both mutt and the mail command can send email attachments from the Linux command line. Mutt offers more advanced features and better MIME support, making it ideal for complex email tasks. The mail command is simpler and perfect for basic automation scripts and quick file transfers.
