How to send message using php

In this article, you will learn how to send SMS to a mobile phone from a website using the PHP programming language.

Text messaging has become incredibly inescapable all over the world. It reduces costs, provides efficient service delivery, enhances marketing promotions, and improves staff communication. It can connect faster with short messages containing crucial bits of information, rather than long conversations. Since mobile phones are almost always within our arm's reach, SMS is landing straight into the customer's hands. SMS is very personal because it's delivered directly to your customer's mobile phone.

This application allows us to compose a message. In order to send an SMS from a website to a mobile, we need a third party API. The SMS gateway allows a computer system to send or receive SMS to or from a telecommunications network. There are lots of free or commercial SMS gateways available.

To fulfil this requirement, we used to have an HTML form that allowed the user to enter the required SMS details, i.e., phone number and message text. On submitting the form, the data is posted to a PHP script, where we have sent the SMS text through a gateway.

index.php

Suppose we have the following interface to get the phone number of the recipient and the message text from the website visitors. When the user clicks on the 'Send Message' button, the data will be posted to the 'phpsendsms.php' page-

<html>
	<head>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
	</head>
	<body>
	<div class="container">
	<h2>PHP Send SMS</h2>
		<form method="post" action='phpsendsms.php'>
		<div class="form-group">
			<label for="phoneno">Mobile Number</label>
			<input type="text" name="phoneno" class="form-control" placeholder="Enter Phone Number" >
		</div>
		<div class="form-group">
			<label for="exampleFormControlTextarea3">Enter Text Message</label>
			<textarea class="form-control" name="smstext" rows="7"></textarea>
		</div>
		<div class="form-group">
			<input type="submit" name="submit" class="btn btn-primary" value="Send Message">
		</div>	
		</form>
	</div>	
	</body>
</html>

How to send message using php

phpsendsms.php

This file is responsible for sending SMS. It collects the form data and configures the authentication key and sender id. The API gateway URL is used to pass the SMS to the gateway. So, there are three entities you basically need from your configuration gateway - Authentication Key, Sender ID and Gateway URL. The curl_setopt_array() method is used here to transfer multiple options to a CURL.

Make sure to replace the YOUR_AUTH_KEY, YOUR_SENDER_ID and YOUR_GATEWAY_URL.

<?php

// Authentication key
$authKey = "YOUR_AUTH_KEY";

// Also add muliple mobile numbers, separated by comma
$phoneNumber = $_POST['phoneno'];

// route4 sender id should be 6 characters long.
$senderId = "YOUR_SENDER_ID";

// Your message to send
$message = urlencode($_POST['smstext']);

// POST parameters
$fields = array(
    "sender_id" => $senderId,
    "message" => $message,
    "language" => "english",
    "route" => "p",
    "numbers" => $phoneNumber,
);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "YOUR_GATEWAY_URL",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode($fields),
  CURLOPT_HTTPHEADER => array(
    "authorization: ".$authKey,
    "accept: */*",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>

The above code returns TRUE with message id if all options were successfully set. If an option could not be successfully set, FALSE is immediately returned.

PHP reverse a string without predefined function
PHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
PHP String Contains
PHP remove last character from string
Electricity bill program in PHP
PHP import Excel data to MySQL using PHPExcel
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP

How create SMS API in PHP?

How to integrate Messente's SMS API in PHP.
Step 1: Create your Messente account. You'll need to signup to Messente to get started (no credit card details required). ... .
Step 2: Create a sender ID. Next, apply for a sender ID. ... .
Step 3: Create an API key..
Step 4: Install our PHP code library..

How can I send SMS from my website?

These texting websites will let you send SMS messages from your computer for free..
TextEm. TextEm lets you send free text messages, but you first need to create an account. ... .
SendSMSnow. ... .
OpenTextingOnline. ... .
txtDrop. ... .
Send Anonymous SMS. ... .
TxtEmNow. ... .
Globfone. ... .
Textport..

How do I send an HTML text?

To Send Text Message It Takes Only Four Steps:-.
Create an account on mvaayoo.com and get api to send message. Create a account on mvaayoo.com and get sms api and user id to send text message. ... .
Make a HTML file and define markup. ... .
Make a PHP file to send text message. ... .
Make a CSS file and define styling..

What is SMS code?

An SMS short code is a five or six-digit number that you can use to send and receive SMS messages to and from mobile phones. There are two types of SMS short codes: a vanity number and a random number. A vanity short code is a specific number that you choose, while a carrier assigns a random short code to you.