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"
  [ -L "$log_file" ];
  "cannot continue, $log_file is a symlink."
  2
 
touch "$log_file" > /dev/null 2>&1
 
sender="SMSScript"
  [ -f "$config_file" ];
  $config_file
 
curl_location=`which curl 2> /dev/null`
  [ -z "$curl_location" ];
  "Curl command not found. Install that." >&2
  1
 
logfail()
{
  message="$1"
  message="Failure: [`date`]: $message" 
 
  $message
 
  [ -w "$log_file" ];
    "$message" >> "$log_file"
 
 
  1
}
 
# Show usage if necessary
[ $# -eq 0 ]; then
    "Usage: $0 -s [sender] -n [numbers] -m [message] -u [username] -p [password]";
    "";
    "[numbers]  = SMS numbers (if multiple, enclose in quotes) to send message to.";
    "[message]  = Text of message you want to send";
    "[username] = Username assocated with Mollie  account";
    "[sender]   = Sender"
    "[password] = MD5 HTTP API Password assocated with Mollie account";
    ""
    "-d         = Dry run, pretend success."
    ""
    "The numbers, sender, username and password options are optional and";
    "override the account credentials defined in $config_file.";
    ""
    "A log file $log_file will be kept if it is writable to the user."
    "";
    1;
 
# Get command line arguments
[ "$1" != "" ] ;
    $1
   
  -n)
      # Get the SMS numbers that we should send message to
      numbers="$2";
      2;
      ;;
  -m)
      # Get the message we should send
      message="$2";
      2;
      ;;
  -s)
      # Get the sender to show in the SMS
      sender="$2";
      2;
      ;;
  -u)
      # Get the username
      username="$2";
      2;
      ;;
  -p)
      # Get the password
      password="$2";
      2;
      ;;
  -d)
      dry_run="dry_run";
      1;
      ;;
  *)
      "Unknown option: $1"
      1;
      ;;
   
  [ -z "$username" ];
  logfail "No username specified or found in $config_file."
  [ -z "$password" ];
  logfail "No password specified or found in $config_file."
  [ -z "$numbers" ];
  logfail "No numbers specified or found in $config_file."
 
message_length= -n "$message"|wc -c`
sender_length= -n "$sender"|wc -c`
  [ "$message_length" -gt "160" ];
  logfail "SMS message is longer than 160 chars ($message_length). That is not allowed." >&2
  [ "$sender_length" -gt "11" ];
  logfail "Sender is longer than 11 chars ($sender_length). That is not allowed." >&2
 
# 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.
number $numbers; 
  [ ! "$dry_run" ];
    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/`
 
    RESPONSE="<success>true</success>"
 
 
  # Curl was able to post okay...
  [ "$?" -eq "0" ];
    success_line_true= "$RESPONSE"|grep -i "<success>true</success>"`
 
    [ -z "$success_line_true" ];
      logfail "$message to $number. Response was: $RESPONSE"
   
 
    logfail "curl return an error while trying to contact mollie to send an SMS." >&2
 
 
  [ -w "$log_file" ];
    "Success: [`date`]: $dry_run Sent '$message' to $number" >> $log_file
 
  "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 )