Skip to content

Bash script for sending SMS using Mollie

I signed up for a Mollie account so that I can send SMS’s from my machines. To do that, I needed a bash script, so I wrote one:

#! /bin/bash
#
# Script to send an SMS email notifcation to Mollie's HTTP gateways
# Based on the SMS2Email script which can also be found on nagiosexchange.org
# Reworked by Dennis Storm - Brainstorm ICT
# Again reworked by Wiebe Cazemier (wiebe@halfgaar.net), to include proper failure checks
#
#################################################################################
 
config_file="/etc/send-sms.conf"
log_file="/var/log/send-sms.log"
 
if [ -L "$log_file" ]; then
  echo "cannot continue, $log_file is a symlink."
  exit 2
fi
 
touch "$log_file" > /dev/null 2>&1
 
sender="SMSScript"
 
if [ -f "$config_file" ]; then
  source $config_file
fi
 
curl_location=`which curl 2> /dev/null`
 
if [ -z "$curl_location" ]; then
  echo "Curl command not found. Install that." >&2
  exit 1
fi
 
logfail()
{
  message="$1"
  message="Failure: [`date`]: $message" 
 
  echo $message
 
  if [ -w "$log_file" ]; then
    echo "$message" >> "$log_file"
  fi
 
  exit 1
}
 
# Show usage if necessary
if [ $# -eq 0 ]; then
    echo "Usage: $0 -s [sender] -n [numbers] -m [message] -u [username] -p [password]";
    echo "";
    echo "[numbers]  = SMS numbers (if multiple, enclose in quotes) to send message to.";
    echo "[message]  = Text of message you want to send";
    echo "[username] = Username assocated with Mollie  account";
    echo "[sender]   = Sender"
    echo "[password] = MD5 HTTP API Password assocated with Mollie account";
    echo ""
    echo "-d         = Dry run, pretend success."
    echo ""
    echo "The numbers, sender, username and password options are optional and";
    echo "override the account credentials defined in $config_file.";
    echo ""
    echo "A log file $log_file will be kept if it is writable to the user."
    echo "";
    exit 1;
fi
 
# Get command line arguments
while [ "$1" != "" ] ; do
    case $1
    in
  -n)
      # Get the SMS numbers that we should send message to
      numbers="$2";
      shift 2;
      ;;
  -m)
      # Get the message we should send
      message="$2";
      shift 2;
      ;;
  -s)
      # Get the sender to show in the SMS
      sender="$2";
      shift 2;
      ;;
  -u)
      # Get the username
      username="$2";
      shift 2;
      ;;
  -p)
      # Get the password
      password="$2";
      shift 2;
      ;;
  -d)
      dry_run="dry_run";
      shift 1;
      ;;
  *)
      echo "Unknown option: $1"
      exit 1;
      ;;
    esac
done
 
if [ -z "$username" ]; then
  logfail "No username specified or found in $config_file."
fi
 
if [ -z "$password" ]; then
  logfail "No password specified or found in $config_file."
fi
 
if [ -z "$numbers" ]; then
  logfail "No numbers specified or found in $config_file."
fi
 
message_length=`echo -n "$message"|wc -c`
sender_length=`echo -n "$sender"|wc -c`
 
if [ "$message_length" -gt "160" ]; then
  logfail "SMS message is longer than 160 chars ($message_length). That is not allowed." >&2
fi
 
if [ "$sender_length" -gt "11" ]; then
  logfail "Sender is longer than 11 chars ($sender_length). That is not allowed." >&2
fi
 
# We haven't sent the message yet
message_sent_ok=0;
 
# The API supports sending to a comma seperated list, but that doesn't seem
# to work well. Therefore, I space seperate them and just call the API for
# each number.
for number in $numbers; do 
  if [ ! "$dry_run" ]; then
    RESPONSE=`curl -s -d username="$username" -d md5_password="$password" -d originator="$sender" -d recipients="$number" -d message="$message" http://www.mollie.nl/xml/sms/`
  else
    RESPONSE="<success>true</success>"
  fi
 
  # Curl was able to post okay...
  if [ "$?" -eq "0" ]; then
    success_line_true=`echo "$RESPONSE"|grep -i "<success>true</success>"`
 
    if [ -z "$success_line_true" ]; then
      logfail "$message to $number. Response was: $RESPONSE"
    fi
  else
    logfail "curl return an error while trying to contact mollie to send an SMS." >&2
  fi
 
  if [ -w "$log_file" ]; then
    echo "Success: [`date`]: $dry_run Sent '$message' to $number" >> $log_file
  fi
done
 
echo "Success sending sms(es)."

It has the following config file in /etc/notify_sms.conf:

# Default values. Can be overriden with command line arguments
username="halfgaar"
password="" # MD5sum of HTTP API password.
sender="Melk"
numbers="+316xxxxxxxx" # You can set multiple numbers by space seperating them. 

    No Comments ( Add comment / trackback )