fbpx
Send SMS from your website with this trick. (detailed guide) Send SMS from your website with this trick. (detailed guide)

Send SMS from your website with this trick. (detailed guide)

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

Sending SMS from your website with Twilio is relatively easy, it all depends on the programming language you use. Marketing by email and phone calls has lost its popularity. Indeed, a study has shown that the rate of opening emails has dropped significantly recently. If you own a website and are planning to try SMS marketing as an additional way to target customers, this article is for you. In this guide, we’ll show you how to send SMS from your website with Twilio. You will therefore be able to add the Twilio SDK to your project to integrate a form for sending SMS. You will also see how to get a Twilio number and an API for your project.

Who is this tutorial for?

Our article is intended for three types of profiles:

  • Developers — who are keen to learn the procedure (for that, follow the steps).
  • Business owners — who don’t have enough time and want a usable solution (Download code).
  • Ordinary people — who are curious and would like to know more.

Requirements for developers

To follow this tutorial and easily apply it to your project, you must first meet a few criteria. Indeed, you must:

  • Possess basic skills in PHP.
  • Understand how HTML tags work.
  • Have some web programming skills.

For a business owner

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

Please contact us if you have any questions.

Get a copy of the code for sending an SMS from your site. The code is ready to use and documented


The procedure for developers to integrate the sending of SMS

To be able to send SMS from your site, you must follow the steps below.

Step 1 – Create a Twilio account and get configuration settings

To start, you must create a Twilio account and acquire certain parameters such as the API SID, the API authentication code, and the telephone number. For example, in your dashboard, you can create API credentials for your project. When you register, you acquire a personalized phone number that you can then use to automate the sending of SMS.

How to create a Twilio account

To create a Twilio account, go to twilio.com and click on “register”. Next, a registration form that gives you the opportunity to try for free will appear. The next step will be to enter your email address, then a password. You will then receive a confirmation request by email. Then, you will only have to approve your account to access it. Initially, you will have free credit that you can use to send SMS from your site or to benefit from other Twilio services. Send SMS from your website with this trick. (guide)

Remarks

The procedure for creating an account on Twilio sometimes varies according to their general conditions. I advise you to follow and read the steps of the registration procedure.

Get API SID and Authentication Code

Log in to your account, and your dashboard (the console) will appear. You will then find your SID account as well as your authentication code by scrolling down the page. Send SMS from your website with this trick. (guide) Finally, copy these two pieces of information for your project.

Acquire your Twilio phone number

All new users can acquire a free trial phone number for Twilio. To do this, just click on the “get a phone number” button on the home dashboard. Send SMS from your website with this trick. (guide) Also, if you want to pay for a phone number, follow the “Phone numbers>Manage>Buy a number” menu. You can then view your phone numbers by going to the left menu and following “Phone numbers>Manage>Active numbers”. Note that the site is displayed in English. However, you can opt to read a guide in French by clicking on “Démarrez ici

Step 2 – Twilio’s PHP Classes

Thanks to the PHP classes or the SDK, you can easily interact with the APIs. For downloading, you have the choice between doing it manually or using the Composer. In this case, we usually recommend the Compositor. Indeed, the latter is downloaded with all the updates. Furthermore, doing a manual update brings the risk of running into an outdated or missing dependency update. However, we will see how to use both approaches:

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

More information about using the Composer can be found at getcomposer.org. You will therefore find instructions as well as 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. Here are the general steps to follow:

  • On the getcomposer.org website, click the “Getting Started” button. You will see the documentation for creating the composer.phar file.
  • Via the command line, 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. See the related article on getcomposer.org for more details. Running the code above will generate the composer.phar file. Thus, all you have to do is generate the composer.json file

Generate the composer.json file to download the Twilio SDK

If you want to have further information on how to create the composer.json file, 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 run the statement

 composer require twilio/sdk

If you follow the instructions carefully, you will see some PHP files for your projects, as well as a new folder named vendor in your main directory.

Include the Twilio SDK in your project

If so far all the steps have gone smoothly, you can then integrate the SDK, and start sending SMS from your website. The code below will help you send an SMS to any recipient 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 redeeming code
  • Replace the SID (line 9) as well as the code (line 10) with the identifiers you obtained in Step 1.
  • Then replace the phone number on line 16 with the phone number of the SMS recipient.
  • Finally, on line 19, replace the phone number with your Twilio phone number.

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

Now having the code to send an SMS, you can opt for the creation of a form that will avoid you to make manual sending. For example, with the code above, you can modify the message, and the recipient’s phone number to send the SMS. However, if you want to automate the process, you need to create a form from your dashboard. Below is an example of a form you can use.

Example code HTML-index.html

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>

Sample PHP-sendms.php action file code

In addition to the HTML code, you will use an action file (PHP) which will receive a variable and send it to the recipient of the SMS. Your PHP action could be similar to 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 an SMS from your website is simple with Twilio. You can use it with various programming languages. Moreover, if you have skills in the development of plugins or extensions, Twilio offers the possibility of converting it into a CMS extension. If you require supplemental information, please contact us, it will be a pleasure to help you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Gilblas Ngunte Possi

Gilblas
Typically replies within an hour

Gilblas
Hi there👋

How can I help you?
1:40
Chat with Us