question

591 Simon avatar image
591 Simon asked ·

Sending SMS using VBA with attachment

Hello,

Does anyone have a working code of being able to send MMS (SMS with attachment using multi-part/mixed content?

Similar to

https://developers.ringcentral.com/api-reference/SMS/createSMSMessage

Simiar to

Example 2 : Send MMS

smsmmsvba
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered ·

I am not an expert on VBA so I cannot help. Not sure anyone else in this community could help you but I recommend you also post your question on stack overflow with more generic to how to post attachment with multi-part/mixed content.

Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

591 Simon avatar image
591 Simon answered ·

@Phong Vu

Based on your advise, I have checked in Stackoverflow. I was able to gather up some pieces of information and built the below function. It is working for png, jpeg, .mpeg but failing for .wmv and .mov.

Is the Mime Type I am using wrong for .wmv (video/x-ms-wmv) and .mov(video/x-ms-wmv) ?


I am sure that the file size that I used was small (<500KB).




{

"errorCode" : "InvalidContent",

"message" : "Unsupported attachment media type, attachment [1]: [video2.wmv]",

"errors" : [ {

"errorCode" : "MSG-348",

"message" : "Unsupported attachment media type, attachment [1]: [video2.wmv]",

"attachmentFileName" : "video2.wmv",

"attachmentNumber" : "1"

} ],

"attachmentFileName" : "video2.wmv",

"attachmentNumber" : "1"

}


Function SendSMSWithAttachment(ByVal ToPhone As String, ByVal SMSMessage As String, ByVal StrFilePath As String) As String


'strFilePath is the file to upload (C:\My Documents\test.zip)

Const MULTIPART_BOUNDARY = "---------------------------0123456789012"

Dim Ado

Dim BytFormData, BytFile

Dim StrFormStart, StrFormEnd, StrFormStartUTF8, StrFormEndUTF8

Dim Web

Dim StrFileField, StrFileExtension, MimeType As String

Dim JSONObject As Object


'Read the file into a byte array

Set Ado = CreateObject("ADODB.Stream")

Ado.Type = 1

Ado.Open

Ado.LoadFromFile StrFilePath

BytFile = Ado.Read

Ado.Close


'Get the File Extension

StrFileExtension = VBA.UCase(Mid(StrFilePath, InStrRev(StrFilePath, ".") + 1))

If ((StrFileExtension = "JPG") Or (StrFileExtension = "JPEG")) Then

MimeType = "image/jpeg"

ElseIf (StrFileExtension = "PNG") Then

MimeType = "image/png"

ElseIf (StrFileExtension = "GIF") Then

MimeType = "image/gif"

ElseIf (StrFileExtension = "BMP") Then

MimeType = "image/bmp"

ElseIf ((StrFileExtension = "TIF") Or (StrFileExtension = "TIFF")) Then

MimeType = "image/tiff"

ElseIf (StrFileExtension = "SVG") Then

MimeType = "image/svg+xml"

ElseIf (StrFileExtension = "3GP") Then

MimeType = "video/3gpp"

ElseIf (StrFileExtension = "MP4") Then

MimeType = "video/mp4"

ElseIf (StrFileExtension = "MPEG") Then

MimeType = "video/mpeg"

ElseIf (StrFileExtension = "FLV") Then

MimeType = "video/x-flv"

ElseIf (StrFileExtension = "WMV") Then

MimeType = "video/x-ms-wmv"

ElseIf (StrFileExtension = "MOV") Then

MimeType = "video/quicktime"

ElseIf (StrFileExtension = "MP3") Then

MimeType = "audio/mpeg3"

ElseIf ((StrFileExtension = "VCF") Or (StrFileExtension = "VCARD")) Then

MimeType = "text/vcard"

ElseIf (StrFileExtension = "ZIP") Then

MimeType = "application/zip"

ElseIf (StrFileExtension = "RTF") Then

MimeType = "application/rtf"

ElseIf (StrFileExtension = "HTML") Then

MimeType = "text/html"

Else

MimeType = "text/html"

End If



'Create the multipart form data.

'Define the end of form

StrFormEnd = vbCrLf & vbCrLf & "--" & MULTIPART_BOUNDARY & "--" & vbCrLf

'First add any ordinary form data pairs

StrFormStart = ""


StrFormStart = StrFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf

StrFormStart = StrFormStart & "Content-Type: application/json; charset=UTF-8" & vbCrLf

StrFormStart = StrFormStart & "Content-Transfer-Encoding: 8bit" & vbCrLf & vbCrLf

StrFormStart = StrFormStart & "{" & Chr(34) & "to" & Chr(34) & ":[{" & Chr(34) & "phoneNumber" & Chr(34) & ": " & Chr(34) & ToPhone & Chr(34) & "}]," & vbCrLf

StrFormStart = StrFormStart & Chr(34) & "text" & Chr(34) & ":" & Chr(34) & SMSMessage & Chr(34) & "," & vbCrLf

StrFormStart = StrFormStart & Chr(34) & "from" & Chr(34) & ":{" & Chr(34) & "phoneNumber" & Chr(34) & ": " & Chr(34) & LoginUserName & Chr(34) & "}}"

StrFormStart = StrFormStart & vbCrLf & vbCrLf


'Now add the header for the uploaded file

StrFileField = Mid(StrFilePath, InStrRev(StrFilePath, "\") + 1)

StrFormStart = StrFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf

StrFormStart = StrFormStart & "Content-Type: " & MimeType & vbCrLf

StrFormStart = StrFormStart & "Content-Disposition: attachment; "

StrFormStart = StrFormStart & "filename=""" & StrFileField & """"

StrFormStart = StrFormStart & "Content-Transfer-Encoding: base64"


StrFormStart = StrFormStart & vbCrLf & vbCrLf


BytFormData = StrFormStart & EncodeBase64(BytFile) & StrFormEnd


'Upload it

Set Web = CreateObject("WinHttp.WinHttpRequest.5.1")

Web.Open "POST", SMSURI, False

Web.SetRequestHeader "Content-Type", "multipart/mixed; boundary=" & MULTIPART_BOUNDARY

Web.SetRequestHeader "Authorization", "Bearer " + AccessToken

Web.setTimeouts 300000, 300000, 300000, 3000000

Web.Send BytFormData

If (Web.Status) = 200 Then

Set JSONObject = WebHelpers.ParseJson(Web.responseText)

SendSMSWithAttachment = JSONObject("conversation")("id")

Else

Err.Raise 11041 + vbObjectError, "SendSMSWithAttachment", _

Web.Status & ": " & Web.responseText

End If

End Function


Private Function EncodeBase64(bytes) As String


Dim objXML As msxml2.DOMDocument60

Dim objNode As msxml2.IXMLDOMElement



Set objXML = New msxml2.DOMDocument60

Set objNode = objXML.createElement("b64")


objNode.DataType = "bin.base64"

objNode.nodeTypedValue = bytes

EncodeBase64 = objNode.Text


Set objNode = Nothing

Set objXML = Nothing

End Function


Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered ·

It looks like we don't support .wmv video for outbound MMS. Here are the list of supported mime types for outbound MMS:

images

.jpg / .jpeg => image/jpeg

.png => image/png

.gif => image/gif

.bmp => image/bmp

.svg => image/svg+xml

.tif / .tiff => image/tiff

files

.vcard => vcf/vcard

.gz => application/gzip

.zip => application/gzip

.rtf => application/rtf

audio

.mp3 => audio/mpeg

.m4a => audio/mp4

.arm => audio/amr

video

.mp4 => video/mp4

.mpeg => video/mpeg

.mpg => video/mpeg

Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.