How to send an email with a file attachment in Ubuntu?[CLI]

How To Send An Email With File Attachment Through Command Line

Hello guys, In this article, We will discuss how to send an email with a file attachment from the command line. Once you start using Linux, You may wish to do every task using some simple commands. Sending emails is easy through GUI but it can also be done using the command line using some simple commands. Sysadmins and developers generally use the command line to email the daily backup files to a safer place, or remotely. We will discuss all of them below in detail.

1. Send an email using the mail command in Linux

mail command is not available by default. To install the mail command, Execute the following command:

sudo apt install mailutils

Configure your email using Postfix as shown below.


Now, Run the following to send an email with a file attachment:

echo "message body" | mail -s "email subject" <target domain>  -A <attachment file>
echo " this is a test message" | mail -s "test mail" -A test.txt

2. Using the mutt command

To install mutt, run the following command:

sudo apt-get install mutt

mutt is a small but powerful mail agent for sending emails, even to multiple recipients and with file attachments. Run mutt using the following command:

echo "Email Body" | mutt -s "Email Subject" -a <file attachment>
echo "Email Body" | mutt 0s "Email Subject" -A test.txt

3. Using mpack command

mpack command is another command similar to the above commands to send an email with file attachment. It can be used to send email to multiple recipients and even send to newsgroups.

sudo apt install mpack

Run the mpack command:

mpack -s "email subject" -a <file_attachment>
mpack -s " This is a test mail" -a test.txt


So, We discussed different ways to send email with file attachment using command line in Ubuntu. Nowadays, Emails are sent through GUI and there are many platforms to send emails. But, sometimes, you may be working on a server with no GUI installed, These commands will help you send emails with attached files. Thankyou for reading!