Tuesday, 28 August 2018

The PHP email form does not work when some text is entered

I'm working on a PHP mail form and for some reason only certain text doesn't send through. It has me baffled and I can't even think of how to Google an answer.

For example, if I enter for the form:
 Name: Indiana Jones
 Number: 000
 Email: indiana@jones.com

The mail function won't work, however if I change the 000 to 999, it works. OR if I change the indiana@jones.com to indiana@jones.net, it suddenly works too.
Here are snippers, from my HTML:
<form method="post" action="mail.php">

Name<br />
<input style="width:878px;" type="text" name="name" id="name" required>
Best Contact Number<br />
<input style="width:415px;" type="text" name="number" id="number" required>
Email<br />
<input style="width:415px;" type="email" name="email" id="email" required>

</form>

And from my PHP (mail.php): (personal email addresses have been removed)
<?php
ini_set("SMTP","mail.email.com.au");
ini_set('sendmail_from', 'email@email.com.au'); 

$to = "email@email.com.au";
$subject = "Mail Form";

$message = "Name:" . "\n" . $_POST['name'] . "\n\n";
$message .= "Best Contact Number:" . "\n" . $_POST['number'] . "\n\n";
$message .= "Email:" . "\n" . $_POST['email'] . "\n\n";
$headers = "From: Removed <email@email.com.au> \n";

if(@mail($to, $subject, $message, $headers))
{
echo "<script type='text/javascript'>alert('Thank you for your submission'); window.location.href = 'index.html';</script>";}
else
{
    echo "<script type='text/javascript'>alert('There was an error - please check all fields and try again'); window.location.href = 'index.html';</script>";
}
?>


well, I just got a code that will make it work great .
    <?php

    if(isset($_POST['email'])) {

        // EDIT THE 2 LINES BELOW AS REQUIRED

        $email_to = "email@email.com.au";

        $email_subject = "Mail Form";
        function died($error) {

            // your error code can go here

            echo "<script type='text/javascript'>alert('There was an error because '); window.location.href = 'index.html';</script>";
            die();
        }
        // validation expected data exists

        if(!isset($_POST['name']) ||

            !isset($_POST['number']) ||

            !isset($_POST['email'])) {

            died('<script type='text/javascript'>alert('There was an error - please check all fields and try again'); window.location.href = 'index.html';</script>');       

        }
        $name = $_POST['name']; // required

        $email = $_POST['email']; // required

        $number = $_POST['number']; // required

        $error_message = "";

        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

      if(!preg_match($email_exp,$email_from)) {

        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

      }

        $string_exp = "/^[A-Za-z .'-]+$/";

      if(!preg_match($string_exp,$first_name)) {

        $error_message .= 'The Name you entered does not appear to be valid.<br />';

      }

      if(strlen($comments) < 0) {

        $error_message .= 'The Number you entered do not appear to be valid.<br />';

      }

      if(strlen($error_message) > 0) {

        died($error_message);

      }

        $email_message = "Form details below.\n\n";

        function clean_string($string) {

          $bad = array("content-type","bcc:","to:","cc:","href");

          return str_replace($bad,"",$string);

        }
        $email_message .= "Name: ".clean_string($name)."\n";

        $email_message .= "Email: ".clean_string($email)."\n";

        $email_message .= "Number: ".clean_string($number)."\n";

    // create email headers

    $headers = 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .

    'X-Mailer: PHP/' . phpversion();

    @mail($email_to, $email_subject, $email_message, $headers);  

    ?>
<!-- if sent successfuly -->
    <script type='text/javascript'>alert('Thank you for your submission'); window.location.href = 'index.html';</script>

    <?php

    }

    ?>

You can add this code to display the errors for users
echo $error."<br /><br />";

0 comments:

Post a Comment