Send SMS from a Command Line Interface

A command line interface can sometimes be useful for quick sending of an SMS message from another application.

A message thread on our discussion board provided a simple example that has been referenced frequently. The purpose of this document is to provide an updated script which offers more flexibility. This updated script allows recipient phone numbers, and the message text, to be specified on the command line.

Assuming that the script is saved as a file named sms.js, you would issue the following command:

cscript sms.js PhoneNumber1[,PhoneNumber2,…] SMS Message Text

(cscript.exe is the Windows Script Host, which is a component of Windows which should be located in the \Windows\System32 directory.)

Examples:

cscript sms.js +44777777777 This is a test message

cscript sms.js +44777777777,+44777777778 This is a test message to 2 recipients

The SMS.JS script file is displayed below. Save everything between the “—begin sms.js—” and “—end sms.js—” markers to a file named SMS.JS. Edit the NowSMSServerAddress, NowSMSUserName and NowSMSPassword variable settings as appropriate for your installation.  This script is also available at the following link:  https://nowsms.com/download/sms.js.txt

—begin sms.js—

/*

This is a command line script for sending an SMS message via NowSMS.

Below, you must substitute in the address of your NowSMS server, plus a valid username and password for

an account defined in the “SMS Users” list of that server.

Note: This script uses the encodeURIComponent method introduced in Internet Explorer 5.5. If you are running

Windows 2000 or an earlier version of Windows, you must have Internet Explorer 5.5 or later installed for this

script to work.

*/

var NowSMSServerAddress = “http://127.0.0.1:8800”;

var NowSMSUserName = “test”;

var NowSMSPassword = “test”;

function HTTPGET(strURL)

{

var strResult;

try

{

// Create the WinHTTPRequest ActiveX Object.

var WinHttpReq = new ActiveXObject(“Msxml2.XMLHTTP” /* or “WinHttp.WinHttpRequest.5″*/);

// Create an HTTP request.

var temp = WinHttpReq.Open(“GET”, strURL, false);

// Send the HTTP request.

WinHttpReq.Send();

// Retrieve the response text.

strResult = WinHttpReq.ResponseText;

}

catch (objError)

{

strResult = objError + “\n”

strResult += “WinHTTP returned error: ” +

(objError.number & 0xFFFF).toString() + “\n\n”;

strResult += objError.description;

}

// Return the response text.

return strResult;

}

var strRequest;

if (WScript.Arguments.Count() < 2) {

WScript.Echo (“Usage: ” + WScript.ScriptName + ” PhoneNumber1[,PhoneNumber2,…] Message Text\r\n”);

WScript.Quit();

}

strRequest = NowSMSServerAddress + “/?PhoneNumber=” + encodeURIComponent(WScript.Arguments(0)) + “&User=” + encodeURIComponent(NowSMSUserName) + “&password=” + encodeURIComponent(NowSMSPassword) + “&text=”;

for (i=1; i<WScript.Arguments.Count(); i++)

{

if (i > 1) {

strRequest += “%20”;

}

strRequest += encodeURIComponent(WScript.Arguments(i));

}

WScript.Echo(HTTPGET(strRequest));

—end sms.js—

System Requirements for using this script:

This script connects to a NowSMS server and posts a request to the NowSMS server to send a message.

If you do not have a NowSMS server installed, this script will not work.

NowSMS server software is installed on a Windows PC and to be able to send SMS or MMS messages, NowSMS also requires a GSM modem, Android phone, or SMS service provider connection.

Trial versions of the NowSMS software can be downloaded at https://nowsms.com/download-free-trial

Either the Now SMS/MMS Gateway or NowSMS Lite can be used. NowSMS Lite can send SMS and MMS messages using a single GSM modem or Android phone as a modem connection. The full product supports multiple modems and/or service provider connections.

Before attempting to interface with NowSMS using scripts, you should verify that NowSMS is configured correctly and can send SMS messages using its built in web interface. For Android phone or GSM modem connections, there are quick start guides that can be found here: https://nowsms.com/doc/quick-start-guide

Once you have verified that NowSMS is working on its own, it will be necessary to change IP addresses referenced in the script to point to your NowSMS server. Our sample scripts use an IP address of 127.0.0.1, which is a special loopback address for the current PC. If your script is running on a different system than the NowSMS server, change 127.0.0.1 to a host name or IP address that is valid for your environment. It is also necessary to define an SMS user account on the NowSMS server with account credentials that match with the user account in the script.