Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.0.0+

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.

Twilio Messages API reference

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_BasicAuth $http 1
#  Load the owning Account SID and an API key from application configuration.
set accountSid "YOUR_ACCOUNT_SID"
CkHttp_put_Login $http "YOUR_API_KEY_SID"
CkHttp_put_Password $http "YOUR_API_KEY_SECRET"
set url [new_CkStringBuilder]

CkStringBuilder_Append $url "https://api.twilio.com/2010-04-01/Accounts/"
CkStringBuilder_Append $url $accountSid
CkStringBuilder_Append $url "/Messages.json"

#  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "application/x-www-form-urlencoded"
CkHttpRequest_AddParam $req "From" "YOUR_MMS_ENABLED_NUMBER"
CkHttpRequest_AddParam $req "To" "RECIPIENT_E164_NUMBER"
#  Supply a URL for the file itself so Twilio can retrieve the media.
CkHttpRequest_AddParam $req "MediaUrl" "https://media.example.com/photo.jpg"
CkHttpRequest_AddParam $req "Body" "Photo attached."

set resp [new_CkHttpResponse]

set reply [new_CkJsonObject]

set success [CkHttp_HttpReq $http [CkStringBuilder_getAsString $url] $req $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

set parsed [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {expr [$success != 1]  ||  [[CkHttpResponse_get_StatusCode $resp] != 201]} then {
    puts "HTTP [CkHttpResponse_get_StatusCode $resp]"
    if {expr [$parsed == 1]  &&  [[CkJsonObject_HasMember $reply "code"] == 1]} then {
        puts "Twilio error [CkJsonObject_IntOf $reply code]: [CkJsonObject_stringOf $reply message]"
    }

    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

if {$parsed != 1} then {
    puts "Expected a JSON response."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $reply "sid"] != 1]  ||  [[CkJsonObject_JsonTypeOf $reply "status"] != 1]} then {
    puts "The create response is missing the message SID or status."
    delete_CkHttp $http
    delete_CkStringBuilder $url
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    exit
}

puts "Message SID: [CkJsonObject_stringOf $reply sid]"
puts "Initial status: [CkJsonObject_stringOf $reply status]"

delete_CkHttp $http
delete_CkStringBuilder $url
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkJsonObject $reply