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
send-email-cli-1

Configure your email using Postfix as shown below.

send-email-cli-2
send-email-cli-3
send-email-cli-4

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" sidbishnoi36@gmail.com -A test.txt
send-email-cli-5

2. Using the mutt command

To install mutt, run the following command:

sudo apt-get install mutt
send-email-cli-6

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> abc@example.com
echo "Email Body" | mutt 0s "Email Subject" -A test.txt sidbishnoi36@gmail.com
send-email-cli-7

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
send-email-cli-8

Run the mpack command:

mpack -s "email subject" -a <file_attachment> abc@example.com
mpack -s " This is a test mail" -a test.txt sidbishnoi36@gmail.com
send-email-cli-9

Conclusion

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!