What we are going to be covering in this tutorial

This tutorial will show you how you can set up a PHP contact form on your web site which will send an SMS message, in this case we will make the contact form send you the information passed by the form through an SMS message.

What you will need

Web server (Linux/Windows) with PHP and cURL enabled.
Text editor (For example Notepad++)
Gateway API (We are going to be using SourceSMS)

Lets get started

First of we need to create our visual form which we shall name contact.htm.

Within this file include the following;

<font size="5" face="arial" color="black">
Contact Form

<form name="contactform" method="post" action="send.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
  <label for="first_name">First Name *</label>
</td>
<td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>

<tr>
<td valign="top"">
  <label for="last_name">Last Name *</label>
</td>
<td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
  <label for="email">Email Address *</label>
</td>
<td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
</td>

</tr>
<tr>
<td valign="top">
  <label for="telephone">Telephone Number</label>
</td>
<td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
  <label for="message">Message *</label>
</td>
<td valign="top">
  <textarea  name="message" maxlength="640" cols="25" rows="6"></textarea>
</td>

</tr>
<tr>
<td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">  
</td>
</tr>
</table>
</form>
</font>

This basically creates the form which sends the POST information to send.php, so lets go ahead and create the send.php file;

For this we need to add the SMS gateway API, as we are going to be using you will need to sign up at SourceSMS.

Once you have got your API account information we need to add their developer API into our code which is;

<?php

  $message = $_POST['message'];
  $first_name = $_POST['first_name'];
  $last_name = $_POST['last_name'];
  $email = $_POST['email'];
  $telephone = $_POST['telephone'];

$message = "$first_name $last_name $email $message";

// Data for text message. This is the text message data.
$from = "FROM"; // This is who the message appears to be from.
$to = "777000000"; //A single number or a comma-seperated list of numbers
// $message = "This is a test message from the PHP API script"; //160 chars or less
$username = "Your SourceSMS username"; // insert your username
$pword = "Your SourceSMS password"; //Your developer API password
$hash = "RjK=H4kL"; //Do not change
$formCountry = "44"; //Change this to the appropriate country code (default UK)
$sourceinfo = "1"; //Display POST info

//extract data from the post
//extract($_POST);

//set POST variables
$url = 'http://sourcesms.com/api/api-function.php';
$fields = array(
            'from'=>urlencode($from),
            'to'=>urlencode($to),
            'message'=>urlencode($message),
            'username'=>urlencode($username),
            'pword'=>urlencode($pword),
            'hash'=>urlencode($hash),
            'formCountry'=>urlencode($formCountry),
            'sourceinfo'=>urlencode($sourceinfo)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

You will need to insert your own mobile/cell phone number for the information to be sent to you, ensure you set the "$formCountry" as the international code for your country, the default is for the UK which is "44",

Recommended Answers

All 9 Replies

Very nice Tutorial. please mark it as solved.

Could you please help with a simpler "send SMS" for my objective?

.... sending SMS from (Android) mobile using <a href.....

My objective is to send an SMS "receipt" from an Agent's mobile (viewing our web page) instantly to the "payer" to acknowledge receipt of a (small) cash payment .... using the bundled SMS package wih the Agent's phone contarct ... not an external SMS service.

I'm hoping to do this from our website (which the Agent will be viewing at the time of transaction) rather than a dedicated App, if possible.

I have searched everywhere for a simple solution to this and successfully used the following as a prelimanry test

<p><a href="sms:7777777777777?body=SMARTiPAY. £5 received, thank you. ">Send SMS welcome message to new member ></a></p>

.. but what I actually need now is a means of replacing both the "number" and "content" with variables generated from a form on the page .... input fields "mobilenum" and "message" (or other variables if spec needs to be different?)

This is the first time I have used a forum, so I hope I'm not abusing any etiquette or people's kindness.

Thank you

commented: Hi +0

Does it really send the form Data to a number via SMS?

Wow awesome tutorial it's works.

Sourcesms.com is no more. This tutorial is thus outdated...

Hi
I have followed your tutorial but I am getting this error message after clicking on the submit button: 403 Forbidden nginx

my API password : removed

Any idea on what it means?

Thanks
Rahman

Gi sir

Hello, nice tutorial .I used this form but not send sms . Could you please help me solve this problem.

What exactly is your problem?

As stated in a reply, SourceSMS is no longer available.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.