How to Send An Email With File Attachment from Command Line

The key to becoming an advanced Linux user is to use more of the command line and less of the GUI; more of the keyboard and less of the mouse! As the diaspora of Linux command-line tools grows, not only administrative but several non-administrative, in fact, crucial day-to-day tasks, are performed using the command line.

In this article, we will learn how to send an email with a file attachment using the mail command in Linux.


You must have already configured your Email with SMTP in your Linux machine. This Email and server will be made use of by the ‘Mail‘ program which we will learn about today.

Sending an Email from Command Line in Linux

The program ‘mail’ can be used to send an Email from the command line, along with file attachments with the Email. This program is not available by default, and can be installed in Debian and Red Hat-based distributions using:

$ sudo apt install mailutils   [On Debian/Ubuntu/Mint]
$ sudo dnf install mailx       [On RedHat/CentOS/Fedora]

The syntax to send an Email using ‘mail’ is as follows:

$ echo "Email Message Body" | mail -s "Subject of the Email"

You can see that we are using the echo command to output the message body and redirecting this output to the ‘mail’ command. This is because the ‘mail’ command reads message body input from the standard input.

Sending an Email with File Attachment from Command Line

Similarly, to attach a file with the mail, the argument '-A' can be used:

$ echo "Email Message Body" | mail -s "Subject of the Email" -A <file to be attached>
Send an Email with File Attachement from Terminal
Send an Email with a File Attachment from Terminal

Sending an Email to Multiple Recipients from Command Line

To send the email to multiple recipients, simply specify the multiple Email IDs separated by a comma.

$ echo "Email Message Body" | mail -s "Subject of the Email";, -A <file to be attached>
Send Email to Multiple Recipients
Send Email to Multiple Recipients

To include a text file as the message body of the Email, instead of using echo for the same, you can redirect the text of a file to the command as shown below:

$ mail -s "Subject of the Email" -A <file to be attached> < mailtext.txt

Today we have seen a way to send an Email from the Linux command line along with an attachment. There are some other programs like ‘mutt‘ and ‘Sendmail‘ which are similar to ‘mail‘ and can be used for the same purpose.

Thanks for reading and let us know your thoughts or questions in the comments below!

Got something to say? Join the discussion.