0

I have a problem with php function mail(). It doesn't send any emails :(. I'm totally new to php and created the code by using a lot of tutorials, so I think it might just be wrong.

Also I'm having problem with validation below the button code. I want to validate if the user entered the valid email and if not, echo nothing, because it will show red error anyway.

Right now when email is invalid it shows "Email sent" message But first of all I want to email actually send.

Also hosting is blocking the SSL protocol if there's no header "From::". Is it placed correctly?

<h4>Contact form</h4>
<?php
$NameErr = $EmailErr = $SubErr = $MessErr = "";
$Name = $Email = $Subject = $Message = "";
$To = "blackmagic@xxx.com";
$headers = "From: blackmagic@xxx.com Name: $Name\n $Message\n E-Mail: $Email\n";
$n = NULL;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["Name"])) {
        $NameErr = "Name is required";
    } else {
        $Name = test_input($_POST["Name"]);
    }
    if (!preg_match("/^[a-zA-Z ]*$/", $Name)) {
        $NameErr = "Only letters and white space allowed!";
    }
    if (empty($_POST["Email"])) {
        $EmailErr = "Email is required";
    } else {
        $Email = test_input($_POST["Email"]);
        if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
            $EmailErr = "Invalid email format";
        }
    }
    if (empty($_POST["Subject"])) {
        $SubErr = "Subject is required";
    } else {
        $Message = test_input($_POST["Subject"]);
    }
    if (empty($_POST["Message"])) {
        $MessErr = "Message is required";
    } else {
        $Message = test_input($_POST["Message"]);
    }
}

$Name = test_input($_POST["Name"]);
$Email = test_input($_POST["Email"]);
$Subject = test_input($_POST["Subject"]);
$Message = test_input($_POST["Message"]);

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"  method="post">
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Name" name="Name"></p>
    <span class="error"><?php echo $NameErr; ?></span>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Email" name="Email"></p>
    <span class="error"><?php echo $EmailErr; ?></span>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Subject" name="Subject"></p>
    <span class="error"><?php echo $SubErr; ?></span>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="Message" name="Message"></p>
    <span class="error"><?php echo $MessErr; ?></span>
    <p>
        <button class="w3-btn w3-grey w3-padding-large w3-hover-green" type="submit" value="1" name="pressed">
            <i class="fa fa-paper-plane"></i> SEND MESSAGE
        </button>
    </p>
</form>
<?php
if (isset($_POST["pressed"])) {

    mail($To, $Subject, $Message, $headers);

    if (empty($_POST["Name" or "Email" or "Subject" or "Message"])) {
        echo $n;
    }
    if (isset($Name) and ( $Email) and ( $Subject) and ( $Message)) {
        echo "Email sent.";
    }
}
?> 
</div>

1 Answers1

0

As i understood you have problem sending mail and Hosting is blocking the SSL protocol if there's no header "From::". Is it placed correctly?

i have solution for your mail not sending and user mail validation problem. Here is the code which i'm using regularly for sending mail and it does Work Perfectly Fine.

  <?php

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

        //EDIT THE 2 LINES BELOW AS REQUIRED

        $email_to = "info.winsoftsolution@gmail.com";

        $email_subject = "Contact Message Received";


        function died($error) {

             //your error code can go here

            echo "We are very sorry, but there were error(s) found with the form you submitted. ";

            echo "These errors appear below.<br /><br />";

            echo $error."<br /><br />";

            echo "Please go back and fix these errors.<br /><br />";

            die();

        }

        // validation expected data exists

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

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

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

            died('We are sorry, but there appears to be a problem with the form you submitted.');       

        }

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

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

         $telephone = $_POST['phone']; //  required

         $comments = $_POST['comments']; // 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,$name)) {

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

      }



      if(strlen($comments) < 2) {

        $error_message .= 'The Comments 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_from)."\n";

        $email_message .= "Telephone: ".clean_string($telephone)."\n";

        $email_message .= "Comments: ".clean_string($comments)."\n";

    // create email headers

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

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

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

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

        if(@mail){
    ?>
        <!-- include your own success html here -->
        <center class="container text-center"><br>
            <div class="alert alert-success">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <p><strong><?php echo "Thank you for contacting us. We will be in touch with you very soon.";?></strong></p>
            </div>
        </center>

        <center class="container text-center"><br>
            <div class="alert alert-info">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <p><a href="index.html"><strong>Click Here</strong></a> to Return.</p>
            </div>
        </center>    

    <?php
        }
        else
        {
    ?>
            <center class="container text-center"><br>
                <div class="alert alert-danger">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <p><strong><?php echo "Erorr Encountered While Sending mail Try again..";?></strong></p>
                </div>
            </center>
    <?php       
        }

    }

    ?>
Pavan Baddi
  • 479
  • 1
  • 11
  • 22
  • Thanks, if I wont find any solution I'll try with your code. Although I'd like to fix what I created and learn something from it :) – BlackMagic Jan 17 '17 at 12:36