Thursday, March 18, 2010

Bash Script to send email using smtp authentiation and bash file descriptors

Sometimes we require bash script to perform some operation and use a remote mail server to send mails. In this instance we require smtp authentication for the remote server to allow this operation.

The script was copied from the link http://linuxshellaccount.blogspot.com/2008/04/shell-script-to-send-mail-using-bash.html which describes networking with the help of file descriptors.

I just added a smtp authentication part.

Please ensure that others do not have access to your bash history for security reasons.

Usage

sh mail.sh report@yourserver.com youremail@gmail.com yourserver.com yourmailserver.com filecontainingmessage email@yourmailserver.com youremailpassword





SCRIPT CONTENTS:


mail.sh




#!/bin/bash

#
# mail.sh
#
# 2008 - Mike Golvach - eggi@comcast.net
# 2010 - Rayber
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ $# -ne 7 ]
then
echo "Usage: $0 FromAdress ToAdress Domain MailServer MailTextFile AuthEmail AuthPass"
exit 1
fi

from=$1
to=$2
domain=$3
mailserver=$4
mailtext=$5
authemail=`echo $6|openssl enc -base64|awk 'sub("..$", "")'`
authpass=`echo $7|openssl enc -base64|awk 'sub("..$", "")'`

if [ ! -f $mailtext ]
then
echo "Cannot find your mail text file. Exiting..."
exit 1
fi

exec 9<>/dev/tcp/$mailserver/25
echo "HELO $domain" >&9
read -r temp <&9
echo "$temp"
echo "auth login" >&9
read -r temp <&9
echo "$authemail" >&9
read -r temp <&9
echo "$authpass" >&9
read -r temp <&9
echo "Mail From: $from" >&9
read -r temp <&9
echo "$temp"
echo "Rcpt To: $to" >&9
read -r temp <&9
echo "$temp"
echo "Data" >&9
read -r temp <&9
echo "$temp"
cat $mailtext >&9
echo "." >&9
read -r temp <&9
echo "$temp"
echo "quit" >&9
read -r temp <&9
echo "$temp"
9>&-
9<&-
echo "All Done Sending Email. See above for errors"
exit 0



When you run the script you should get a output similar to the following output.

235 2.7.0 Authentication successful
250 2.1.0 Ok
250 2.1.5 Ok
354 End data with .
250 2.0.0 Ok

5 comments:

  1. If you get an error like AUTH command used when not advertised

    replace HELO with EHLO

    Works great for me :)

    ReplyDelete
  2. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. hotmail correo electrónico

    ReplyDelete
  3. I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. clique aqui

    ReplyDelete
  4. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future...
    find business email

    ReplyDelete
  5. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks learn more about Hotmail

    ReplyDelete