Jump to content

Send SMS with contact form

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Oakley21

Oakley21

    Newbie

  • Members
  • Pip
  • 1 posts
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;

Quote

<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 the SourceSMS gateway, 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
$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",

#2
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
See this is a good tutorial, but it's almost identical to the one on the SourceSMS developer page. Also, you should point out that people actually need to pay for this service, and that it cant be done for free.

#3
kimmy123

kimmy123

    Newbie

  • Members
  • Pip
  • 3 posts
thanks for share these code its a amazing.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users