Skip to content

Instantly share code, notes, and snippets.

@gasparfm
Created December 9, 2014 09:47
Show Gist options
  • Save gasparfm/de52561f431fda5d7513 to your computer and use it in GitHub Desktop.
Save gasparfm/de52561f431fda5d7513 to your computer and use it in GitHub Desktop.
Send e-mails with attachments from console using sendmail
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# email.sh Copyright (C) 2014 Gaspar Fernández <[email protected]>
# This bash script will help you to use sendmail allowing us to send e-mails from
# the terminal in a friendly way just say destination, subject and body in the command
# arguments. It allows us also to send mail attachments with local files.
#
# I find it interesting when using remotely a server with SSH and I must send a file
# located in it to someone. I wrote this little script to send the file directly to
# the destination.
# Changelog:
# - 20141129: Initial idea of the script and wrote basic functionality
# - 20141206: Code clean up
# - 20141208: Allows us to send many attachments
# To do:
# - Doc and examples !!!
#MAIL_BOUNDARY="m41lb0und4ry" # We can randomize it later
MAIL_BOUNDARY=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 40`
SENDMAIL_COMMAND=`which sendmail`
function showUsage()
{
# We must extend this help to use headers ans so
echo $1
echo "Use:"
echo $2 destination subject body [attachment1] [attachment2] ... [attachmentN]
echo
exit
}
#Basic arguments of our program
TO=$1
SUBJECT=$2
BODY=$3
if (( $# < 3 ))
then
showUsage "Invalid arguments" $0
fi
# Fill attachments and additional user headers
ATTACHMENTS=""
MOREHEADERS=""
for (( i=4; $i<=$#; i++))
do
ATTACH="${!i}"
if [ ! -r "$ATTACH" ];
then
# Not an attachment, ok but it can be an additional header it will have : let's check it
OLDIFS=$IFS
IFS=":"
read -a HEADER <<< "$ATTACH"
IFS=$OLDIFS
case ${HEADER[0]} in
"From" | "Reply-to" | "Date" | "X-Mailer" | "Organization" | "CC" | "BCC" )
if [ -z "$MOREHEADERS" ]
then
MOREHEADERS=$ATTACH
else
MOREHEADERS="`echo -e "$MOREHEADERS""\n""$ATTACH"`"
fi
;;
*)
showUsage "Attachment not found" $0
esac
else
ATTACH_MIME=`file --mime-type -b "$ATTACH"`
ATTACH_NAME=`basename "$ATTACH"`
ATTACH_BASE64=`base64 "$ATTACH"`
ATTACHMENTS="$ATTACHMENTS
--$MAIL_BOUNDARY
Content-Type: $ATTACH_MIME; name=\"$ATTACH_NAME\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$ATTACH_NAME\"
$ATTACH_BASE64"
fi
done
# Send message
( echo "To: $TO";
echo "Subject: $SUBJECT";
echo -e "$MOREHEADERS"
echo "MIME-Version: 1.0";
echo "Content-Type: multipart/mixed; boundary=\"$MAIL_BOUNDARY\"";
echo "--"$MAIL_BOUNDARY;
echo "Content-Type: text/plain";
echo -e "Content-Disposition: inline\n";
echo "$BODY";
echo -e "$ATTACHMENTS"
echo "--"$MAIL_BOUNDARY"--") | $SENDMAIL_COMMAND $TO
RESULT=$?
if (( $RESULT==0 ))
then
echo "Message sent successfully";
else
echo "Could not send message"
fi
@gasparfm
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment