Categories

Category cover

Automation
43 posts

Category cover

Security
20 posts

Category cover

Notes
19 posts

Category cover

Personal Security
15 posts

Category cover

CISO
14 posts

Category cover

Infrastructure
12 posts

Category cover

Learning paths
9 posts

Category cover

OT/ICS
6 posts

Category cover

UNetLab
4 posts

Category cover

Books
3 posts

Category cover

Write-up
3 posts

Category cover

OSInt
2 posts

Category cover

My life
1 posts

Create email with attachment using BASH

Andrea Dainese
August 12, 2013
Post cover

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