0

I am making a website but and it is complete, just I have to add the PHP form for my Contact Form. As I do not know PHP, I have copied one code of PHP for my contact form and everything is working fine but it is not sending mails.

I have also tried to send submit from my other emails but the PHP form is not sending mails.

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "jhulonporel9@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);
?>

<?php
}
?>
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 5
    `"Hello there, Please help me"` is not a **Title that summarizes the specific problem** please read: https://stackoverflow.com/help/how-to-ask as – caramba Jun 20 '21 at 08:26
  • Use php Miler `https://www.youtube.com/watch?v=CclhcZm6TZc`. By the way, never ask for help, define the problem .... – Mantykora 7 Jun 20 '21 at 08:32
  • 1
    Does this answer your question? [How can I send an email using PHP?](https://stackoverflow.com/questions/5335273/how-can-i-send-an-email-using-php). If you remove the `@` (a bad habit) you’ll at least see why it fails to send btw. – AD7six Jun 20 '21 at 08:35

0 Answers0