' SendMMS.vbs - This is a VBScript that can be used to submit an MMS message to a NowSMS server. ' In order to use this script, you must change the URL below to point to your NowSMS server. ' The "User" and "Password" settings must point to a valid "SMS Users" account on your NowSMS server. Const DestURL = "http://127.0.0.1:8800/?user=test&password=test" ' To run this script from a command line interface, use the Windows cscript command. ' ' cscript sendmms.vbs recipient[,recipient2,recipient3...] filename [filename2] [filename3...] [variable=setting] ' recipient can contain either one recipient phone number, or a comma delimited list of recipients, e.g., +44777777777,+44777777778 (do not include spaces) ' filename is the name of a local file to be included in the MMS message ' variable=setting can specify additional URL parameters, such as MMSSubject=SubjectLine or MMSFrom=SenderAddress ' Note: If the Subject line contains a space character, put quotes around the setting, e.g., "MMSSubject=This is a test message" ' ' Examples: ' ' cscript sendmms.vbs +4477777777 image.gif filename.txt "MMSSubject=This is a test message" ' cscript sendmms.vbs +4477777777,+4477777778 image.gif "MMSText=This is some message text" MMSFrom=+44777777779 MMSSubject=Test ' ' ' Credit where credit is due, this script was adapted from the example at http://www.motobit.com/tips/detpg_uploadvbsie/ do_sendMMS Sub do_sendMMS() Dim FileContents Dim FileName, FieldName Dim FormData Dim aCounter, Arg Dim fso FormData = "" FieldName = "MMSFILE" 'Variable name for file upload 'Define MIME Boundary Const Boundary = "---------------------------NowSMS---VBScript---Boundary----" 'We need at least two arguments (Recipient Phone Number, File) If WScript.Arguments.Count < 2 Then InfoEcho 'Are some required objects missing? If InStr(CheckRequirements, "Error") > 0 Then InfoEcho Set fso = CreateObject("Scripting.FileSystemObject") aCounter = 1 'Argument counter For Each Arg In WScript.Arguments if (aCounter = 1) Then 'First argument is always the recipient phone number if (isValidPhoneNumber (Arg)) Then FormData = AppendBinary (FormData, AddVarToFormData (Boundary, "PhoneNumber", Arg)) Else Msg = "Invalid Phone Number: " + Arg + vbCrlf WScript.Echo Msg WScript.Quit End if Else 'Other arguments can be either a file or a variablename=value setting If InStr(Arg, "=") > 0 Then varName = Left (Arg, InStr(Arg, "=")-1) varValue = Mid (Arg, InStr(Arg, "=")+1) FormData = AppendBinary (FormData, AddVarToFormData(Boundary, varName, varValue)) Else If fso.FileExists(Arg) Then 'Get source file As a binary data. FileContents = GetFile(Arg) 'Build multipart/form-data document FormData = AppendBinary (FormData, AddFileToFormData(FileContents, Boundary, Arg, FieldName)) Else Msg = "File Not Found: " + Arg + vbCrlf WScript.Echo Msg WScript.Quit End if End if End if aCounter = aCounter + 1 Next 'close the multipart MIME FormData = AppendBinary (FormData, CloseFormData (Boundary)) 'Post the data To the destination URL WScript.Echo WinHttpPostRequest (DestURL, FormData, Boundary) End Sub 'function to test if the phone number is a valid format Function isValidPhoneNumber(theStr) LenString = Len(theStr) isValid = True for i = 1 to LenString midStr = Mid(theStr, i, 1) if (midStr = "+") AND (i = 1) Then elseif (midStr = "0") OR (midStr = "1") OR (midStr = "2") OR (midStr = "3") OR (midStr = "4") OR (midStr = "5") OR (midStr = "6") OR (midStr = "7") OR (midStr = "8") OR (midStr = "9") Then else isValid = False End if Next isValidPhoneNumber = isValid End Function 'Combine 2 binary strings into 1 Function AppendBinary(binary1, binary2) Dim BinaryData 'Build form data using recordset binary field Const adLongVarBinary = 205 Dim RS: Set RS = CreateObject("ADODB.Recordset") RS.Fields.Append "b", adLongVarBinary, LenB(binary1) + LenB(binary2) RS.Open RS.AddNew Dim LenData RS("b").AppendChunk (binary1) RS("b").AppendChunk (binary2) RS.Update BinaryData = RS("b") RS.Close AppendBinary = BinaryData End Function 'Add a file to a multipart/form-data document Function AddFileToFormData(FileContents, Boundary, FileName, FieldName) Dim FormData, Pre, Po Const ContentType = "application/upload" 'The two parts around file contents In the multipart-form data. Pre = "--" + Boundary + vbCrLf + mpFields(FieldName, FileName, ContentType) Po = vbCrLf 'Build form data using recordset binary field Const adLongVarBinary = 205 Dim RS: Set RS = CreateObject("ADODB.Recordset") RS.Fields.Append "b", adLongVarBinary, Len(Pre) + LenB(FileContents) + Len(Po) RS.Open RS.AddNew Dim LenData 'Convert Pre string value To a binary data LenData = Len(Pre) RS("b").AppendChunk (StringToMB(Pre) & ChrB(0)) Pre = RS("b").GetChunk(LenData) RS("b") = "" 'Convert Po string value To a binary data LenData = Len(Po) RS("b").AppendChunk (StringToMB(Po) & ChrB(0)) Po = RS("b").GetChunk(LenData) RS("b") = "" 'Join Pre + FileContents + Po binary data RS("b").AppendChunk (Pre) RS("b").AppendChunk (FileContents) RS("b").AppendChunk (Po) RS.Update FormData = RS("b") RS.Close AddFileToFormData = FormData End Function 'Add a variable to multipart/form-data document Function AddVarToFormData(Boundary, varName, varValue) Dim FormData, Pre, Po, binValue 'The two parts around file contents In the multipart-form data. Pre = "--" + Boundary + vbCrLf + mpFields2(varName) Po = vbCrLf 'Build form data using recordset binary field Const adLongVarBinary = 205 Dim RS: Set RS = CreateObject("ADODB.Recordset") RS.Fields.Append "b", adLongVarBinary, Len(Pre) + Len(varValue) + Len(Po) RS.Open RS.AddNew Dim LenData 'Convert Pre string value To a binary data LenData = Len(Pre) RS("b").AppendChunk (StringToMB(Pre) & ChrB(0)) Pre = RS("b").GetChunk(LenData) RS("b") = "" 'Convert varValue string value To a binary data LenData = Len(varValue) RS("b").AppendChunk (StringToMB(varValue) & ChrB(0)) binValue = RS("b").GetChunk(LenData) RS("b") = "" 'Convert Po string value To a binary data LenData = Len(Po) RS("b").AppendChunk (StringToMB(Po) & ChrB(0)) Po = RS("b").GetChunk(LenData) RS("b") = "" 'Join Pre + FileContents + Po binary data RS("b").AppendChunk (Pre) RS("b").AppendChunk (binValue) RS("b").AppendChunk (Po) RS.Update FormData = RS("b") RS.Close AddVarToFormData = FormData End Function 'Close multipart/form-data document with final boundary Function CloseFormData(Boundary) Dim FormData, Po Po = "--" + Boundary + "--" + vbCrLf 'Build form data using recordset binary field Const adLongVarBinary = 205 Dim RS: Set RS = CreateObject("ADODB.Recordset") RS.Fields.Append "b", adLongVarBinary, Len(Po) RS.Open RS.AddNew Dim LenData 'Convert Po string value To a binary data LenData = Len(Po) RS("b").AppendChunk (StringToMB(Po) & ChrB(0)) Po = RS("b").GetChunk(LenData) RS("b") = "" 'Join Pre + FileContents + Po binary data RS("b").AppendChunk (Po) RS.Update FormData = RS("b") RS.Close CloseFormData = FormData End Function 'Infrormations In form field header. Function mpFields(FieldName, FileName, ContentType) Dim MPTemplate 'template For multipart header MPTemplate = "Content-Disposition: form-data; name=""{field}"";" + _ " filename=""{file}""" + vbCrLf + _ "Content-Type: {ct}" + vbCrLf + vbCrLf Dim Out Out = Replace(MPTemplate, "{field}", FieldName) Out = Replace(Out, "{file}", FileName) mpFields = Replace(Out, "{ct}", ContentType) End Function 'Infrormations In form field header. Function mpFields2(FieldName) Dim MPTemplate 'template For multipart header MPTemplate = "Content-Disposition: form-data; name=""{field}""" + vbCrLf + vbCrLf mpFields2 = Replace(MPTemplate, "{field}", FieldName) End Function 'Returns file contents As a binary data Function GetFile(FileName) Dim Stream: Set Stream = CreateObject("ADODB.Stream") Stream.Type = 1 'Binary Stream.Open Stream.LoadFromFile FileName GetFile = Stream.Read Stream.Close End Function 'Converts OLE string To multibyte string Function StringToMB(S) Dim I, B For I = 1 To Len(S) B = B & ChrB(Asc(Mid(S, I, 1))) Next StringToMB = B End Function 'Basic script info Sub InfoEcho() Dim Msg Msg = Msg + "Send MMS message via NowSMS" & vbCrLf & vbCrlf Msg = Msg + "usage:" & vbCrLf Msg = Msg + "cscript sendmms.vbs recipient[,recipient2,recipient3...] filename [filename2] [filename3...] [variable=setting]" & vbCrLf Msg = Msg + " filename ... Local file To include in MMS message" & vbCrLf Msg = Msg + " variable=setting ... Additional URL paremeters, e.g., MMSSubject=Test" & vbCrLf Msg = Msg + vbCrLf + CheckRequirements WScript.Echo Msg WScript.Quit End Sub 'Checks If all of required objects are installed Function CheckRequirements() Dim Msg Msg = "This script requires some objects installed To run properly." & vbCrLf Msg = Msg & CheckOneObject("ADODB.Recordset") Msg = Msg & CheckOneObject("ADODB.Stream") CheckRequirements = Msg ' MsgBox Msg End Function 'Checks If the one object is installed. Function CheckOneObject(oClass) Dim Msg On Error Resume Next CreateObject oClass If Err = 0 Then Msg = "OK" Else Msg = "Error:" & Err.Description CheckOneObject = oClass & " - " & Msg & vbCrLf End Function 'sends multipart/form-data To the URL using WinHttprequest/XMLHTTP 'FormData - binary (VT_UI1 | VT_ARRAY) multipart form data Function WinHTTPPostRequest(URL, FormData, Boundary) Dim http 'Create XMLHTTP/ServerXMLHTTP/WinHttprequest object 'You can use any of these three objects. 'Set http = CreateObject("WinHttp.WinHttprequest.5") Set http = CreateObject("MSXML2.XMLHTTP") 'Set http = CreateObject("MSXML2.ServerXMLHTTP") 'Open URL As POST request http.Open "POST", URL, False 'Set Content-Type header http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary 'Send the form data To URL As POST binary request http.send FormData 'Get a result of the script which has received upload WinHTTPPostRequest = http.responseText End Function