Send Email Alerts from Shell Script

When you’re automating tasks with shell scripts, it’s often essential to notify yourself (or your team) if something goes wrong—or even when everything goes right! One of the most convenient ways to receive such notifications is via email alerts. In this article, we’ll show you how to send email notifications from a shell script using both the classic mail command and external APIs like SendGrid or Mailgun.


Why Send Email from Scripts?

Shell scripts orchestrating automated jobs (cron jobs, system monitoring, backups, etc.) often need to notify humans about failures or successful completion. Email is universally accessible, requires no extra apps, and can even trigger further automation.


Prerequisites

Depending on your method, you may need:

  • Access to an SMTP server OR an account with an email API provider (eg. SendGrid)
  • Installed Linux tools: mail, curl, or sendmail
  • Script permissions

Method 1: Sending Mail with mail

The mail command is a simple CLI tool for sending mail. Ensure it’s installed (mailutils on Debian/Ubuntu, mailx on CentOS/RHEL):

sudo apt update
sudo apt install mailutils
# OR
sudo yum install mailx

Note: Make sure your server can send emails via SMTP. Sometimes local deliveries work; more often you’ll need to configure /etc/ssmtp/ssmtp.conf or /etc/mail.rc with your SMTP relay.

Example Usage

echo "Backup completed successfully at $(date)" | mail -s "Backup Alert" your@email.com

Explanation:

  • echo sends the body text.
  • -s specifies the subject.
  • The recipient’s email follows.

More Advanced Example

Send output of a script as an email alert:

#!/bin/bash
if ! ./backup-script.sh; then
    mail -s "Backup Failure: $(hostname)" admin@example.com < /var/log/backup.log
fi

Method 2: Using sendmail Directly

sendmail can give you more control:

sendmail admin@example.com <<EOF
Subject: Custom Alert from $(hostname)

The script failed at $(date).
Log output:

$(cat /var/log/script.log)
EOF

This constructs an email message with a subject and a body.


Method 3: Sending Email via External API (SendGrid Example)

For reliability, deliverability, or branding, you may prefer to use a transactional email provider's API.

Steps:

  1. Sign up for SendGrid or Mailgun.
    Obtain API Key and sender email.

  2. Install curl if needed.

    sudo apt install curl
    
  3. Script Example (SendGrid):

#!/bin/bash
API_KEY="YOUR_SENDGRID_API_KEY"
TO="recipient@example.com"
FROM="sender@example.com"
SUBJECT="Script Alert: $(hostname)"
BODY="The script has completed at $(date)"

curl -s --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
    "personalizations": [{"to": [{"email": "'"${TO}"'"}]}],
    "from": {"email": "'"${FROM}"'"},
    "subject": "'"${SUBJECT}"'",
    "content": [{"type": "text/plain", "value": "'"${BODY}"'"}]
}'

Replace the placeholders with your actual email addresses and API key.


Troubleshooting

  • Mail not arriving?
    Check spam/junk, verify SMTP server configuration, and examine mail logs (/var/log/mail.log).

  • API issues?
    Check API credentials, permissions, and sender authentication (DKIM, SPF).


Summary

Whether you need to send a quick notification or integrate with modern email providers, sending email alerts from a shell script is straightforward. The choice of method (mail, sendmail, or API) depends on your system environment, reliability requirements, and permission levels.

Start with mail for simple notifications, or use an API for scalable, reliable delivery.


Further Reading


Automate smarter—never miss a script event again!