The cost of complexity: Ansible AWX
May 05, 2024
Create email with attachment using BASH
I replaced most of my Bash script with Python but sometimes I still need Bash. The following script requires uuencode
and sends an email with attachments using Bash:
#!/bin/bash
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
EMAIL_OK="dainese@example.com"
BODY_OK="From: Webmaster\nTo: undisclosed@example.com\nSubject: Requested files"
EMAIL_ERR="dainese@example.com"
BODY_ERR="From: Webmaster\nTo: undisclosed@example.com\nSubject: Cannot send files"
SOURCE="/path/file/report*.xls"
ZIP="/tmp/report-date +%Y%m%d.zip"
zip -q $ZIP $SOURCE
if [ $?=0 ]; then
( echo -e "$BODY_OK"; uuencode $ZIP basename $ZIP ) | sendmail $EMAIL_OK
else
( echo -e "$BODY_ERR" ) | sendmail $EMAIL_ERR
fi
rm -f $ZIP