简体   繁体   中英

HTML form won't send email

I have a contact us form on my webpage, and im using php and jquery to send data from contact us form.

When i fill the data, i get alert "message sent", but no message gets delivered on my email.

Here is my HTML code

<!--CONTACT US FORM-->
<section id="contact">
    <div class="containercontact">
      <h2>Contact me</h2>
      <div class="title-bar"></div>
      <form action="" method="post" id="mycontactform">
        <div class="left">
          <input type="text" id="name" name="name" placeholder="Name" required="required"/>
          <input type="email" id="email" name="email" placeholder="Email" required="required"/>
          <input type="text" id="subject" name="subject" placeholder="Subject"/>
        </div>
        <div class="right">
          <textarea placeholder="Message" id="message" name="message" required="required"></textarea>
        </div>
        <div class="send-button cl">
          <button id="submit">Send Message</button>
        </div>
      </form>

    </div>
</section>

Here is my PHP code

<?php

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

$to = 'example@example.com';
$message = <<<EMAIL

$subject

$message

From: $name

Email: $email

EMAIL;
$headers = 'From:' . $email;

if (filter_var($email, FILTER_VALIDATE_EMAIL)) 
{
mail($to, $subject, $message, $headers);
echo "Thank you for your email! We will reply as soon as possible.";
}
else
{
echo "Invalid Email, please provide a correct email address.";
}

?>

And here is my Jquery code

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
    $.post("php/contactUs.php", 
    $("#mycontactform").serialize(),  
    function(response) {
        alert("message sent!");
    });
    return false;
});
});
</script>

I simply cannot get it to work. I run it on my wamp localhost and still nothing happens.

Any help will be appreciated!

you cant use email() function from local host, it wont send anything. you need a valid SMTP server. set up local SMTP or use a third party SMTP server.

Do you have a mail server on your host ?

if you dont the php mail function wont work .

you will have to use google's smtp server to send emails from your gmail

i have a project on github that i did if you want to just tell me

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