How to send SMS from your website using Twilio [code to download available]

Comment envoyer des SMS depuis votre site web grâce à Twilio

Sending text messages from your website with Twilio can be easy, depending on the programming language you are using. Email marketing and phone calls have become relatively complicated. Indeed, a study has shown that the rate of opening emails has dropped relatively in recent years. If you have a website and are planning to try SMS marketing as an additional way to target customers, this article is for you.

This article looks at how you can send SMS from your website with Twilio. At the end of the article, you will add the Twilio SDK to your project and add a form to send SMS. You will also see how to get a Twilio number and API for your project.

Prerequisite for the tutorial

Our article is intended for three types of profiles:

  • Developers want to learn how to do it themselves (follow the steps).
  • A business owner doesn’t have enough time and wants a workable solution (Download code).
  • For anyone curious and would like to learn.

Required for developers

To follow this tutorial and easily apply it to your project, you should have the following basics:

  • Basic skills in PHP.
  • Understand how HTML tags work.
  • Some skills in web programming.

For a business owner

You can read the article to understand how it works. However, we have provided two options that can help you:

Please get in touch with us if you have any questions.

An approach for developers to integrate the sending of SMS

To configure sending SMS from your site, you must follow the steps below.

Step 1 – Create a Twilio account and get the settings

To start, you will be required to create a Twilio account and acquire specific parameters such as API sid, API authentication code and telephone number. For example, you can create API credentials for your project in your dashboard. When you sign up, you get a personalized phone number that you can use to automate texting.

How to create a Twilio account

To create a Twilio account, go to twilio.com and click on “register.” From there, you will see a registration form that gives you the option to try for free. The next step will be to enter the email address then the password you need. Then the software will send you an email confirmation, from which you can approve and access your account. First, you will have a free credit that you can use to send SMS from your site or benefit from other Twilio services.

Formulaire de création de compte Twilio

Notes

The procedure for creating an account on Twilio may change depending on their terms and conditions. I advise you to follow and read the steps of the registration process.

Get API SID and Authentication Code

After creating your account and logging into your dashboard, you will find your Account SID and your authentication code under the Dashboard ⇒ General tab (the link is console/project/settings). You can then copy these two pieces of information for your project.

Get your Twilio account number

To get your Twilio phone number, click on the three-dot menu panel at the bottom left, then a menu tab will display the menu link. From there, you will see a list of phone numbers under the super network heading.

Step 2 – Twilio PHP classes

Thanks to PHP classes or SDK, you can easily interact with APIs. You can either use the composer to download or do it manually. So, we generally recommend Compositor because it downloads with all updates. A manual update may be out of date or lack dependency. In addition, we will see how to use the two approaches:

Download using Composer and generate the “Composer. phar” file.

In addition, you can find more information about using the composer on the getcomposer.org site. Thus, you will find instructions there and detailed documentation on how to use the composer within the framework of a project.

Note that you must have command line access to use and download the composer.

Below are the general steps:

  • On the website,getcomposer.org, click on the “Getting Started” button. You will see the documentation for creating the composer.phar file.

Using the command line, you can run the code below.


php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Note that you may need to change the version of the sha256 depending on the current version. Read the article on the website getcomposer.org for more details.

Running the above code will generate the composer.phar file. To get the composer.json file, you can do the following:

Generate the composer.json file to download the Twilio SDK

If you want more information on creating a composer.json file, you can refer to the official documentation. For this tutorial, you just need to create the composer.json file and paste the following code.


{
    "require": {
        "twilio/sdk": "^6.5"
    }
}

Then from the command line you can run the code

composer require twilio/sdk

If you follow the instructions carefully, you will see a new vendor folder in your leading directory with some PHP files for your projects.

Include the Twilio SDK in your project

If you followed the steps above carefully, you can now include the SDK and send SMS from your website. The code below will help you text anyone from your website.


<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    '+15558675309',
    [
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+15017250604',
        // the body of the text message you'd like to send
        'body' => 'Hey Jenny! Good luck on the bar exam!'
    ]
);
?>
Instructions for using the code
  • Replace it with sid (line 9) as well as the code (line 10) by the identification you obtained in step 1.
  • You can then replace the phone number on line 16 with the recipient’s phone number (the person you want to send an SMS to).
  • Finally, on line 19, you will replace the phone number with your Twilio phone number.

Step 3 – Create a form to send SMS from your website

Now that you have the code to send an SMS, you can create a form that will save you from doing it manually. For example, with the code above, you can change the recipient’s phone number to send the SMS. However, if you want to automate the process, you can create a form from your dashboard to accomplish this. Below is an example of a form you can use.

Sample HTML-index.html code

The HTML code corresponding to the above form is as follows.


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Send SMS</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Send an SMS</h2>
  <form action="sendsms.php" method="POST">
    <div class="form-group">
      <label for="email">Send SMS to (Phone number)</label>
      <input type="tel" class="form-control"  placeholder="Enter the phone number" name="phonenumber">
    </div>
    <div class="form-group">
      <label for="pwd">Send SMS to (Name)</label>
      <input type="text" class="form-control"  placeholder="Enter the name" name="receiverrname">
    </div>
    <div class="form-group">
      <label>
         Message to send
      </label>
      <textarea  class="form-control" placeholder="your message" name="receivermsg"></textarea>
    </div>
    <button type="submit" class="btn btn-primary form-control">Submit</button>
  </form>
</div>

</body>
</html>

PHP-sendms.php action file code example

In addition to the HTML code, we will need an action file (PHP) that will receive a variable and send it to the SMS recipient. The action PHP might look like the code below:


<?php
function removespecials($data) {
   $data =  strip_tags($data);
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
$receiverNumber=removespecials($_POST['phonenumber']);
$receiverName=removespecials($_POST['receiverrname']);
$receivermsg=removespecials($_POST['receivermsg']);
$message="Hello ".$receiverName." \n  ".$receivermsg;
// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    $receiverNumber,
    [
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+15017250604',
        // the body of the text message you'd like to send
        'body' => $message
    ]
);
?>

Conclusion

The process of sending SMS from your website is fairly straightforward with Twilio. You can use different programming languages. Also, if you have any skills in developing plugins or extensions, it might be a good idea to convert it to a CMS extension. If you have any questions, please contact us; we will be happy to help you.

Leave a comment



Thank you

Thank you for contacting Prositeweb,
Go back to the home page

Please fill free to contact us at any time for any request regarding our services. Contact us.

Thank you