简体   繁体   中英

HTML - PHP contact form email

I have searched here and in google how to make a contact form to work and I havent been able.

I downloaded a "php contact form" but I dont know how to make it work. I know there have been a lot asked question about this but please help me out. I am stuck here :/

This is the HTML code:

<form action="form-to-email.php" name="myemailform" method="post">
    <div>
        <span>Name</span>
        <input type="text" name="name" value="" placeholder="Your Name">
    </div>

    <div>
        <span>Email</span>
        <input type="email" name="email" autocapitalize="off" autocorrect="off" value="" placeholder="example@domain.com">
    </div>

    <div><textarea name="message" placeholder="Message"></textarea></div>

    <div class="code">
        <button><input type="submit" name='submit' value="Send"></button>
    </div>
    <i class="clear" style="display: block"></i>
    </div>
</form>

And here is the "free PHP form code" I downloaded from a website:

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'myEmail@domain.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".

$to = "myEmail@domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

Do I need to do something in mysql or do something more at all? Thanks for the help!

everything looks correct, just add exit after redirect.

header('Location: thank-you.html');
exit;

Here "Here is the message:\\n $message". <= see the dot? That's supposed to be a semi-colon.

That, is the reason why OP's code is not working.

Add error reporting to the top of your file(s) which will help find errors, which it would have.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM