Menu

Expand
Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Send SMS and MMS Messages In C++

Today we'll use C++, some standard libraries, libcurl, and some POSIX APIs to send SMS and MMS messages using the Twilio REST API. You'll need a (mostly) POSIX-compliant build environment as well as libcurl to follow along with this guide.

Let's get started!

Sign Up for (or Sign In to) a Twilio Account

You'll need to login to your existing Twilio account, or create a new account.

Don't have an account yet? Sign up for a free Twilio trial, post-haste.

Purchase or Find a SMS-Capable Number

You'll need a SMS (or optionally, MMS) capable number to complete this demo. Either use a phone number that you already have, or purchase a new one with SMSes enabled.

From the Twilio Console, here's where you can determine the capabilities of your purchased number:

Checking a Twilio Phone Number for SMS Capability

If you need to purchase a number, after navigating to the 'Buy a Number' link click the SMS checkbox (and the MMS checkbox, if you wish to send media messages).

Buy a SMS-Capable Twilio Phone Number

Prerequisites for This C++ MMS and SMS Guide

We're targeting POSIX Compliant environments with this guide, and tested our code with clang-800.0.42.1 on Mac OSX 10.11, gcc version 4.9.2 on Raspbian 8.0 (kernel 4.4.38-v7+) and gcc 5.4.0 in Cygwin 2.877 on Windows 7 64-Bit.

To build, you will need to ensure you have:

libcurl was already on the Mac. On the Raspberry Pi, you can install a suitable version with:

sudo apt-get install libcurl4-openssl-dev

In Windows 7 and Cygwin, you can install libcurl with Cygwin Setup:

Installing libcurl-devel and curl in Cygwin

Building cpp_demo

All three environments worked with this example Makefile. You may need to make minor edits - usually to the library search paths - to build in your environment.

On almost all *NIX-type systems (including Windows/Cygwin), building should be very similar to the following lines (you might even be able to copy, paste and execute):

git clone https://github.com/TwilioDevEd/twilio_cpp_demo.git
cd twilio_cpp_demo
make

Send an SMS or MMS Message with C++

We've just built cpp_demo, which attempts to send an SMS or MMS based on your inputs from the command line. src/twilio.cc and include/twilio.hh demonstrate a class, Twilio, which should be easy to integrate into your own codebase. (Note that before putting it into production, you need to add input validation and safety. The first order of concern: the code in its current form can be passed additional HTTP parameters through the command line inputs.)

For C++ developers, our C SMS and MMS guide might give some key insights into the following code - but let's take a closer look to see what's happening under the hood.

  • First, we instantiate a Twilio object with our account credentials (find them on the Twilio Console).
  • Second, we call the method send_message with the to/from numbers, the message, and optionally a URL to a picture and a boolean of whether we want verbose output.
        
        
        
        
        Instantiating a Twilio class and sending a SMS or MMS message with C++ and libcurl.

        Using the Twilio REST API from C++

        Instantiating a Twilio class and sending a SMS or MMS message with C++ and libcurl.
        • Third, once inside the code we first do some exception checking (making sure the message is the proper size), followed by setting up libcurl with our eventual HTTP POST
        • Fourth, curl makes the HTTP POST and we clean up with curl_easy_cleanup.
        • Finally, we check the response for success or failure, and return the result to the calling function.
              
              
              
              
              The send_message method from the Twilio class from the CPP Messaging demo in C++.

              The C++ Twilio Class

              The send_message method from the Twilio class from the CPP Messaging demo in C++.

              Running cpp_demo

              Running cpp_demo from the command line is easy once you have it built. Merely set your account credentials and call the code as follows:

              account_sid=ACXXXXXXXXXXXXXXXXXXXXXX
              auth_token=your_auth_token
              bin/cpp_demo -a $account_sid -s $auth_token -t "+18005551212" -f "+18005551213" -m "Hello, World!"
              # Optionally, use '-p http://some/path/to/img.jpg' to send an MMS.
              

              Getting C-Rious With Twilio and C++

              After this demo, you now have the means to add communications to your new or existing C++ applications. Add monitoring or help to your C++ application, or bring your own use case.

              Whether you're a master of the language or closer to a practitioner of "C with classes" please do let us know on Twitter when you've got something built!

              Paul Kamp Kat King Maylon Pedroso
              Rate this page:

              Need some help?

              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.