Categories

Category cover

Automation
35 posts

Category cover

Notes
19 posts

Category cover

Security
19 posts

Category cover

Personal Security
14 posts

Category cover

Infrastructure
11 posts

Category cover

CISO
9 posts

Category cover

OT/ICS
5 posts

Category cover

UNetLab
3 posts

Category cover

Write-up
3 posts

Category cover

Books
2 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="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