Home > Back-end >  Script for sending mail from contact form not working
Script for sending mail from contact form not working

Time:11-23

I am trying to create a simple contact form with a PHP script to send an email. But the following script, located in the same folder as the index.html, is not working. It leads me to an 'This page is not working' page. On this URL: ipaddress/contact-form.php. Could be I am misplacing files, but do not seem to grasp what must be done.

The HTML

<form method="POST" action="contact-form.php" class="contact-form">
            <label for="name" class="form-label">Volledige naam*</label>
            <input
              type="text"
              name="name"
              id="Name"
              placeholder="Your full name"
              required
            />
            <label for="email" class="form-label">Email*</label>
            <input
              type="email"
              name="email"
              id="Email"
              placeholder="[email protected]"
              required
            />
            <label for="phone" class="form-label">Telefoonnummer</label>
            <input
              type="tel"
              name="phone"
              id="Phone"
              placeholder="06-12345667"
            />
            <label for="message" class="form-label">Bericht*</label>
            <textarea
              type="text"
              name="message"
              id="Message"
              placeholder="Uw bericht.."
              required
            ></textarea>
            <small class="required">* verplicht</small>
            <button type="submit" class="submit">Verstuur</button>
          </form>
<?php
if (isset($_POST['Email'])) {

    $email_to = "[email protected]";
    $email_subject = "Een nieuw contactbericht";

    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();
    }

    if (
        !isset($_POST['Name']) ||
        !isset($_POST['Email']) ||
        !isset($_POST['Phone']) ||
        !isset($_POST['Message'])
    ) {
        problem('We are sorry, but there appears to be a problem with the form you submitted.');
    }

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

    $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 .= "Phone: " . clean_string($phone) . "\n";
    $email_message .= "Message: " . clean_string($message) . "\n";


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

    Bedankt voor uw bericht! We zullen zo spoedig mogelijk contact met u opnemen.

<?php
}
?>

Any thoughts? :)

CodePudding user response:

When browsers submit forms, they encode the data as a series of name=value pairs, not id=value pairs.

You're using the wrong keys (e.g. $_POST['Email'] should be $_POST['email'] because you have name="email")


The server responded with a status of 405

405 means Method Not Allowed.

This means that your server (whatever server that might be) is configured not to accept POST requests for that URL.

Maybe it is a server that doesn't support PHP at all.

Maybe you have the wrong URL.

Maybe the security settings for it are set to restrict request types.

CodePudding user response:

  1. Remember respect case sensitive in all $_POST => $_POST['Name'] etc.
  2. You forget declare var $phone before use => $email_message .= "Phone: " . clean_string($phone) . "\n"
  3. Error 405 looks like API respons. Are you working with an API or Framework?

I reproduce the code on my PC and works fine.

https://turendu.com.ar/Jorik/

If you have problem with php mail, maybe you can try with https://github.com/PHPMailer/PHPMailer

  • Related