/ Bash Scripts

Send email notifications

This guide walks you through the creation of a Bash script that sends email notifications. The script will use sendmail, a standard email tool on most Unix-based systems, for simplicity. This guide also covers the integration of user-provided custom options and settings.

Requirements

  • A Unix-like operating system with a Bash shell.
  • sendmail utility installed. This is usually pre-installed on most Linux distributions.
  • Familiarity with the command-line interface and basic scripting in Bash.

Step 1: Creating the Bash Script

Start by creating a new Bash script file. Use any text editor you prefer; nano, vim, or emacs are popular options.

nano send_email.sh

Step 2: Declare the Bash Interpreter

At the start of the script, declare the path to the Bash interpreter using a shebang (#!):

#!/bin/bash

Step 3: Define Variables for Email Components

In this script, we will include the basic components of an email: the recipient’s address, the subject, and the body. Declare these as variables at the top of the script:

recipient=""
subject=""
body=""

Step 4: Handle Command-Line Arguments

Next, we will handle command-line arguments for these variables. This allows users to provide custom input when running the script:

while getopts r:s:b: flag
do
    case "${flag}" in
        r) recipient=${OPTARG};;
        s) subject=${OPTARG};;
        b) body=${OPTARG};;
    esac
done

Here, getopts is used to parse command-line arguments. The r:s:b: argument specifies that the -r, -s, and -b options require an argument.

Step 5: Verify Required Parameters

Before sending the email, the script should verify that all required parameters have been provided:

if [ -z "$recipient" ] || [ -z "$subject" ] || [ -z "$body" ]
then
    echo "All parameters - recipient (-r), subject (-s), and body (-b) - are required."
    exit 1
fi

This block will print an error message and exit the script if any parameter is missing.

Step 6: Construct the Email

Construct the email with the provided components and pass it to sendmail:

(
echo "From: no-reply@yourdomain.com"
echo "To: $recipient"
echo "Subject: $subject"
echo "Content-Type: text/plain"
echo
echo "$body"
) | sendmail -t

This block first sets up the email headers, including the sender, recipient, and subject. The Content-Type is set as text/plain, which indicates a plain text email. The body of the email follows after a blank line.

The entire constructed email is then piped into the sendmail command with the -t flag, which tells sendmail to derive recipients from the email headers.

Step 7: Make the Script Executable

Finally, make your script executable using the chmod command:

chmod +x send_email.sh

Using the Script

Now, you can run your script with custom input like this:

./send_email.sh -r "recipient@example.com" -s "Test Email" -b "Hello, this is a test email."

Conclusion

This script provides a basic example of a Bash script that sends email notifications. It can be further customized to suit your specific needs. You might, for example, modify it to read recipients from a file, attach files, or send HTML emails.

Remember, sending email from a script can trigger spam filters depending on the email server configuration. Be sure to check these factors when deploying such a script.

Was this helpful?

Thanks for your feedback!