Created
April 10, 2012 11:35
-
-
Save cstroie/2350707 to your computer and use it in GitHub Desktop.
Slugsy mail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Simple mail(1) replacement | |
# Software name and version | |
SWNAME="Slugsy-mail" | |
SWVER="0.1" | |
SWSIG="${SWNAME}/${SWVER}" | |
# From | |
FROM="`hostname -s`@`hostname -d`" | |
NAME="Slugsy" | |
SIGN="n-- n${NAME}" | |
# Globals | |
SUBJ="" | |
TO="" | |
CC="" | |
BCC="" | |
BODY="" | |
EMPTY="" | |
VERB="" | |
# Check for positional parameters | |
until [ -z "$1" ] | |
do | |
case "$1" in | |
"-s") shift; SUBJ="$1";; | |
"-c") shift; CC="$1";; | |
"-b") shift; BCC="$1";; | |
"-v") VERB="1";; | |
"-E") EMPTY="1";; | |
*) TO="$1";; | |
esac | |
shift | |
done | |
# Create the headers | |
HDR="" | |
if [ -z "${NAME}" ] | |
then | |
HDR="${HDR}From: ${NAME} <${FROM}>n" | |
else | |
HDR="${HDR}From: ${FROM}n" | |
fi | |
if [ -z "${TO}" ] | |
then | |
echo "Missing recipient's address." | |
exit 1 | |
else | |
HDR="${HDR}To: ${TO}n" | |
fi | |
if [ ! -z "${CC}" ] | |
then | |
HDR="${HDR}Cc: ${CC}n" | |
fi | |
if [ ! -z "${BCC}" ] | |
then | |
HDR="${HDR}Bcc: ${BCC}n" | |
fi | |
if [ -z "${SUBJ}" ] | |
then | |
echo "Missing subject." | |
exit 2 | |
else | |
HDR="${HDR}Subject: ${SUBJ}n" | |
fi | |
# Add the user agent signature | |
HDR="${HDR}User-Agent: ${SWSIG}n" | |
# Read the body | |
if [ -z "${BODY}" ] | |
then | |
while read LINE | |
do | |
if [ "${LINE}" == "." ] | |
then | |
break | |
else | |
BODY="${BODY}${LINE}n" | |
fi | |
done | |
fi | |
# Check if the body is empty | |
if [ -z "${BODY}" -a "${EMPTY}" ] | |
then | |
echo "The email body is empty." | |
exit 3 | |
fi | |
# Send to email account | |
echo -e -n "${HDR}n${BODY}${SIGN}" | /usr/sbin/sendmail -t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment