Bash Script to Send Email With Attachment [Step-by-Step Guide]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

There are multiple ways of sending emails with attachments. Here I am showing you how you can send emails by generating an app-specific password while using Gmail. The mailx command will be used to send the mail. The -A option in mailx enables attaching files to the email. This demonstration will show you the whole process step by step.

Key Takeaways

  • Sending email with the mailx command
  • Sending mail with attachment.
  • Different options of mailx command.

Free Downloads

Sending Email With Attachment by Setting up SMTP Server

Before sending an email with an attachment using Bash script there are multiple steps you must follow properly. I am going to discuss all the steps from generating an app-specific password to writing a Bash script for sending email in the following steps.

Step 1: Creating an App-Specific Password

The first step of sending an email with an attachment is to create an App-Specific Password. If you are a Gmail user follow the steps below to generate an App-Specific Password.

❶ At first, enable the 2FA. Follow this documentation to enable 2FA on Gmail.

❷ Once you have 2FA enabled, visit this site to generate an app-specific password. Read more about generating app-specific password from this article.

Step 2: Installing “mailx” command

The mailx command may not be already installed in your system. Run the following command to install it. Go through this documentation if you face any issues while installing the mailx command.

sudo apt-get install mailutils
EXPLANATION
  • sudo: Give the root privilege.
  • apt-get: apt-get is the command-line tool for handling packages using the APT
  • install: Install packages of the specified name.
  • mailutils: A rich set of utilities for handling electronic mail.

Install SMTP using the following command:

sudo apt-get install ssmtp
EXPLANATION
  • ssmtp: sSMTP delivers email from a local computer to a configured mailhost.

Run the following command to configure the smtp configuration file:

sudo nano /etc/ssmtp/ssmtp.conf
EXPLANATION
  • nano: Opens a file in the nano text editor.
  • /etc/ssmtp/ssmtp.conf: The path of the smtp configuration file in the system.

Read this guide to know more about installing and configuring the smtp configuration file.

Note: The sender mail is the mail that is set as the SERVER in the configuration file. I set my gmail (zahidlaku72gmail.com) as the SERVER. So zahidlaku72gmail.com will be the sender of the mail.

Step 3: Specific Location of Attachment File

The file you want to attach with your email must be located somewhere in your system. You have to specify the pathname of the file while writing a script for sending email. Hence a clear idea about the attachment file location is necessary. For example, the Bash_Script.pdf can be found in the following path of my machine and I am going to attach this file with my email.

/home/laku/Bash_Script.pdf

Step 4: Bash Script for Sending Emails

At this point, we are going to write a Bash script that will ultimately send the email with an attachment.

Steps to Follow >

❶ Write the following command to open a sendmail.sh file in the build-in nano editor:

nano sendmail.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • sendmail.sh: Name of the file.
Creating a .sh file to send email with attachment using bash script❷ Copy the following script and paste it into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.

Script (sendmail.sh) >

#!/bin/bash

# Email details

recipient="[email protected]"
subject="Bash Script"
body="This is a cheat sheet that contains all the basics of Bash scripting. Read this for primary idea about Bash scripting"

# File attachment
attachment="/home/laku/Bash_Script.pdf"
# Check if the attachment file exists
if [ ! -f "$attachment" ]; then
echo "Attachment file not found: $attachment"
exit 1
fi
# Sending the email with attachment
echo -e "$body" | mailx -s "$subject" -A "$attachment" "$recipient"
echo "Email sent successfully with attachment: $attachment"
EXPLANATION

This Bash script is designed to send an email with an attachment using the mailx command. The recipient‘s email address, email subject and the email body content is set initially. The path to the attachment file Bash_Script.pdf is stored in the attachment variable.

Before sending the email, the script checks if the attachment file exists using an if statement. If it exits the script proceeds to send the email with the specified details. The email body is passed to the mailx command using echo command. The -A option is used to attach the file. Once the email is sent successfully, a confirmation message is displayed, indicating the name of the attached file.

➌ Now, Use the following command to make the file executable:

chmod u+x sendmail.sh

Changing permission of the script

EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • sendmail.sh: Name of the script.

➍ Finally, Run the script by the following command:

./sendmail.sh

Executing the script to send email with attachment

When executed, this script will send an email to the recipient [email protected] with the subject “Bash Script” and the attachment Bash_Script.pdf with the given body text.

Conclusion

In summary, sending emails with attachments has become a routine activity in modern days. Although sending emails with attachments using a Bash script can be challenging, I believe now you can easily accomplish this task. Hope you enjoy reading this article.

People Also Ask

Why sending emails with an attachment shows invalid header?
Due to the incorrect option it may show an invalid header when you send an email with an attachment using the mailx command. Always remember the -a is used for appending and -A option is used to attach files while using mailx command.
Why I can’t find the SMTP configuration file in the system?
You may not find the SMTP configuration file because it is not properly installed in your system. Install the package using sudo apt-get install ssmtp command.
Can I attach multiple files while sending an email using mailx?
Yes, you can. Specify the path of all the attachments and send those using mailx with -A option.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment