Private Sub SendSMS()
Dim xmlHttp As Object
Dim URL As String
Dim AccountSID As String
Dim AuthToken As String
Dim EncodedAuth As String
Dim ToNumber As String
Dim FromNumber As String
Dim Body As String
Dim PostData As String
' Twilio account details
AccountSID = "123456789" ' Replace with your actual Account SID
AuthToken = "YourAuthToken" ' Replace with your actual Auth Token
URL = "https://api.twilio.com/2010-04-01/Accounts/" & AccountSID & "/Messages.json"
' SMS details
ToNumber = "+39123456789" ' Replace with the recipient's phone number
FromNumber = "+12345687" ' Replace with your Twilio phone number
Body = "cvbcvbcvbcvb" ' Replace with your message body
' Encode authentication in base64
EncodedAuth = Base64Encode(AccountSID & ":" & AuthToken)
' Prepare POST data
PostData = "To=" & URLEncode(ToNumber) & "&From=" & URLEncode(FromNumber) & "&Body=" & URLEncode(Body)
' Create an XMLHTTP object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
' Make the POST request
xmlHttp.Open "POST", URL, False
xmlHttp.setRequestHeader "Authorization", "Basic " & EncodedAuth
xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlHttp.Send PostData
' Check the response
If xmlHttp.Status = 201 Then
MsgBox "SMS sent successfully!"
Else
MsgBox "Error sending SMS: " & xmlHttp.responseText
End If
' Clean up
Set xmlHttp = Nothing
End Sub
' Function to URL encode a string
Private Function URLEncode(ByVal str As String) As String
Dim i As Long
Dim ch As String
Dim res As String
res = ""
For i = 1 To Len(str)
ch = Mid(str, i, 1)
Select Case Asc(ch)
Case 48 To 57, 65 To 90, 97 To 122 ' 0-9, A-Z, a-z
res = res & ch
Case Else
res = res & "%" & Right("0" & Hex(Asc(ch)), 2)
End Select
Next i
URLEncode = res
End Function
' Function to encode a string in Base64
Private Function Base64Encode(ByVal str As String) As String
Dim objXML As Object
Dim objNode As Object
Set objXML = CreateObject("MSXML2.DOMDocument")
Set objNode = objXML.createElement("base64")
objNode.DataType = "bin.base64"
objNode.NodeTypedValue = StrConv(str, vbFromUnicode)
Base64Encode = objNode.Text
Set objNode = Nothing
Set objXML = Nothing
End Function