0

I hope you are doing well. I am new to PHP and trying to develop a contact form. I have tried many time but I am not getting email from the server side. Everything seems to be OK but I don't know why it is not working. I have attached both my HTML and PHP scripts below. Please correct me where I am wrong.

HTML Form Script.

 <form name="sentMessage" id="contactForm" novalidate="novalidate" method="post" action="contact.php">
                            <div class="control-group">
                                <input type="text" class="form-control" id="name" placeholder="Your Name" required="required" data-validation-required-message="Please enter your name" name="Name"/>
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="control-group">
                                <input type="email" class="form-control" id="email" placeholder="Your Email" required="required" data-validation-required-message="Please enter your email" name="Email" />
                                <p class="help-block text-danger"></p>
                            </div>
                            
                            <div class="control-group">
                                <textarea class="form-control" rows="6" id="message" placeholder="Message" required="required" data-validation-required-message="Please enter your message" name="Message"></textarea>
                                <p class="help-block text-danger"></p>
                            </div>
                            <div>
                                <button class="btn btn-primary py-2 px-4" type="submit" name="Email" value="submit" id="sendMessageButton">Send Message</button>
                            </div>
                        </form>

PHP Code

<?php
if (isset($_POST['Email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "muhammadsulaiman7870@gmail.com";
    $email_subject = "New form submissions";

    function problem($error)
    {
        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['Message'])
    ) {
        problem('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $name = $_POST['Name']; // required
    $email = $_POST['Email']; // required
    $message = $_POST['Message']; // required

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

    if (!preg_match($email_exp, $email)) {
        $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 Name you entered does not appear to be valid.<br>';
    }

    if (strlen($message) < 2) {
        $error_message .= 'The Message you entered do not appear to be valid.<br>';
    }

    if (strlen($error_message) > 0) {
        problem($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 .= "Message: " . clean_string($message) . "\n";

    // create email headers
    $headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
?>

    <!-- include your success message below -->

    Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>
MD Sulaiman
  • 185
  • 1
  • 15
  • 1
    Please check everything in the accepted answer here: [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail). Unfortunately, email isn't simple (and while I'm here, it's worth remembering that _sending_ an email in no way guarantees that anyone will _receive_ it.) – ADyson Sep 08 '21 at 09:57
  • 1
    P.S. Just as a specific observation, allowing the user to input the email address to be used as the "From" field is a great way to get your messages marked as spam / spoofing attempts, because their email address domain is unlikely to match the domain of the server you're sending the message from. Therefore it looks like a forgery attempt (which is basically is). Instead, use a generic "donotreply@yourdomain.com" address as the "From", and if you need to show the user's email address, you can put it in the message body and/or in the reply-to field of the email header. – ADyson Sep 08 '21 at 10:02

0 Answers0