1

I have very limited knowledge. I tried my best to understand. I want to send mail from a contact form, upon success show an alert message and remain on the same page. the mail is not sending and seems likeI have some bugs in the code. kindly help.

<form action="" method="post">
    <div class="col-md-6">
        <input name="name" type="text" class="form-control" placeholder="Name">
    </div>
    <div class="col-md-6">
        <input name="email" type=“text” class="form-control" placeholder="Email">
    </div>
    <div class="col-md-12">
       <input name="subject" type="text" class="form-control" placeholder="Subject">
    </div>
    <div class="col-md-12">
        <textarea name="message" class="form-control" placeholder="Message" rows="4"></textarea>
    </div>
    <div class="col-md-8">
        <input type="submit" class="form-control text-uppercase" name="send" value="submit" />
    </div>
</form>

<?php

if ($_POST['send']) { 
    $ToEmail = 'info@autonomousdata.com'; 
    $EmailSubject = $_POST['subject']; 
    $mailheader = "From: ".$_POST['email']."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST['email']."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n" .
                                            "X-Mailer: PHP/" . phpversion(); 
    $MESSAGE_BODY = "Name: ".$_POST['name']."\n"; 
    $MESSAGE_BODY .= "Email: ".$_POST['email']."\n"; 
    $MESSAGE_BODY .= "Comment: ".$_POST['message'].""; 

    if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader)!==true) {
?>
    <script type='text/javascript'> alert('failed to send the mail'); </script>
<?php
        die('Fail to send');
    } else{
?>
    <script type='text/javascript'>alert('Thank you for contacting us! All information received will always remain confidential. We will contact you as soon as we review your message.');
    </script>

<?php       
    }           
}
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46

1 Answers1

0

The HTML form had dubious quotation marks around the type='text' and I modified the php to add some rudimentary checking of variables and also sanitisation ~ hope it will help.

<form action="" method="post">
    <div class="col-md-6">
        <input name="name" type="text" class="form-control" placeholder="Name">
    </div>
    <div class="col-md-6"><!-- // incorrect quotation used on `text` //-->
        <input name="email" type='text' class="form-control" placeholder="Email">
    </div>
    <div class="col-md-12">
       <input name="subject" type="text" class="form-control" placeholder="Subject">
    </div>
    <div class="col-md-12">
        <textarea name="message" class="form-control" placeholder="Message" rows="4"></textarea>
    </div>
    <div class="col-md-8">
        <input type="submit" class="form-control text-uppercase" name="send" value="submit" />
    </div>
</form>

<?php

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

        $to         = 'info@autonomousdata.com';
        $name       = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
        $subject    = filter_input( INPUT_POST, 'subject', FILTER_SANITIZE_STRING );
        $message    = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
        $email      = filter_var( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_STRING ), FILTER_VALIDATE_EMAIL );

        $headers=array();
        $headers[]="From: {$email}";
        $headers[]="Reply-To: {$email}";
        $headers[]="Content-type: text/html; charset=iso-8859-1";
        $headers[]="X-Mailer: PHP/" . phpversion();
        /* generate final headers string */
        $headers=implode( "\r\n", $headers );


        $message=array();
        $message[]="Name: {$name}";
        $message[]="Email: {$email}";
        $message[]="Comment: {$message}";
        /* generate final message string */
        $message=implode( "\n", $message );


        $result=@mail( $to, $subject, $message, $headers );

        switch( $result ){
            case true:
                $msg='Thank you for contacting us! All information received will always remain confidential. We will contact you as soon as we review your message.';
            break;
            case false:
                $msg='failed to send the mail';
            break;
        }

        echo "<script type='text/javascript'>alert('{$msg}');</script>";
    }
?>

The test page, in it's entirety, that I set up to test the basic functionality of the script. As this was all run locally I cannot confirm whether the email would be sent - it looks correct however.

<?php

?>
<!doctype html>
<html>
    <head>
        <title>Basic Contact Form - investigating email send failure.</title>
        <script type='text/javascript' charset='utf-8'></script>
        <style type='text/css' charset='utf-8'>
            form{
                width:50%;
            }
            form,output,input[type='text'],textarea,input[type='button'],input[type='submit']{
                display:block;
                box-sizing:border-box;
                padding:0.5rem;
                margin:0.5rem;
            }
            output{
                color:red;
            }
            input[type='text'],textarea{
                width:60%;
            }
        </style>
    </head>
    <body>


        <form action="" method="post">
            <div class="col-md-6">
                <input name="name" type="text" class="form-control" placeholder="Name">
            </div>
            <div class="col-md-6"><!-- // incorrect quotation used on `text` //-->
                <input name="email" type='text' class="form-control" placeholder="Email">
            </div>
            <div class="col-md-12">
               <input name="subject" type="text" class="form-control" placeholder="Subject">
            </div>
            <div class="col-md-12">
                <textarea name="message" class="form-control" placeholder="Message" rows="4"></textarea>
            </div>
            <div class="col-md-8">
                <input type="submit" class="form-control text-uppercase" name="send" value="submit" />
            </div>
            <output id='msgs'></output>
        </form>


        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['subject'],$_POST['email'],$_POST['name'],$_POST['message'] ) ){

                $to         = 'info@autonomousdata.com';
                $name       = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
                $subject    = filter_input( INPUT_POST, 'subject', FILTER_SANITIZE_STRING );
                $message    = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
                $email      = filter_var( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_STRING ), FILTER_VALIDATE_EMAIL );

                if( !$to or !$name or !$subject or !$message or !$email ){

                    echo "Error: one or more required variables are not available.";

                } else {

                    $headers=array();
                    $headers[]="From: {$email}";
                    $headers[]="Reply-To: {$email}";
                    $headers[]="Content-type: text/html; charset=iso-8859-1";
                    $headers[]="X-Mailer: PHP/" . phpversion();
                    /* generate final headers string */
                    $headers=implode( "\r\n", $headers );


                    $message=array();
                    $message[]="Name: {$name}";
                    $message[]="Email: {$email}";
                    $message[]="Comment: {$message}";
                    /* generate final message string */
                    $message=implode( "\n", $message );


                    $result=@mail( $to, $subject, $message, $headers );


                    switch( $result ){
                        case true:
                            $msg='Thank you for contacting us! All information received will always remain confidential. We will contact you as soon as we review your message.';
                        break;
                        case false:
                            $msg='Failed to send the mail';
                        break;
                    }



                    /* Let the user know the status of the form submission somehow */
                    echo "
                        <script type='text/javascript'>
                            document.getElementById('msgs').innerHTML='{$msg}';
                            /*
                                alert('{$msg}');
                            */
                        </script>";
                }
            }
        ?>
    </body>
</html>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • WoW, excellent example. sadly not working. the last line echo ""; seems to give some syntax error. removing that line fix it but still the message is not received at info@autonomousdata.com. really don't understand what is wrong. – Agile Nightmare Sep 10 '16 at 12:59
  • I just knocked up a test page but observed no errors - unable to confirm whether an email would have been sent as was testing on localhost only... – Professor Abronsius Sep 10 '16 at 13:33
  • I could send a screen shoot. is it possible to add? with the echo line, the browser shows this sting - alert('{$msg}');"; } ?> near to the contact form. This means some error right. Thoiugh, when I removed that line that string was not there any more. may be I have some other error somewhere? – Agile Nightmare Sep 10 '16 at 13:38