Jump to content

Massive Email Client

- - - - -

  • Please log in to reply
1 reply to this topic

#1
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Sometime in early January I started taking this class that required development both on Windows and Linux. As such, I was constantly moving files to and from the Linux servers at school, which became a real pain because the Windows computers didn't have an FTP client. So I'd email the stuff to myself using the sendmail program.
After a while that got annoying, so I partially automated it with a little script. Eventually I started adding a feature or two here and there, and now it's grown into this 700-line beast. Pretty useful, though.

A few caveats:
- I strongly suggest you run shellmail -h and read the entire help page before you use this. There are some funky things here.
- I am not liable for any damages whatsoever directly or indirectly caused by use of this script, negligent or otherwise.

I've commented it pretty well, but if you have any questions on how it works, just ask. I'll be around. :)
Oh, and if you find a bug, please let me know. My email is dargueta@codecall.net.

#!/bin/bash

################################################################################
# FUNCTION DECLARATIONS

function getMIMEType()
{
	echo $(file -biLp $1)
}

function getFileSize()
{
	local ANS=$(stat -L --printf=%s $1)
	local RVAL=$?
	if [[ $RVAL -ne 0 ]]; then
		echo -n "0"
	else
		echo -n "$ANS"
	fi
}

function randomFile()
{
	echo -n "/tmp/$$-${RANDOM}${RANDOM}-${RANDOM}${RANDOM}.$1"
}

function encodeToBase64()
{
	local TMPFILE=$(randomFile "tmp")
	cat $1 | uuencode -m /dev/stdout | sed -e '1d' > $TMPFILE
	echo -n "$TMPFILE"
}

function encodeToQuotedPrintable()
{
	# Convert file to quoted-printable encoding
	echo "ERROR: QUOTED-PRINTABLE NOT SUPPORTED YET"
	exit 6
}

function attachFile()
{
	local MSGFILE=$1			# Email message file
	local BOUND=$2			  # Message part boundary
	local ATTACHMENTFILE=$3	 # File to attach

	# Content-ID of the file. If none is provided we make a random one up.
	local CONTENTID=${4:-udef${$}${RANDOM}}

	# Get the simple file name of the file we're attaching. Strip off all the
	# leading directory entries to get the file name. For example, if we're
	# passed "/usr/bin/shellmail.sh" we would turn this into "shellmail.sh".
	local FILENAME=$(echo -n "$ATTACHMENTFILE" | sed 's/.*\///g')

	# Get the MIME type of the file we're attaching
	local FILETYPE=$(getMIMEType $ATTACHMENTFILE)

	# if we get something weird, then complain.
	if [[ $FILETYPE = 'application/x-not-regular-file' ]]; then
		echo "Error: '$MSGFILE' isn't a regular file.";
		exit 9
	fi

	# Transfer encoding for the file
	local XFERENC=""

	# Examine the MIME type and see if the character set is listed as anything
	# other than text.
	# If so, we need to encode it in Base64 to email it successfully.
	if [[ $FILETYPE =~ "text/" ]]; then
		ATTACHMENTFILE=$(encodeToBase64 $ATTACHMENTFILE)
		XFERENC="base64"
	else
		# File is a text file, but we need to check if it's in anything other
		# than ASCII or ISO-8859-1. The ! is required because =~ returns 0 if
		# the regex matches.
		if [[ ! $FILETYPE =~ "utf" ]]; then
			ATTACHMENTFILE=$(encodeToQuotedPrintable $ATTACHMENTFILE)
			XFERENC="quoted-printable"
		else
			XFERENC="7bit"
		fi
	fi

	# Get the size of the file we're going to attach. Notice that we hold off
	# calculating the size until here; this is in case we have to base64-encode
	# the file, which will change the size.
	local FILESIZE=$(getFileSize $ATTACHMENTFILE)

	# Calculate the new message size
	local TOTALSIZE=$(( $(getFileSize $MSGFILE) + $FILESIZE ))

	echo "
--$BOUND
Content-Type: $FILETYPE
Content-Disposition: attachment; filename=\"$FILENAME\"
Content-Length: $FILESIZE
Content-Transfer-Encoding: $XFERENC
Content-ID: <$CONTENTID>
" >> $MSGFILE

	# Stick the attachment file in.
	cat $ATTACHMENTFILE >> $MSGFILE

	# If we base64-encoded the file we attached, then we need to delete the
	# leftover temporary file.
	if [[ $XFERENC =~ "base64" ]]; then
		rm -f $ATTACHMENTFILE
	fi

	# Return the new size of the email
	echo -n "$TOTALSIZE"
}

################################################################################
# ENTRY POINT
#
# declare arrays of recipients
declare -a TOLIST
declare -a CCLIST
declare -a BCCLIST
VERSION="2.33"
VERSIONDATE="3 Dec 2011"
TOINDEX=0						   # index of next open recipient array element
CCINDEX=0						   # index of next open CC array element
BCCINDEX=0						  # index of next open BCC array element
DEBUG=0							 # debug flag
SERVER=$(hostname -f)			   # server name
DOMAIN=$(hostname -d)			   # domain name
USERNAME=$(whoami)				  # user name
SENDER="${USERNAME}@${DOMAIN}"	  # figure out default sender email
MESSAGETYPE="text/plain"			# default message body type
ORIGINALMSGID=""					# Original message ID
SUBJECT="No Subject"				# default subject
REPLYTO=""						  # reply-to email
TOTALSIZE=0						 # total message size
MAXIMUMSIZE=50					  # maximum message size in MiB
MAXIMUMSIZESPECIFIED=0			  # maximum message size specified by user
ZIP=0							   # if nonzero, then zip attachments together
MESSAGESTRING=""					# body of the message
BODYFILE=""						 # file to use as the body of the message
USEBODYFILE=0					   # flag:
									# 0 - use message as body
									# 1 - use user file as body
									# 2 - use temp interactive file as body
INTRMESSAGEFILE=""				  # interactive message file
EDITOR=""						   # text editor for interactive message mode
INTERACTIVE=0					   # 1 - in interactive mode
SIGFILE=""						  # Signature file

################################################################################
# Before we do anything, we need to make sure that SENDMAIL is installed.
if [[ ! -x $(which sendmail) ]]; then
	# No sendmail installed. Explode.
	echo -e "

\e[1mFATAL ERROR: You do not have the sendmail program installed.
You \e[4mcannot\e[0;1m send any emails from this machine.\e[0m

"
	exit 7
fi
################################################################################
# Get our arguments
while getopts ":t:f:M:m:l:z:e:c:b:r:s:R:Sihd" flag
do
	case $flag in
		"t")
			# append recipient to the list
			TOLIST[$TOINDEX]=$OPTARG
			TOINDEX+=1
			;;
		"f")
			SENDER=$OPTARG
			;;
		"m")
			# use a simple message
			MESSAGESTRING=$OPTARG
			BODYFILE=""
			USEBODYFILE=0
			;;
		"M")
			# use a file for the message
			MESSAGESTRING=""
			BODYFILE=$OPTARG
			USEBODYFILE=1
			MESSAGETYPE=$(getMIMEType $OPTARG)
			;;
		"e")
			# specify text editor for interactive message
			EDITOR=$OPTARG
			;;
		"i")
			# enable interactive mode
			INTERACTIVE=1
			;;
		"l")
			# message size limit in megabytes
			MAXIMUMSIZE=$OPTARG
			MAXIMUMSIZESPECIFIED=1
			;;
		"R")
			# Reference to original message, i.e. the one we're responding to
			ORIGINALMSGID=$OPTARG
			;;
		"c")
			# append CC recipient to the list
			CCLIST[$CCINDEX]=$OPTARG
			CCINDEX+=1
			;;
		"b")
			# append BCC recipient to the list
			BCCLIST[$BCCINDEX]=$OPTARG
			BCCINDEX+=1
			;;
		"z")
			# user requested to zip files
			ZIP=$OPTARG
			;;
		"s")
			# user-specified subject line.
			SUBJECT=$OPTARG
			;;
		"S")
			# user wants a signature appended to the email
			SIGFILE="$HOME/.shmail.$USERNAME.sig"
			if [[ ! -f $SIGFILE ]]; then
				echo -e "
