Monday 27 May 2013
Facebook StumbleUpon Twitter Google+ Pin It

Sending Text Messages in PHP

Recently I’ve had to implement an alert system in PHP were the alerts are sent via a text message. The concept is very simple – use an email to SMS gateway. Essentially, you send an email to a phone companies gateway server, that server then relays the email to the phone number specified via a SMS. Since PHP has the ability to send emails, the whole process is quite easy.
1. First you need to acquire a list of SMS gateways you are going to support. For an extensive list, check out Wikipedias List of SMS Gateways. For this tutorial, I will only be using AT&T Wireless (number@txt.att.net) and Verison (number@vtext.com).
2. Create a simple HTML form.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"></meta>
    <title>SMS</title>
</head>
<body>
    <form action="POST">
     
        <label for="provider">Provider: </label>
        <select>
            <option value="txt.att.net">AT&T Wireless</option>
            <option value="vtext.com">Verizon</option>
        </select><br />
         
        <label for="number">Number: </label>
        <input type="number" /><br />
        <label for="subject">Subject: </label>
        <input type="subject" /><br />
         
        <label for="message">Message: </label>
        <input type="message" /><br />
         
        <input type="submit" />
    </form>
</body>
</html>
3. Add the necessary PHP code to send the email
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"></meta>
    <title>SMS</title>
</head>
<body>
    <?php
        if(!empty($_POST['number'])) {
            mail($_POST['number'] . $_POST['provider'], $_POST['subject'], $_POST['message']);
        }
    ?>
    <form action="POST">
     
        <label for="provider">Provider: </label>
        <select>
            <option value="@txt.att.net">AT&T Wireless</option>
            <option value="@vtext.com">Verizon</option>
        </select><br />
         
        <label for="number">Number: </label>
        <input type="number" /><br />
        <label for="subject">Subject: </label>
        <input type="subject" /><br />
         
        <label for="message">Message: </label>
        <input type="message" /><br />
         
        <input type="submit" />
         
    </form>
</body>
</html>
Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

No comments: