Home Linux Commands How to Send An Email With File Attachment from Command Line

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.

Pre-Requisites

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" [email protected]

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" [email protected] -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" [email protected];[email protected],[email protected] -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" [email protected] -A <file to be attached> < mailtext.txt
Conclusion

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!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

1 thought on “How to Send An Email With File Attachment from Command Line”

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.