HTML Form Email submission

I am struggling to get my html form to email me on submission. This is my current code:

<form action="email.php" method="POST">
    <label for="name">Name:</label><br>
    <input type="text" id="name" name="name" required><br>

    <label for="email">Email:</label><br>
    <input type="email" id="email" name="email" required><br>

    <label for="subject">Subject:</label><br>
    <input type="text" id="subject" name="subject" required><br>

    <label for="message">Message:</label><br>
    <textarea id="message" name="message" rows="4" required></textarea><br>

    <input type="submit" value="Submit">
</form>

Then the php file

$errors="";
$myemail="[email protected]";//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['subject']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$subject = $_POST['subject']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors)) {

    $to = $myemail;
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
                " Here are the details:\n Name: $name \n ".
                "Email: $email_address\n Message \n $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";
    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact.html');
}

I am viewing my website on a local host using node.js, which may be part of the issue. Do I need a real online server?

This website is telling me I need more description so now I am typing this.

  • This might help you stackoverflow.com/a/70794053/909973

    – 

  • 1

    Does this answer your question? PHP mail function doesn’t complete sending of e-mail

    – 

  • 1

    NodeJS can’t run php code. Either use a server that can run php, or write your code using nodeJS instead of php

    – 

Leave a Comment