An even better version...
#!/usr/bin/bash
# filename: send2owa
# send2owa -- Send Message From Linux To Exchange Server Via Outlook
Web Access
# Public Domain
# The anonymous author designed this so that Linux-based business
system batch job status reports could be sent to Outlook clients in a
corporate environment where SMTP was banned and Exchange Server was the
only thing enabled.
# /note/ This is programmed for Exchange 5.5 -- may need mods for other
revs of OWA. If you need to modify, consider using Squid with query
params turned on and point your browser to this proxy. That way, you
can see the web server interaction of GETs and POSTs and can tweak this
script per your needs.
# Note Microsoft calls itself interoperable but this API below is not
explained
# and they want you to go through the even more convoluted WebDAV
technique. Note how interoperable Linux is!
# Note that you can't use CDO API in VB anymore without all these
security alerts popping up, leaving us with no choice but to go this
route!
# Possible improvement suggestion -- build a fake SMTP service in your
favorite Linux language that then uses send2owa to send the message to
the Exchange server. This permits you to use ordinary SMTP tools with
your corporate development and operations teams in order to send mail
out.
if [ "$1" == "-i" ]; then
INTERACTIVE="TRUE"
fi
if [ "$1" == "-h" ]; then
NEEDSHELP="TRUE"
fi
if [ "$1" == "--help" ]; then
NEEDSHELP="TRUE"
fi
if [ "$1" == "-?" ]; then
NEEDSHELP="TRUE"
fi
if [ "$1" == "/?" ]; then
NEEDSHELP="TRUE"
fi
if [ "$1" == "" ]; then
NEEDSHELP="TRUE"
fi
if [ $NEEDSHELP ]; then
echo -e "send2owa $VERSION: Send Message From Linux To Outlook\n"
echo -e "Usage: send2owa [-i | {strict options order}]\n"
echo -e "\t-i =\t\t\t Interactive. Prompts you for everything."
echo -e "\t{strict options order} = Non-interactive. Requires options
in following order."
echo -e "\n\tNote: \"Quoting\" your options is REQUIRED.\n"
echo -e "\t\t<Your Outlook Webmail Host Name>"
echo -e "\t\t<Your Domain>"
echo -e "\t\t<Your Username>"
echo -e "\t\t<Your Password>"
echo -e "\t\t<Your First Name>"
echo -e "\t\t<Your Last Name>"
echo -e "\t\t<Your Recipient Email Address List (Semicolon Delimited)>"
echo -e "\t\t<Your CC Email Address List (Ditto)>"
echo -e "\t\t<Your BCC Email Address List (Ditto)>"
echo -e "\t\t<Email Subject>"
echo -e "\t\t<Email Message>\n"
echo -e "Ex:\n"
echo -e "./send2owa 'webmail' 'rook' 'jsmith' 'Pass1' 'Jack' 'Smith'
'd*****@gadgets.bv' '' '' 'Hello, World!' 'This is a test.'\n"
exit
fi
rm -f inbox_id.html
rm -f msg_id.html
rm -f sent_result.html
rm -f cookies.txt
if [ $INTERACTIVE ]; then
echo -e "\nsend2owa $VERSION: Send Message From Linux To Outlook\n\n"
read -p "Your Outlook Webmail Host Name: " OWAHOST
read -p "Your Domain: " DOMAIN
read -p "Your Username: " USER
echo -en "Your Password: "
read -rs PASS
echo ""
read -p "Your First Name: " FIRSTNAME
read -p "Your Last Name: " LASTNAME
read -p "Recipient Email Address List (Semicolon Delimited): " EMAIL_TO
read -p "[OPTIONAL] CC Email Address List (Semicolon Delimited): "
EMAIL_CC
read -p "[OPTIONAL] BCC Email Address List (Semicolon Delimited): "
EMAIL_BCC
read -r -p "Email Subject: " EMAIL_SUBJECT
read -r -p "Email Message: " EMAIL_MSG
else
OWAHOST=$1
DOMAIN=$2
USER=$3
PASS=$4
FIRSTNAME=$5
LASTNAME=$6
EMAIL_TO=$7
EMAIL_CC=$8
EMAIL_BCC=$9
EMAIL_SUBJECT=${10}
EMAIL_MSG=${11}
fi
echo -e "\nLogging in to Outlook Webmail Server \"$OWAHOST\" ..."
# LOGIN TO MAIL SERVER. NOTE FIRST TIME YOU DO THIS WE GRAB AN ERROR
AND A VALID ASP SESSION COOKIE. THIS WAS THE ONLY WAY I COULD GET IN.
# NEXT, IT USES THIS SESSION COOKIE ON ALL SUBSEQUENT PAGES. I HAD TO
RETRIEVE logonfrm.asp TWICE BECAUSE OF I THINK MICROSOFT HAS TO TAKE
# TIME ON IIS TO BUILD A SESSION OBJECT FOR US?? OH WELL -- WHATEVER
WORKS
curl -y 15 -c cookies.txt -u $DOMAIN\\$USER:$PASS
"http://$OWAHOST/exchange/LogonFrm.asp?isnewwindow=0&mailbox=$LASTNAME%20$FI RSTNAME"
/dev/nul 2>&1
curl -y 15 -b cookies.txt -u $DOMAIN\\$USER:$PASS
"http://$OWAHOST/exchange/LogonFrm.asp?isnewwindow=0&mailbox=$LASTNAME%20$FI RSTNAME" /dev/nul 2>&1
echo -e "Sending Message \"$EMAIL_SUBJECT\" ..."
# CHEAP URLENCODING WITHOUT DOWNLOADING EXTRA TOOLS!
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/ /%20/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/?/%3F/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/\`/%60/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/~/%7E/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/!/%21/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/@/%40/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/#/%23/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/&/%26/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/*/%2A/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/(/%28/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/)/%29/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/+/%2B/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/=/%3D/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/{/%7B/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/}/%7D/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/|/%7C/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/\[/%5B/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/]/%5D/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/\\\\/%5C/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/:/%3A/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/;/%3B/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/"/%22/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed "s/'/%27/g"`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/</%3C/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/>/%3E/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/\//%2F/g'`
EMAIL_SUBJECT=`echo -n $EMAIL_SUBJECT | sed 's/,/%2C/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/ /%20/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/?/%3F/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/\`/%60/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/~/%7E/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/!/%21/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/@/%40/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/#/%23/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/&/%26/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/*/%2A/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/(/%28/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/)/%29/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/+/%2B/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/=/%3D/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/{/%7B/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/}/%7D/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/|/%7C/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/\[/%5B/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/]/%5D/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/\\\\/%5C/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/:/%3A/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/;/%3B/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/"/%22/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed "s/'/%27/g"`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/</%3C/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/>/%3E/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/\//%2F/g'`
EMAIL_MSG=`echo -n $EMAIL_MSG | sed 's/,/%2C/g'`
# NOW WE HIT main_fr.asp TO GET A LONG INBOX ID FOR USE IN SUBSEQUENT
PAGES
curl -y 15 -o inbox_id.html -b cookies.txt -u $DOMAIN\\$USER:$PASS
"http://$OWAHOST/exchange/inbox/main_fr.asp?view=1&store=0&obj=&acs=" >
/dev/nul 2>&1
INBOXID=$(cat inbox_id.html | grep -i "var szCurFolderID" | cut -f 4 -d
" " | cut -f 2 -d "\"" | tr -d "\n")
# NOW WE HIT frmroot.asp SO THAT IT ALLOCATES A FRESH MESSAGE OBJECT
FOR US FOR THIS INBOX AND GIVES US A
# FRESH MESSAGE OBJECT ID.
curl -y 15 -o msg_id.html -b cookies.txt -u $DOMAIN\\$USER:$PASS
"http://$OWAHOST/exchange/forms/ipm/note/frmroot.asp?command=new&store=0" /dev/nul 2>&1
MSGID=$(cat msg_id.html | grep -i "+\"&obj=" | cut -f 2 -d "=" | cut -f
1 -d "\"" | tr -d "\n")
# NOW WE POST OUR EMAIL FORM DATA TO
/exchange/forms/ipm/note/commands.asp TO SEND THE MESSAGE OUT
curl -y 15 -o sent_result.html -b cookies.txt -u $DOMAIN\\$USER:$PASS
-d
"to=$EMAIL_TO&cc=$EMAIL_CC&bcc=$EMAIL_BCC&subject= $EMAIL_SUBJECT&message=$EMAIL_MSG&tab=message&comm and=send&objID=$MSGID&savecopy=1&importance=1"
"http://$OWAHOST/exchange/forms/ipm/note/commands.asp" > /dev/nul 2>&1
# NOW WE PLAY NICE AND FREE UP SERVER RESOURCES BY LOGGING OFF
curl -y 15 -b cookies.txt -u "$DOMAIN\\$USER:$PASS"
"http://$OWAHOST/exchange/logoff.asp" > /dev/nul 2>&1
if [ `cat sent_result.html | grep -i "command=holding"` ]; then
echo -e "\n\tMail SENT\n\n"
else
echo -e "\n\tMail NOT sent!!! Try debugging send2owa script.\n\n"
fi
# FOR DEBUG -- COMMENT THE PAGES OUT BELOW SO THAT YOU CAN READ THE
CONTENT TO DETERMINE PROBLEMS
rm -f inbox_id.html
rm -f msg_id.html
rm -f sent_result.html
rm -f cookies.txt