简体   繁体   中英

Contact form with Email & SMS in PHP

I am just a beginner in PHP,

I have created a landing page with simple contact form with - Name, Email & Phone fields, the PHP code is

<?php
$name = $_POST['contact-form-name'];
$email = $_POST['contact-form-email'];
$message = $_POST['contact-form-message'];

$to = 'name@example.com';
$subject = 'Subject';

$body = "";
$body .= "Name: ";
$body .= $name;
$body .= "\n\n";

$body .= "";
$body .= "Message: ";
$body .= $message;
$body .= "\n";

$headers = 'From: ' .$email . "\r\n";

//$headers = 'From: noreply@domain.com' . "\r\n";

//$body .= "";
//$body .= "Email: ";
//$body .= $email;
//$body .= "\n";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
mail($to, $subject, $body, $headers);
echo '<span id="valid">Your Email was sent!</span>';
}else{
echo '<span id="invalid">Your message cannot be sent.</span>';
}

All we want now is get SMS as well as Email from the same contact form,

We are using Textlocal as our SMS gateway & there API allows as to send SMS using PHP, the send SMS code in PHP POST is

    <?php
    // Textlocal account details
    $username = 'youremail@address.com';
    $hash = 'Your API hash';

    // Message details
    $numbers = array(918123456789, 918987654321);
    $sender = urlencode('TXTLCL');
    $message = rawurlencode('This is your message');

    $numbers = implode(',', $numbers);

    // Prepare data for POST request
    $data = array('username' => $username, 'hash' => $hash, 'numbers' => $numbers, "sender" => $sender, "message" => $message);

    // Send the POST request with cURL
    $ch = curl_init('http://api.textlocal.in/send/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    // Process your response here
    echo $response;
?>

Need help in merging the both PHP's into one or making the both work,

So that we will get both Email & SMS

*Note the contact form validation is done custom.js

Thanks

Ashok

Assuming that both pieces of original code work and do what they are supposed to do you could nest the code like the following and send the sms based upon the success of sending the email.

<?php

    $status=array();

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['contact-form-name'], $_POST['contact-form-email'], $_POST['contact-form-message'] ) ){

        $name = !empty( $_POST['contact-form-name'] ) ? $_POST['contact-form-name'] : false;
        $email = !empty( $_POST['contact-form-email'] ) ? filter_input('contact-form-email', FILTER_VALIDATE_EMAIL ) : false;
        $message = !empty( $_POST['contact-form-message'] ) ? $_POST['contact-form-message'] : false;


        if( !$name || !$email || !$message ){
            $status[]='one or more required fields are empty';
        }

        if( empty( $status ) ){
            /* email elements */
            $to = 'name@example.com';
            $subject = 'Subject';
            $body = "\n
                Name: {$name}\n\n
                Message: {$message}\n";
            $headers = "From: {$email}\r\n";

            /* send the email - assign result as a variable */
            $result=@mail( $to, $subject, $body, $headers );

            /* add a status based on mail sending */
            $status[]=$result ? 'Mail sent' : 'Mail could not be sent';




            if( $result ){

                $username = 'youremail@address.com';
                $hash = 'Your API hash';

                $numbers = array( 918123456789, 918987654321 );
                $sender = urlencode( 'TXTLCL' );
                $message = rawurlencode( $message );
                $numbers = implode( ',', $numbers );


                $data = array(
                    'username'  => $username,
                    'hash'      => $hash,
                    'numbers'   => $numbers,
                    'sender'    => $sender,
                    'message'   => $message
                );


                $ch = curl_init( 'http://api.textlocal.in/send/');
                curl_setopt( $ch, CURLOPT_POST, true );
                curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
                $response = curl_exec( $ch );
                curl_close( $ch );

                $status[]=$response;
            }
        }

    } else {
        $status[]='Invalid method';
    }

    /* process the various status messages stored in the array */
    echo "<h2>Status</h2><ul><li>", implode( "</li><li>", $status ), "</li></ul>";

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM