send email with attachment in linux

How to Send Email With Attachment in Linux

Did you know that you can send emails from Linux command line interface? Sometimes you may need to send email to others, as a part of system administration. There are numerous tools in Linux to help you send emails. In this article, we will look at how to send email with attachment in Linux.


How to Send Email With Attachment in Linux

Here are different commands to send email with attachment in Linux.


1. Using mail

mail is one of the most common commands to send emails in Linux. You can install it with the following commands.

#ubuntu/debian
$ sudo apt-get install mailutils

# redhat/fedora/centos
# yum install mailx

Once it is installed, you can easily send emails with the following command. Replace message body, subject, user email and attachment file name as per your requirement.

$ echo "Message Body Here" | mail -s "Subject Here" user@example.com -A attachment.zip

We use -s option for subject line and -A option for attachment. You can also send message from text files with the following command.

$ sudo mail -s "Subject here" -t user@example.com -A attachment.zip < message.txt

Please note, if you don’t mention full path to attachment file then Linux will look for it in your present working directory.


2. Using mutt

You can also install and use mutt command to send emails. It is a popular light weight email client for Linux. Here is the command to install it.

# ubuntu/debian
$ sudo apt-get install mutt 

# redhat/fedora/centos
# yum install mutt

Once you have installed mutt, you can easily send emails with the following command.

$ echo "Message Body Here" | mutt -s "Subject Here" -a attachment.zip user@example.com

The above command is similar to mail command, and uses -s option for subject and -a option for attachment.


3. Using Mailx

mailx is similar to mutt and is also part of mailutils (Debian). Here is how to install it.

# Ubuntu/Debian
$ sudo apt-get install mailutils 

# Redhat/Fedora/CentOS
# yum install mailx

Just as in mail and mutt, you can easily send emails using mailx as shown below.

$ echo "Message Body Here" | mailx -s "Subject Here" -a attachment.zip user@example.com

Here too we use -s option for subject and -a option for attachment.


4. Using mpack

mpack is another utility that allows you to send messages to one or more recipients, or write to named file or set of files, or post to set of newsgroups. Here is how to install it.

# Ubuntu/Debian
$ sudo apt-get install mpack 

# Redhat/Fedora/CentOS
# yum install mpack

You can easily send message with an attachment using the following command.

$ mpack -s "Subject here" attachment.zip user@example.com

That’s it. In this article, we have learnt four different tools to send mail with attachment in Linux. If you want to send emails regularly in an automated manner, you can simply add cronjob using any of the above commands.

Open crontab with the following command.

$ sudo crontab -e

Add the following line to send email every day at 10 a.m and mute the output.

0 10 * * * echo "Message Body Here" | mail -s "Subject Here" user@example.com -A attachment.zip >/dev/null 2>&1

Save and close the file. Now your Linux system will automatically send email to the recipient everyday, with attachment.

Also read:

How to Remove (Delete) Symbolic Links in Linux
How to Install Zoom in Ubuntu
Shell Script to Backup Files in Directory
How to Disable Apache Configuration File
How to Read Command Line Arguments in NodeJS

Leave a Reply

Your email address will not be published.