Sending outbound SMS messages with MessageBird

⏱ 5 min build time || Watch the Video Tutorial

In this MessageBird Developer Tutorial you’ll learn how to send your first SMS using the MessageBird SMS Messaging API.

Before we get started, have you set up your PHP development environment and project directory with the MessageBird SDK?

  • No! Make sure you to set this up before continuing; you can read this MessageBird Developer Tutorial to learn how to do so.
  • Yes! Great! Now you can make your first API request and send an SMS message with MessageBird using PHP.

Getting started

First, let's create a new file in the directory of your composer.json file and call it send_sms.php.

In this file, first include the Composer autoloader so you can access the SDK (and other packages):

<?php
require_once __DIR__.'/vendor/autoload.php';

Then, initialize the SDK by creating a new instance of the MessageBird\Client class. The constructor takes a single argument, your API key. Next, to start testing the SDK, you can simply replace the string YOUR-API-KEY in the following PHP code and thus hardcode your API key.

Pro-tip: For production applications, we recommended storing the key in a configuration file or environment variable instead and passing this variable with the key to the constructor.

$messagebird = new MessageBird\Client('YOUR-API-KEY');

The SDK defines a PHP class for SMS messages. Create an instance of this class and then assign values for the required attributes originator, recipients and body:

$message = new MessageBird\Objects\Message;
$message->originator = '31970XXXXXXX';
$message->recipients = [ '31970YYYYYYY' ];
$message->body = 'Hello World, I am a text message and I was hatched by PHP code!';

But wait, what do these attributes mean? 🤔

  • The originator is the sender for the message; it can be a telephone number including country code or an alphanumeric string (with a maximum length of 11 characters). You can use the number you bought as part of our Getting Started tutorial as originator. Keep in mind that alphanumeric senders are not supported in every country including the United States, so it’s important to check the country restrictions. If you can't use alphanumeric IDs, use a real phone number instead. You can check our originator article in Help Center to learn more about this topic.

  • The recipients are the phone numbers that will receive the message. All numbers should be in the international format with country code. You must specify this attribute as an array even if you have just a single recipient. You can send a message to up to 50 numbers at a time. For testing, add your own mobile telephone number here so you can receive the test message on your phone.

  • The body is the content of the message. Keep in mind that SMS messages are limited to 160 characters, and if you enter a longer value for the body it will be split into multiple parts which are each sent and billed separately.

Make sure to replace the values in the sample code with adequate data for testing. There are additional optional attributes as well; you can find them in our SMS Messaging reference documentation.

Once the message object is ready, you can send it through the API. Call messages->create() on the SDK instance object and pass your previously created message object as the first argument:

$response = $messagebird->messages->create($message);
var_dump($response);

Your first PHP script is ready. Let’s go ahead and save it!

Keep in mind that the MessageBird SDK throws an exception when something goes wrong with your API request. It's okay for now, but in production applications, you should place any API call between try and catch and provide user-friendly error handling inside the catch block.

Enough said, let's try running it from the command line:

php send_sms.php

If everything works fine, you should see the API response as output from the script. If you used a live API key and verified your number or added test credits to your account the message will arrive on your phone in seconds.

Nice work! 🎉

You've just sent your first SMS message with MessageBird's SMS Messaging API using PHP!

Start building!

Head over to the next MessageBird Developer Tutorial and learn how to set up inbound messaging functionality.

Want to start building your solution but not quite sure how to get started? Feel free to let us know at support@messagebird.com; we'd love to help!

Questions?

We’re always happy to help with code or other doubts you might have! Check out our Quickstarts, API Reference, Tutorials, SDKs, or contact our Support team.

Cookie Settings