Email

How to Send Email with Attachments from Command Line in Linux

Most computer users are probably familiar with the simple process of sending and receiving emails. Apart from simple text conversations, emails can also be used for sending and receiving files. These files are transferred inside of an email as attachments. Any email client of your choice may be used for sending and receiving emails with attachments.

As a Linux user, you might prefer terminal-based methods of sending emails with attachments. This article shows you four different methods of sending emails with attachments from the command line in Linux Mint 20.

You can use any of the following four methods to send an email with attachments from the command line in Linux Mint 20.

Note: For all the methods discussed below, attached the sample text file named abc.txt to every email. You can also attach other kinds of files, such as PDFs, spreadsheets, images, audios, and more.

Method 1: Using the “mutt” Package

The “mutt” package is not installed on Linux systems by default. So, you will first need to install this package by running the command below in your terminal:

$ sudo apt install mutt

Once the “mutt” package is installed on your Linux Mint 20 system, along with all the required dependencies, you can run further commands, as shown in the following image:

After the “mutt” package has been installed on our system, you can use this package to send an email with attachments in the manner shown below:

$ echo “Sample Email Body” | mutt –s “Sample Email Subject”
 –a SampleAttachment SampleEmailID

Here, replace “Sample Email Body” with the actual body of your email; “Sample Email Subject” with the actual subject of your email; “SampleAttachment” with the path of the file that you want to attach (you can also attach multiple files in a single email simply by mentioning the paths of all the files to be attached, separated by spaces); and “SampleEmailID” with the email ID of the desired recipient to whom you wish to send the email.

Method 2: Using the “mail” Command

The “mail” command is included in the “mailutils” package, which does not come installed on Linux systems by default. So, to use this method, you must first install this package by running the following command in your terminal:

$ sudo apt install mailutils

Once the “mailutils” package is installed on your Linux Mint 20 system, along with all the required dependencies, you can run further commands, as shown in the image below:

After the “mailutils” package has been installed on your system, you can use this package to send an email with attachments in the following manner:

$ echo “Sample Email Body” | mail –s “Sample Email Subject”
 SampleEmailID –A SampleAttachment

Here, replace “Sample Email Body” with the actual body of your email; “Sample Email Subject” with the actual subject of your email; “SampleAttachment” with the path of the file that you want to attach (you can also attach multiple files to a single email simply by mentioning the paths of all the files to be attached, separated by spaces); and “SampleEmailID” with the email ID of the desired recipient to whom you wish to send the email.

Method 3: Using the “mailx” Command

The “mailx” command is also included in the “mailutils” package and, since you have probably already installed this package in Method 2, you do not have to install the package again. Instead, you can use the “mailx” command to send an email with attachments in the manner shown below:

$ echo “Sample Email Body” | mailx –s “Sample Email Subject”
 –a SampleAttachment SampleEmailID

Here, replace “Sample Email Body” with the actual body of your email; “Sample Email Subject” with the actual subject of your email; “SampleAttachment” with the path of the file that you want to attach (you can also attach multiple files to a single email simply by mentioning the paths of all the files to be attached, separated by spaces); and “SampleEmailID” with the email ID of the person to whom you wish to send the email.

Method 4: Using the “mpack” Package

The “mpack” package is also not installed on Linux systems by default. You can install this package by running the command below in your terminal:

$ sudo apt install mpack

Once the “mpack” package has been installed on your Linux Mint 20 system, along with all the required dependencies, you can run further commands, as shown in the following image:

After the “mpack” package has been installed on our system, you can use this package to send an email with attachments in the manner shown below:

$ mpack –s “Sample Email Subject” –a SampleAttachment SampleEmailID

Here, replace “Sample Email Subject” with the actual subject of your email; “SampleAttachment” with the path of the file that you want to attach (you can also attach multiple files to a single email simply by mentioning the paths of all the files to be attached, separated by spaces); and SampleEmailID with the email ID of the person to whom you wish to send the email.

Conclusion

By using any of the four methods discussed in this article, you can conveniently send as many emails with attachments as you would like. These methods are quite simple and straightforward, and after installing the required packages, it just takes a single command to send an email with an attachment via the command line. With these commands at your disposal, sending emails with attachments could never be easier than via the terminal.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.