How to send SMS from your website with Twilio

Would you like to send SMS to your customers from your website? This little tutorial will show you how to achieve this. In just three steps, we'll see how to create a Twilio account, add an SDK to a project, and create a form to send SMS.

How to send SMS from your website with 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 are getting more and more complicated. If you have a website and are planning to try SMS marketing as an additional way to target customers, this article is for you.

In this article, we will see how you can send SMS from your website with Twilio. At the end of the article, you will be able to 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

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.

To send SMS from your site, without these skills, you can still follow the tutorial by doing copy and paste. Please do not hesitate to contact us if you have any questions.

Step 1 – Create a Twilio account and get the settings

To get started, you will need to create a Twilio account and get some settings like: API sid, API passcode, and phone number. For example, in your dashboard, you can create API credentials for your project. 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 which gives you the option to try for free. The next step will be to enter the email address and 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 d'enregistrement 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 authentication code under the Dashboard tab -> General (the link is console / project / settings). You can then copy these two pieces of information for your project.

Authentication et SID

Get your Twilio account number

To get your Twilio phone number, click on the menu three-dot 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.

Numéro de téléphone

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. We generally recommend the Composer, as it downloads with all the updates. A manual update may be out of date or lack dependency. We will see how to use both approaches:

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

You will find more information on the use of the composer on the site getcomposer.org . There you will find detailed instructions and documentation on how to use the composer in 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 how to create composer.json file, you can refer to 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 folder called vendor in your main directory with some PHP files for your projects.

Include the Twilio SDK in your project

If you have followed the above steps carefully, you can now include the SDK and start texting 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) and 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 to whom you want to send an SMS).
  • 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 having to do it manually. For example, with the code above, you can change the message, 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.

Read also …

Envoyer des SMS depuis votre site

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) which will receive a variable and send it to the recipient of the SMS. 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, feel free to contact us contact ; we will be happy to help you.

Leave a Reply

Do you want a quality website, ecommerce or tools?

Please leave us a message, and an expert will contact you within the next few hours for a free 15 min consultation

Free Estimate