Error: Cannot append a signature to this email because there is no signature
file. If you want your own signature, please create this file and put the text
in there:
\e[4m$SIGFILE\e[0m
"
				exit 4
			fi
			;;
		"r")
			# Reply-to header
			REPLYTO=$OPTARG
			;;
		"h")
			# help for options
			echo -e "SHELLMAIL(1)														SHELLMAIL(1)

\e[1mNAME\e[0m
	ShellMail - An easy-to-use command-line tool for sending emails.
	Version $VERSION created on $VERSIONDATE

\e[1mCOPYRIGHT\e[0m
	Released under the GPL3 license. No rights reserved.

\e[1mDESCRIPTION\e[0m
	ShellMail is a simple utility used to quickly send files through email from
	the command line. Any kind of file whose MIME type is recognizable by the
	\e[1mfile\e[0m utility can be added as an attachment. A message can be as simple as a
	a single line such as \"Hello!\" or as complicated as an HTML email with
	attached files. (Currently there is no support for embedded content.)

	You must have \e[1msendmail\e[0m installed for this script to work.

	Planned improvements / features
		o   Allow sender specification (different than \"from\")
		o   Read & receipt notification
		o   Embedded images in HTML messages
		o   Support for quoted-printable encoding
		o   Support for attaching entire directory trees without globbing

\e[1mSYNOPSIS\e[0m
	\e[1mshellmail -t \e[0;4mrecipient\e[0m [\e[1m-f \e[0;4msender\e[0m] [\e[1m-m \e[0m'\e[4mmsg-string\e[0m' | \e[1m-i\e[0m | \e[1m-M \e[0;4mmsg-file\e[0m]
				[\e[1m-e \e[0;4meditor\e[0m] [\e[1m-c \e[0;4mcc-recipient\e[0m] [\e[1m-b \e[0;4mbcc-recipient\e[0m] [\e[1m-l \e[0;4msize-limit\e[0m]
				[\e[1m-s \e[0m'\e[4msubject-line\e[0m'] [\e[1m-S\e[0m] [\e[1m-z \e[0;4mn\e[0m] [\e[1m-d\e[0m] [\e[1m-r \e[0;4mreply-to\e[0m] \e[1;4mFILES\e[0m

\e[1mOPTIONS\e[0m
	\e[1m-b \e[0;4mbcc-recipient\e[0m
		Address to BCC the message on. This option may be used more than once to
		specify multiple BCC addresses.

	\e[1m-c \e[0;4mcc-recipient\e[0m
		Address to CC the message on. This option may be used more than once to
		specify multiple CC addresses.

	\e[1m-d\e[0m
		Debug messages - do not delete the temporary files after sending the
		email, and instead print out the locations of all the temp files used
		while creating the message.

	\e[1m-e \e[0;4meditor\e[0m
		Lets you specify your own text editor for interactive mode.

	\e[1m-f \e[0;4msender\e[0m
		The sender of the email. If no value is specified, the current username
		and host domain will be used. In this case the default would be:
		\e[4m$SENDER\e[0m.

	\e[1m-i\e[0m
		Interactive mode. This will allow you to type your email upon executing
		the program, instead of specifying a prewritten file or a string on the
		command line. ShellMail will attempt to use \e[1mgedit\e[0m, \e[1mvim\e[0m, and \e[1mnano\e[0m in
		that order. If no other programs are found, it will exit with an error.

	\e[1m-l \e[0;4msize-limit\e[0m
		The maximum allowable size of an email, in megabytes. The default value
		is 32, but you can put any size you want. Note that if it exceeds your
		system's limit, the email will bounce back. If you specify more than one
		size limit, only the last one will take effect.

	\e[1m-m \e[0m'\e[4mmessage-string\e[0m'
		A simple string that will be used as the message in the email.

	\e[1m-M \e[0;4mmessage-file\e[0m
		Specifies a file whose contents will be the message in the email.

	\e[1m-r \e[0;4mreply-to\e[0m
		Redirect responses to this email instead of the sender. Only one may be
		specified. If more than one is passed, only the last one takes effect.

	\e[1m-R \e[0;4mmessage-id\e[0m
		Original message ID; this is the ID of the email you're responding to.
		This enables the recipient's email client to thread the emails and make
		it obvious which one you're replying to.

	\e[1m-s \e[0m'\e[4msubject-line\e[0m'
		Specifies the subject line of the email. If none is specified, the sub-
		ject field in the email will be blank. Spaces \e[1mmust\e[0m be escaped.

	\e[1m-S\e[0m
		If the user's signature file exists, it'll be appended to the text of
		the message. The path to this user's signature file is:
		\e[4m$HOME/.shmail.$USERNAME.sig\e[0m

	\e[1m-t \e[0;4mrecipient\e[0m
		Address of the recipient of the email (in the To: field). To specify
		more than one recipient, just pass multiple -t arguments.

	\e[1m-z \e[0;4mn\e[0m
		Causes all the attachments to be compressed into one file. You may
		specify a value from 1-9 indicating how much you want the files to be
		compressed (1 is fastest but the least compression, 9 is the slowest
		but compresses files the most). If you pass 0 the files will not be
		compressed at all.

	\e[1;4mFILES\e[0m
		A list of the files you want to send as attachments. If the total size
		of the files exceeds the message size limit, then an error message will
		be displayed and the message will not be sent.

\e[1mRETURN VALUES\e[0m
	 0	  Success
	 1	  Unimplemented feature
	 2	  Unrecognized option or missing required option
	 3	  Invalid data passed to option
	 4	  Error accessing a file
	 5	  Maximum message size exceeded
	 6	  Unexpected error
	 7	  Required utility (e.g. sendmail) not found
	 8	  No suitable text editor found for interactive mode
	 9	  Tried to attach something that isn't a file.

\e[1mKNOWN ISSUES\e[0m
	o Compression does \e[1mnot\e[0m work on file names that contain spaces,
		regardless of whether you escaped them on the command line or not.

\e[1mCONTACT\e[0m
	If you find a bug or would like to request a feature, please send email to
	\e[4mdargueta@codecall.net\e[0m with SHELLMAIL in the subject header. Otherwise I
	might mistake your email for spam. :)
" | less -SR
			exit 0
			;;
		"d")
			DEBUG=1
			;;
		"?")
			echo "Unknown option -$OPTARG."
			exit 2
			;;
		":")
			echo "Option -$OPTARG must have an argument."
			exit 3
			;;
	esac
done

# Make sure that we have at least one recipient.
if [[ $TOINDEX -eq 0 ]]; then
	echo "Error: No recipients specified.
For help, execute $0 -h"
	exit 2
fi

# validate sender email
if [[ $SENDER =~ [A-Za-z0-9\-_]+@[A-Za-z0-9\-_]+\.[A-Za-z]{2,4} ]]; then
	echo "Error: Invalid sender email '$SENDER'
For help, execute $0 -h"
	exit 3
fi

# validate reply-to email
if [[ $REPLYTO != "" ]]; then
	# user specified a reply-to email, check it.
	if [[ $REPLYTO =~ [A-Za-z0-9\-_]+@[A-Za-z0-9\-_]+\.[A-Za-z]{2,4} ]]; then
		echo "Error: Invalid reply email '$REPLYTO'"
		exit 3
	fi
fi

# Validate recipient emails
for thisr in ${TOLIST[*]}
do
	if [[ ! $thisr =~ [A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,4} ]]; then
		echo "Error: Invalid recipient email '$thisr'"
		exit 3
	fi
done

# Validate CC recipient emails
for thisr in ${CCLIST[*]}
do
	if [[ ! $thisr =~ [A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,4} ]]; then
		echo "Error: Invalid CC recipient email '$thisr'"
		exit 3
	fi
done

# Validate BCC recipient emails
for thisr in ${BCCLIST[*]}
do
	if [[ ! $thisr =~ [A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,4} ]]; then
		echo "Error: Invalid BCC recipient email '$thisr'"
		exit 3
	fi
done

# If there's an originating message ID, validate that too
if [[ $ORIGINALMSGID != "" ]]; then
	if [[ ! $ORIGINALMSGID =~ [A-Za-z0-9_.@=-]+ ]]; then
		echo "Error: Invalid original message id '$ORIGINALMSGID'"
		exit 3
	fi
fi

if [[ $INTERACTIVE -ne 0 ]]; then
	# get the editor we're going to use
	if [[ $EDITOR = "" ]]; then
		# EDITOR == "" so the user didn't specify their own editor.

		EDITOR=$(which gedit)
		if [[ ! -x $EDITOR ]]; then
			# GEDIT not found
			EDITOR=$(which vim)
			if [[ ! -x $EDITOR ]]; then
				# VIM not found
				EDITOR=$(which nano)
				if [[ ! -x $EDITOR ]]; then
					# no editor, explode
					echo -e "\e[1mFATAL ERROR:\e[0m No text editor found."
					exit 8
				fi  # no editor found
			fi  # vim found
		fi  # gedit found
	fi  # EDITOR != "", so user specified a custom editor.

	# Call editor for message
	BODYFILE=$(randomFile "tmp")			# generate random message file name
	echo "Executing '$EDITOR $BODYFILE'"	# note for user
	touch $BODYFILE						 # create message file
	eval "$EDITOR $BODYFILE;"			   # launch
	wait ${!}							   # wait for editor to finish

	if [[ $(getFileSize $BODYFILE) -eq 0 ]]; then
		# user didn't provide anything in the file
		echo "No body, setting default message."
		MESSAGESTRING="Automated command-line email from $SENDER."
		rm $BODYFILE
		USEBODYFILE=0
		BODYFILE=""
		MESSAGETYPE="text/plain; charset=\"us-ascii\""
	else
		MESSAGESTRING=""
		USEBODYFILE=2
		MESSAGETYPE=$(getMIMEType $BODYFILE)
	fi
fi

# If the user hasn't provided a message for the recipient, provide a default.
if [[ $MESSAGESTRING = "" ]]; then
	MESSAGESTRING="Automated command-line email from $SENDER."
fi

# Get the size for the message body. This'll either be our default message
# or one that the user provided; we put this code here so that regardless
# of where we got the message from, we get its size.
if [[ $USEBODYFILE -eq 0 ]]; then
	# Get the length of the message string if we're using a string
	MESSAGESIZE=${#MESSAGESTRING}
else
	# Get the file size if we're using a message file
	MESSAGESIZE=$(getFileSize $BODYFILE)
fi

# Shift the argument list over so we're now looking at the files the user
# wants to attach.
shift $((OPTIND-1))

# Get the number of files we need to attach
NUMFILES=$#
# Generate a random message ID
MESSAGEID="${$}${RANDOM}-${USER}@${SERVER}"
BOUNDARY="${$}-${RANDOM}${RANDOM}${RANDOM}-${USER}-${NUMFILES}"
# Create a random temporary file for the email.
MAILFILE=$(randomFile "tmp")

if [[ $NUMFILES -gt 0 ]]; then
	echo "Loading $NUMFILES attachment(s)..."
fi

# Put the initial email headers:
# TO header field
echo -n "To: " > $MAILFILE

# add in all the TO recipients
for thisr in ${TOLIST[@]}
do
	echo -n "<$thisr>;" >> $MAILFILE
done
echo >> $MAILFILE

# add CC recipients if any
if [[ ${#CCLIST[@]} -gt 0 ]]; then
	echo -n "Cc: " >> $MAILFILE
	for thisr in ${CCLIST[@]}
	do
		echo -n "<$thisr>;" >> $MAILFILE
	done
	echo >> $MAILFILE
fi

# add BCC recipients if any
if [[ ${#BCCLIST[@]} -gt 0 ]]; then
	echo -n "Bcc: " >> $MAILFILE
	for thisr in ${BCCLIST[@]}
	do
		echo -n "<$thisr>;" >> $MAILFILE
	done
	echo >> $MAILFILE
fi

# add the Reply-To tag if we have a reply-to email specified
if [[ $REPLYTO != "" ]]; then
	echo "Reply-To: $REPLYTO" >> $MAILFILE
fi

# add the reference to the original email if there was one
if [[ $ORIGINALMSGID != "" ]]; then
	echo "In-Reply-To: $ORIGINALMSGID" >> $MAILFILE
fi

# Add the email headers and the starting header for our message.
echo -e "From: $SENDER
Subject: $SUBJECT
Message-ID: <$MESSAGEID>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"

--$BOUNDARY
Content-Type: $MESSAGETYPE
Content-Disposition: inline
Content-Length: $MESSAGESIZE
" >> $MAILFILE

# Now it's time to insert the body of our email. It's either going to come
# from a string or a file, so we need to check that.
if [[ $USEBODYFILE -eq 0 ]]; then
	# message comes from a STRING (or wasn't specified at all).
	echo "$MESSAGESTRING" >> $MAILFILE
else
	# user wants to use a message FILE
	cat $BODYFILE >> $MAILFILE
fi

# WARNING: THIS WILL NOT WORK WITH HTML EMAILS. MUST FIX THIS
if [[ ( SIGFILE != "" ) && ( -e $SIGFILE ) ]]; then
	if [[ $MESSAGETYPE != "text/plain" ]]; then
		echo "Cannot add signature to an email that's not plain text."
	else
		cat $SIGFILE >> $MAILFILE
	fi
fi

echo -ne "

Sent by $USERNAME from $SERVER with ShellMail $VERSION
Email alias: $SENDER
" >> $MAILFILE

################################################################################
# The message portion of the email is complete. Now we start working on the
# attachments, if any. The user can either send them all individually or choose
# to put them all in a .tar.gz archive.
#
# See if the user requested to zip the attachments. If they did, then $ZIP will
# be greater than 0. If it's 0 or less, then the user doesn't want to zip the
# attachment files.
if [[ $ZIP -le 0 ]]; then
	############################################################################
	# ATTACH INDIVIDUAL FILES
	#
	i=0
	while [ $# -ge 1 ];
	do
		# Attach this file, and capture the new size of the file.
		echo -n "Attaching '$1'..."

		if [[ ! -f $1 ]]; then
			echo "Error: File not found."
			break
		fi

		attachFile $MAILFILE $BOUNDARY $1 &> /dev/null
		echo "done."
		shift

		# if we don't have any more files in the queue, then add the
		# end-of-message token to the email and exit the loop.
		if [[ $# -eq 0 ]]; then
			echo -e "\n--$BOUNDARY--" >> $MAILFILE
			break
		fi
	done
	############################################################################
else
	############################################################################
	# ZIP ALL ATTACHMENT FILES TOGETHER
	#
	# Create a random file to put the .TAR.GZ archive into
	GZIPFILE=$(randomFile "tar.gz")

	# Zip the files together. NOTE: This will NOT work if any files have spaces
	# in their names.
	tar -hcf - $@ | gzip -$ZIP -c > $GZIPFILE

	# Attach the zipped archive to our email
	attachFile $MAILFILE $MESSAGEID $GZIPFILE &> /dev/null

	# Stick the end-of-message token on the end of the email
	echo -e "\n--$BOUNDARY--" >> $MAILFILE

	# Delete the GZIP file only if we're not debugging. Otherwise print it out.
	if [[ $DEBUG -eq 0 ]]; then
		rm -f $GZIPFILE
	else
		echo "Debugging file: $GZIPFILE"
	fi
	############################################################################
fi

################################################################################
# ALL FILES ALREADY ATTACHED
#
# Now we're doing a little math to print out the size of the message in human-
# readable form. Messages less than a kilobyte are printed out in bytes, those
# greater than 1K but less than 1MB are printed in kilobytes, etc.
#
MESSAGESIZE=$(getFileSize $MAILFILE)
SCALEDSIZE=$MESSAGESIZE
SIZEUNIT="B"
if [[ $SCALEDSIZE -gt 1024 ]]; then
	SCALEDSIZE=$(($SCALEDSIZE / 1024))
	SIZEUNIT="KB"

	if [[ $SCALEDSIZE -gt 1024 ]]; then
		SCALEDSIZE=$(($SCALEDSIZE / 1024))
		SIZEUNIT="MB"

		if [[ $SCALEDSIZE -gt 1024 ]]; then
			# how the ** do we get here?
			SCALEDSIZE=$(($SCALEDSIZE / 1024));
			SIZEUNIT="GB"
		fi
	fi
fi

# Check to make sure that the message size doesn't exceed the maximum size
# allowed for an email. This is typically 20MB.
if [[ $(($MESSAGESIZE / 1048576)) -gt $MAXIMUMSIZE ]]; then

	# Oops...we exceeded the message size limit.
	echo -e "\n\e[1mWARNING:\e[0m Message size is $SCALEDSIZE $SIZEUNIT."

	# However, we also let the user specify the maximum message size, in case
	# their system only allows 10MB messages, or is more lenient and allows
	# 32MB messages, and so on. Here we check to see if the user specified
	# a new maximum size.
	if [[ $MAXIMUMSIZESPECIFIED -ne 0 ]]; then
		# The user specified a maximum size and we exceeded it. Crash 'n' burn.
		echo -e "
The maximum size for your system is $MAXIMUMSIZE MB. Please remove some files
and try sending it again.\n"
		exit 5
	else
		# The user didn't specify a maximum size, but we're assuming that it's
		# 50MB. We're still going to try to send the message, but we're just
		# warning them that it might not go through.
		echo -e "
Most systems do not allow messages larger than 50MB to be sent. As this may not
be the case with your system, your email will be sent anyway, but be advised
that the system may not deliver it.\n"
	fi
else
	# Everything's good, we haven't exceeded the maximum message size.
	echo -e "\nFinal message size: $SCALEDSIZE $SIZEUNIT ($MESSAGESIZE bytes)."
fi

# If we get here, then SENDMAIL is installed. We can now send our email.
echo -n "Sending email..."
cat $MAILFILE | sendmail -it

# Make sure everything went off without a hitch; check the return value.
MAILERROR=$?
if [[ $MAILERROR -ne 0 ]]; then
	# Return value is nonzero...oops. Something went wrong.
	echo "
Unexpected error $MAILERROR encountered when sending the email.
Please see '$MAILFILE' for the contents of the email,
or just delete it.
"
	# We don't delete the message file in this case, regardless of whether
	# we're debugging or not. The user should check the message file to
	# make sure it's well-formed, or just delete it.
	exit 6
fi
echo "done."

# Delete the message file (if not debugging)
if [[ $DEBUG -eq 0 ]]; then
	rm $MAILFILE
	if [[ $USEBODYFILE -eq 2 ]]; then
		rm $BODYFILE
	fi
else
	# We are debugging, print the message file name and path
	echo "Message file: $MAILFILE"
fi

# Exit w/ success code.
exit 0

Edited by dargueta, 13 April 2012 - 10:15 PM.
New version 2.33

sudo rm -rf /

#2
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
nice work dargueta!:thumbup:

Edited by dargueta, 07 May 2011 - 09:14 AM.
Typo

Be a joke unto yourself!
Check out my blog at Flashcore




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users