Automating Threat Intelligence series
May 04, 2025
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="[email protected]"
BODY_OK="From: Webmaster\nTo: [email protected]\nSubject: Requested files"
EMAIL_ERR="[email protected]"
BODY_ERR="From: Webmaster\nTo: [email protected]\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