How to Send Email in Linux with Attachment [With Examples]

If you have a requirement to write a script that sends email notifications to a single email id or multiple email ids, then this post is for you. This article will share everything about “how to send email in Linux” with attachments or without. While there are multiple ways of sending emails in Linux command line, you will know the simplest way using mailx command.

Read through the complete article and follow the exact steps, you will have the email feature working on your Linux script, be it bash, ksh, or any other.

1. Install mailx:

The installation of the mailx feature varies from one operating system to another. Below are the commands you need to run for installing mailx features for the most common Linux operating systems i.e. Redhat, Ubuntu, Debian, and CentOS.

 
# Installing mailx in RedHat and CentOS
$ yum install mailx

# Installing mailx in Ubuntu and Debian
$ sudo apt install mailx
 

2. Send email in Linux using mailx:

Using the mailx command gives you the flexibility to use various flags like email subject, attachment, cc, bcc, etc while sending emails in Linux. Here are the flags that you can use for achieving your requirement.

-semail subject
-aemail attachment
-ccopy email address (cc)
-bblind copy email address (bcc)

The below example sends an email in Linux with an email body, email subject, and an attachment.

$ echo "Email Body" | mailx -s "Email Subject" -a /tmp/log.txt john.doe@email.com

Send email with multiple attachments:

If you want to add multiple attachments, you can include multiple files in the mailx -a flag to send email in Linux.

$ echo "Email Body" | mailx -s "Email Subject" -a /tmp/log1.txt /tmp/log2.txt john.doe@email.com

Send email with cc / bcc:

If you want to copy (cc) or blind copy (bcc) some email addresses in your email in Linux, here is an example for you.

$ echo "Email Body" | mailx -s "Email Subject" -a /tmp/log.txt john.doe@email.com -c email1@email.com -b email2@email.com

Note:
The email is sent from the user id that runs the script and from the server where the script is running. If the user id running the mailx command is dbauser and the script is running on a server with the name server123.google.com, then the email is sent from the email id dbauser@server123.google.com

Read email ids from a text file and send email in Linux:

If you have a scenario where the email ids are present in a file in each line, you can use the below code snippet to read each line from the file and send an email to each line’s content.

EMAILCONFIGFILE=/home/user/email_config.txt

while read -r EMAILID; do
    echo "Email Body" | mailx -s "Email Subject" $EMAILID
done < "$EMAILCONFIGFILE"

This example reads one email id in each line from the EMAILCONFIGFILE and sends the email. This continues till an empty line is encountered.

Final words:

That’s all, isn’t it easy? You can tweak the mailx command with different combinations to satisfy your requirement of sending email in Linux using mailx command with attachment or without. I hope this was useful to you.

Let us know in the comments if you have any questions, we would love to answer them. Also, you can comment on any other tips that helped you work around the mailx command for our fellow readers.

Cheers!!

Leave a Comment

Your email address will not be published.

Scroll to Top