1. Overview
In this document, we will explore how to create a simple yet effective shell script to send email alerts with multiple attachments.
2. Technologies and Tools Used
The following technologies has been used to achieve the expected output.
- Oracle PLSQL
- UNIX/Linux.
3. Use Case
The below shell script which created for sending email alerts with multiple attachments can be useful in various scenarios where automated notifications and data sharing are required.
Schedule the script to run after the completion of critical database jobs. Attach relevant reports or data files to notify stakeholders about the job status and results.
Automate the distribution of daily or weekly reports to specific teams or individuals. Attach the reports as necessary, ensuring that stakeholders are kept informed without manual intervention.
If you are frequently transfers files between systems or partners, use the script to notify relevant parties upon successful file transfers. Attach acknowledgment files or transfer logs.
4. Architecture
Let’s look into a shell script named Email_alert_multiple_attachment.sh that automates the process of sending email alerts with attachments.
Shell script:
#!/bin/bash
# Script Name: Email_alert_multiple_attachment.sh
# Comments: Sending email alert with multiple attachments using shell script
echo “==========================================================================”
echo “$0 Started at : ” $(date)##
# Check if another instance is running
scr_running=$(cat /path/Email_alert_multiple_attachment.chk | wc -l)
chk_flag=$(expr $scr_running)
if [ $chk_flag -ne 0 ]; then
echo “Another instance of the script is already running”
exit
fi
echo “RUNNING” > /path/Email_alert_multiple_attachment.chk
# Execute SQL procedure
sqlplus -s username/pwd@DB << EOF
exec PROC1;
EOF
echo “$0 Ended at : ” $(date)
echo “==========================================================================”
# File Attachments
attach1=””
for fn in $(ls /PATH/FILENAME*.xls); do
[ -f “$fn” ] && attach1=”$attach1 $fn”done
attach2=””
for fn in $(ls /PATH/FILENAME*.xls); do
[ -f “$fn” ] && attach2=”$attach2 $fn”done
# Check for file existence
openCSI_SG_SUMMARY=$(ls -ltr /PATH/FILENAME*.xls | wc -l)
openCSI_SG_DETAIL=$(ls -ltr /PATH/FILENAME*.xls | wc -l)
# Send email if attachments exist
if [ $openCSI_SG_SUMMARY -ne 0 ]; then
(uuencode $attach1 $attach1; uuencode $attach2 $attach2) | mailx -s “Subject Message” -r “sender@xyz.com” receiver@xyz.com
# Cleanup
> /path/Email_alert_multiple_attachment.chk
fi
Understanding the Script
Install Necessary Utilities:
Verify that all utilities used in the script, such as mailx and uuencode, are installed on the system where the cron job will run. Install them if necessary using your package manager.
Concurrency Control:
The script employs a check file (Email_alert_multiple_attachment.chk) to prevent multiple instances from running concurrently, ensuring data integrity and preventing conflicts.
Database Interaction:
The script interacts with a database by executing a stored procedure (PROC1) using sqlplus. Ensure proper permissions and configurations for secure database access.
File Attachments:
It identifies and attaches files with names matching the pattern /PATH/FILENAME*.xls to the email. The script checks for file existence before proceeding.
Email Sending:
The email is sent using the mailx command with specified subject, sender, and recipient addresses. The email body is left blank.
Cleanup:
After a successful execution, the script cleans up by removing the check file.
5. Conclusion
Automation of email notifications with multiple attachments is a powerful way to keep stakeholders informed and streamline communication processes. The provided shell script serves as a foundation for building a robust notification system. Adapt and customize it according to your organization’s needs, and embrace the efficiency that automation brings to your daily operations.