News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Sending SMS using VBA with attachment
Tags: rest api
Dec 23, 2019 at 9:26am   •   4 replies  •  0 likes
591 Simon

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

3 Answers
answered on Dec 26, 2019 at 8:56am  

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


 0
answered on Dec 24, 2019 at 8:16am  

@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



 0
answered on Dec 23, 2019 at 11:00am  

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.


 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us