Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#!

/bin/sh
#
#
# This script will send an email using a Gmail account through an openssl sessio
n.
#
# Version 1.0
# December 23, 2015
#
#
# Note: you must turn ON "Access for less secure apps" in the Gmail settings:
# https://www.google.com/settings/security/lesssecureapps
# I therefore recommend you create a separate Gmail account just for your router
.
#
#
# Usage: ./sendgmail.sh login password recipient subject message
#
#
# (and some patience... it takes less than 10 seconds)
#
# login
: the Gmail login you created for the router
#
# password : the Gmail password (best to enclose in quotes "" in case there are
any spaces)
#
# recipient : email address of recipient
#
# subject : subject of email (best to enclose in quotation marks "" since any
spaces will
#
be misinterpreted as the start for the next parameter)
#
# message : body of email (enclose in quotation marks "" and do not start any
lines with a dot!).
#
You can pipe a file like this: ./sendgmail.sh ... "$(cat yourfileh
ere)" (you
#
have to use quotes otherwise any space is treated as the next para
meter).
#
You can also include escape characters (like new line \n) by using
$'some\nmessage'
#
as the message parameter (note the dollar sign and single quotes).
#
#
# Examples of usage :
#
# Send a simple message from command line:
# ./sendgmail.sh randomemail@gmail.com secretpassword recipient@gmail.com "Cool
subject" "Nice body (of email)"
#
# Send a message with some new line feeds \n in body:
# ./sendgmail.sh randomemail@gmail.com secretpassword recipient@gmail.com "Cool
subject" $'Nice body\n(of email)'
#
# Send a message from command line but read email body from a text file called "
body.txt":
# ./sendgmail.sh randomemail@gmail.com secretpassword recipient@gmail.com "Cool
subject" "$(cat body.txt)"
#
# And for this one, see notes below for explanation:
# read login passwd <credentials.txt ; ./sendgmail.sh $login $passwd recipient@g

mail.com "some subject" "$(cat body.txt)"


#
#
# Note: as you can see from the syntax, it's not a very secure way to use this c
ommand as it requires
# you to put your login and password in plain text. However, you can create a c
redentials file (such
# as "credentials.txt"), put your plain text login and password separated by a s
pace on the first
# line, and give it read/write permission for root only (chmod 600 credentials.t
xt). Then use this
# syntax to call the script:
# read login passwd <credentials.txt ; ./sendgmail.sh $login $passwd ...
#
#
# Warning: this is a very basic script that does not check for errors. If somet
hing goes wrong,
# Try commenting out the "-quiet > /dev/null 2>&1" at the end of the openssl com
mand to maybe
# see what's going on. This shouldn't really matter as it would most likely be
used as part of
# a cron job.
#
#
# Warning: if you add this as part of a cron job script, watch out for the paths
! Relative paths
# that work in a user shell may not work when cron is running. Use absolute pat
hs
# (e.g. "/opt/sendgmail.sh ...") instead.
# Show that something is happening (-n doesn't send a line feed)
echo -n 'Sending email through Gmail... '
# Parenthesis to start a "subshell" that will pass commands to openssl through a
pipe
(
# This is the Gmail login for your router's special Gmail address
AUTH=$1
# The FROM line is only "for show" in the email header (the email will come from
the Gmail account
# regardless of what you put for the FROM line)
FROM=$AUTH
# This is the Gmail password
PASS=$2
# The email address to which you are sending the email
RECIPIENT=$3
# Subject and body of email
SUBJECT=$4
BODY=$5
# We need to generate base64 login and password for openssl session
AUTH64="$(echo -n $AUTH | openssl enc -base64)"
PASS64="$(echo -n $PASS | openssl enc -base64)"
# Time to start talking to Gmail smtp server

echo 'auth login' ; sleep 1 ; \


echo $AUTH64 ; sleep 1 ; \
echo $PASS64 ; sleep 1 ; \
echo 'mail from: <'$FROM'>' ; sleep 1 ; \
echo 'rcpt to: <'$RECIPIENT'>' ; sleep 1 ; \
echo 'data' ; sleep 1 ; \
echo 'Subject: '$SUBJECT ; sleep 1 ; \
echo ''; sleep 1; \
echo "$BODY"; \
echo '.' ; sleep 1 ; \
echo 'QUIT') 2>&1 | \
openssl s_client -connect smtp.gmail.com:587 -starttls smtp -crlf -ign_eof -quie
t > /dev/null 2>&1
# Be positive and cross your fingers that everything went right!
echo 'Done!'

You might also like