Wednesday, November 25, 2015

Email sent from Raspberry PI

UPDATE:
Added details and Links

Sending Email from a Raspberry PI (or any typical Linux workstation) is NOT normally configured. But with the help of some optional commands it can be done with ease.

Also, see my previous post.


Some History

In the early days of Unix Workstations and Server, "sendmail" was typically configured by default, and therefore sending email was easy. In fact, it was "too easy", which allowed the bad guys to anomalously send SPAM. To combat SPAM most ISP's started requiring mail be "signed" (user name and password) by the originator, so it could be traced back to the point of origin.

Today

Often it is useful to send a simple status report, information, attachments, or a quick SMS messages from; a Linux Workstation, a small Raspberry PI, or a Beagle Bone.

Today "most" email must be signed, and because of that it has become more difficult to use email for sending mail even to oneself.

Note: There are a "few" Open Mail (SMTP) Gateways that can be used. But the gateways can not, and will not guarantee any kind of performance, or can not provide any reasonable expectations. These Open SMTP's  are often used by SPAMMER.

Google'ing "Raspberry PI email" will provide several solutions.

But, most (if not all), require you use/save your EmailAddress and EmailPasswd as clear open-text on the command line or within a configuration file. I do not like that idea. There has got to be a better way?

One of the suggested solutions is "sendEmail", unfortunately it has a (some what) "complex" command line interface, and as above it normally requires the user to type an email password as clear open-text on the command line.

Note the upper case "E" in the sendEmail name.

To simplify, I have created a "Wapper Script", that can be used similar to those used in the early days of UNIX.

Examples:

      $ date | mysendgmail some_user@gmail.com

or,

      $ mysendgmail some_user@gmail.com < textfile

or, SMS

      $ echo "Meet me for Lunch" | mysendgmail phonenumber@vtext.com

or, with an attachement

      $ echo "Here is my report" | mysendgmail some_user@yahoo.com -a myfile

or

      $ mysendgmail some_user@aol.com -s "My Status" -m I am driving, Do you want a ride.

Note: in the above example, @vtext.com is the Verizon SMS gateway, others could be used as necessary.

For the above examples, using my "Wapper Script", the users Gmail Password is prompted for, but does not display, see help text.

Also, see the included Help text for install instructions.

This Script makes my life easy, I hope you also find it useful. It is a little long for a blog post, but I wanted to provide it here before going to the trouble of setting up its own GitHub.

Note: some of the options of "sendEmail" have been used/perverted by this script to make the user interface easy and simple, which I find is a good compromise for functionality and usability.

Note: there is a BUG in the Raspberry PI implementation of "sendEmail",  Again, see the Help text.

If all else fails, "sendEmail" is a useful tool even when used by itself.

This following is mysendgmail script:



#! /bin/bash
# mysendgmail - Command line mail interface for Linux users,
# often on Raspberry Pi and other similar
# This script is a user friendly wrapper for "sendEmail"

# Because this script requires customization for each user,
# it should be save in each users $HONE/bin directory,
# as $HOME/bin/mysendgmail (or what ever name you like).


AUTHOR='Eldon R. Brown  - WA0UWH - 2015/11/22'
REV='1.02.30'

# Notes:
#   I think Google gmail allows 99 SMTP emails to be sent per day.
#   Note: an SMS sent to a Cell Phone has a 160 Character Limit.

progname=`basename $0`

# The Gmail Account, these must be customized per user
 from_user='userName@gmail.com'
 gmail_user='userName'

#This is for Google Gmail, others could be used
 SMTP='smtp.gmail.com:587'


# Setup for Bold, Highlight, and Normal output
 B=`tput bold` G=`tput setf 2` N=`tput sgr0` H=${B}${G}

Usage="

${B}${progname}-${REV}, by: ${AUTHOR}${N}

  ${H}Usage:${N}

        ${progname} [mode] [-t] user@domain.com [[-s|-u] subject] [sendEmail args] -m message . .
  . . | ${progname} [mode] [-t] user@domain.com [[-s|-u] subject] [sendEmail args]

        Where: [mode] =
               -n     = Dry Run
               -v     = Verbose
               -h     = This help message
              --h     = This help message, plus more Info
              --help  = sendEmail help message, also see the
                        man page, as: man sendEmail

"

Info="
  ${H}More Info:${N}

        To Install supporting functions:
        $ sudo aptitude install sendEmail perl
        $ sudo aptitude install libio-socket-ssl-perl libnet-ssleay-perl

        See some sendEmail Examples, at: http://goo.gl/6LqX8Q

        NOTE: There is a BUG in Raspberry PI's implementation
              of: /usr/bin/sendEmail REV=1.56
              Workaround:
                 replace 'SSLv3 TLSv1' with 'SSLv3' within /usr/bin/sendEmail

 NOTE: The Gmail Passwd can be saved (and somewhat hidden)
              in the current environment with:
              $ export GPW; read -s -p 'Gmail Passwd: ' GPW; echo
       And then later, it can be removed with:
              $ unset GPW

" 
 
# Main

 # Check first arg for Help Message, or Version Info
 case "${1}" in
 (-V)    echo -e "${progname}-${REV} by ${AUTHOR}"; exit;;
 (-h|"") echo -e "${Usage}" >&2; exit;;
 (--h)   echo -e "${Usage} ${Info}" >&2; exit;;
 (-help|--help) sendEmail --help >&2; exit;;
 esac
 
 # Check first arg for Dry Run or Verbose
 case "${1}" in
 (-n) DRYRUN='yes'; shift;;
 (-v) VERBOSE='yes'; shift;;
 esac

 # Check next arg for Dry Run or Verbose, again to avoid order conflict
 case "${1}" in
 (-n) DRYRUN='yes'; shift;;
 (-v) VERBOSE='yes'; shift;;
 esac

 # Prompt for Gmail Account Passwd, OR used the Environment Varrible $GPW
 gmail_passwd="${GPW}"
 if [ -z "${GPW}" ]
 then read -s -p "Gmail Password: " gmail_passwd </dev/tty; echo
 fi


# Parse Mail Address and Subject Args
 
 # Check if First arg is "-t", if so ignore, ignore it and
 # assume the next is email address
 case "${1}" in (-t) shift;; esac

 # Assume Next arg is the TO address
 to="${1}"; shift
 
 # Check if Next arg is "-s" or "-u", if so, ignore it and
 # assume the next is the Subject
 case "${1}" in (-u|-s) shift;; esac

 Default_Subject="Sent: `date '+%Y/%m/%d %H:%M:%S'`"
 # Assume Next arg is Subject
 sub="${1:-${Default_Subject}}"; shift

# Setup DEBUG modes

 # Check for DRYRUN mode, if so, Munge passwd for DRYRUN
 if [ "${DRYRUN}" = "yes" ]; then gmail_passwd='"*****"'; fi

 # Check for VERBOSE, For Debug
 if [ "${VERBOSE}" = "yes" ]; then MODE='set -xv'; fi

# Build the Command
 CMD="sendEmail -o tls=yes -s ${SMTP} \
  -xu ${gmail_user} -xp ${gmail_passwd} \
  -f ${from_user} -t ${to} -u ${sub} \
  $@" # Adds user supplied sendEmail options

# Finally,  Run Command
 case "${DRYRUN}" in
 (yes) echo $CMD;;  # For Debug
 (*) ${MODE}; $CMD | grep "`date '+%b %d'`";; # Report just the "basic" status
 esac

# End



-- Home Page: https://WA0UWH.blogspot.com

No comments:

Post a Comment