SQL Server Requires Chilkat v11.0.0+
SQL Server
Send an MMS with Twilio using a media URL
See more Twilio Examples
Send an image and an optional caption from an MMS-capable Twilio number. Supply an MMS-supported destination in E.164 format and replace the example media URL with an HTTPS URL Twilio can fetch. The host must handle HEAD and GET and return the correct Content-Type for the file.
Live send. With live credentials and valid inputs, this request submits a billable message. Save the Message SID to check delivery later. Supported media types and size limits.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'BasicAuth', 1
-- Load the owning Account SID and an API key from application configuration.
DECLARE @accountSid nvarchar(4000)
SELECT @accountSid = 'YOUR_ACCOUNT_SID'
EXEC sp_OASetProperty @http, 'Login', 'YOUR_API_KEY_SID'
EXEC sp_OASetProperty @http, 'Password', 'YOUR_API_KEY_SECRET'
DECLARE @url int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT
EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://api.twilio.com/2010-04-01/Accounts/'
EXEC sp_OAMethod @url, 'Append', @success OUT, @accountSid
EXEC sp_OAMethod @url, 'Append', @success OUT, '/Messages.json'
-- Add raw field values. Chilkat form-encodes them, including the + in a phone number.
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'From', 'YOUR_MMS_ENABLED_NUMBER'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'To', 'RECIPIENT_E164_NUMBER'
-- Supply a URL for the file itself so Twilio can retrieve the media.
EXEC sp_OAMethod @req, 'AddParam', NULL, 'MediaUrl', 'https://media.example.com/photo.jpg'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'Body', 'Photo attached.'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @reply int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @sTmp0, @req, @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
DECLARE @parsed int
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @reply, 'Load', @parsed OUT, @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) or (@iTmp0 <> 201)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP ' + @iTmp0
EXEC sp_OAMethod @reply, 'HasMember', @iTmp0 OUT, 'code'
IF (@parsed = 1) and (@iTmp0 = 1)
BEGIN
EXEC sp_OAMethod @reply, 'IntOf', @iTmp0 OUT, 'code'
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'message'
PRINT 'Twilio error ' + @iTmp0 + ': ' + @sTmp0
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
IF @parsed <> 1
BEGIN
PRINT 'Expected a JSON response.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'sid'
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp1 OUT, 'status'
IF (@iTmp0 <> 1) or (@iTmp1 <> 1)
BEGIN
PRINT 'The create response is missing the message SID or status.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
RETURN
END
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'sid'
PRINT 'Message SID: ' + @sTmp0
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'status'
PRINT 'Initial status: ' + @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
END
GO