- Posted on
- • Administration
How to Send Email Notifications from Bash Scripts
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Sending email notifications from a Bash script can be useful for alerts, reports, or task completion updates. Here's how to achieve this on a Linux system:
1. Prerequisites
Ensure you have an email client or mail transfer agent (MTA) installed. Common options include:
- mail
or mailx
: Simplest email clients.
- sendmail
: Powerful MTA for advanced configurations.
- ssmtp
or msmtp
: Lightweight MTAs, often used with Gmail or other SMTP services.
- mutt
: Versatile email client for sending attachments.
2. Using mail
or mailx
Step 1: Install mailx
Install the package (if not already installed):
sudo apt install mailutils # For Debian/Ubuntu
sudo yum install mailx # For CentOS/RHEL
Step 2: Send an Email
Basic command:
echo "Email body text" | mail -s "Subject Here" recipient@example.com
Step 3: Use in a Bash Script
Example:
#!/bin/bash
# Variables
EMAIL="recipient@example.com"
SUBJECT="Backup Status"
BODY="The backup job completed successfully at $(date)."
# Send email
echo "$BODY" | mail -s "$SUBJECT" $EMAIL
3. Sending Attachments with mail
To send a file as an attachment:
echo "Email body text" | mail -s "Subject Here" -A /path/to/file recipient@example.com
4. Using sendmail
Step 1: Install sendmail
sudo apt install sendmail # Debian/Ubuntu
sudo yum install sendmail # CentOS/RHEL
Step 2: Send an Email
Example:
#!/bin/bash
# Email details
FROM="your_email@example.com"
TO="recipient@example.com"
SUBJECT="Task Status"
BODY="The scheduled task completed successfully."
# Build the email
{
echo "From: $FROM"
echo "To: $TO"
echo "Subject: $SUBJECT"
echo
echo "$BODY"
} | sendmail -t
5. Using ssmtp
or msmtp
These tools are ideal for sending emails via SMTP (e.g., Gmail).
Step 1: Install ssmtp
or msmtp
sudo apt install ssmtp # For lightweight SMTP
sudo apt install msmtp # Alternative lightweight SMTP
Step 2: Configure SMTP
Edit the configuration file (/etc/ssmtp/ssmtp.conf
or /etc/msmtprc
) and include your SMTP server details:
# Example for Gmail
root=your_email@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=your_email@gmail.com
AuthPass=your_password
UseSTARTTLS=YES
Step 3: Send Email
echo "Email body text" | ssmtp recipient@example.com
6. Using mutt
for Advanced Features
Step 1: Install mutt
sudo apt install mutt
Step 2: Send an Email with an Attachment
echo "Email body text" | mutt -s "Subject Here" -a /path/to/attachment -- recipient@example.com
7. Using Gmail API or External Libraries (Advanced)
For secure and scalable email notifications, you can use Python scripts or APIs like the Gmail API. This is useful when integrating with larger applications.
8. Debugging Email Issues
- Check Logs: Email errors are often logged in
/var/log/mail.log
or/var/log/maillog
. - Test SMTP Server:
bash telnet smtp.example.com 25
- Verify Installation:
Ensure the necessary packages (
mail
,sendmail
, etc.) are correctly installed and configured.
9. Practical Example: Email Notifications for a Cron Job
Here's how to notify on job completion:
#!/bin/bash
# Task
tar -czf /backup/documents.tar.gz /home/user/documents
# Check if the command succeeded
if [ $? -eq 0 ]; then
echo "Backup completed successfully" | mail -s "Backup Success" admin@example.com
else
echo "Backup failed" | mail -s "Backup Failure" admin@example.com
fi
This guide provides multiple methods for sending email notifications from Bash scripts.