简单的联系表单不用PHP mail()函数发送邮件

时间:2016-07-04 17:44:11

标签: php html contact-form

HTML:

<form action="php/send-contact.php" class="contact-form" name="contact-form" method="post">
  <div class="row">
    <div class="col-sm-6 cols">
      <input type="text" name="name" required="required" placeholder="Name*">
    </div>
    <div class="col-sm-6 cols">
      <input type="email" name="email" required="required" placeholder="Email*">
    </div>
    <div class="col-sm-6 cols">
      <input type="text" name="subject" required="required" placeholder="Subject*">
    </div>
    <div class="col-sm-12 cols">
      <textarea name="message" required="required" cols="30" rows="5" placeholder="Message*"></textarea>
    </div>
    <div class="col-sm-12 cols">
      <input type="submit" name="submit" value="Send Message" class="btn btn-send">
    </div>
  </div>
</form>

php/send-contact.php

<?php
$name = @trim(stripslashes($_POST['name'])); 
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message'])); 
$email_from = $email;
$email_to = 'hello@domain.co.uk';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $body, 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message);
?>
<!DOCTYPE HTML>
<html lang="en-US">

  <head>
    <script>
      alert("Thank you for getting in touch. We will contact you as soon as possible.");

    </script>
    <meta HTTP-EQUIV="REFRESH" content="0; url=../index.html">
  </head>

HTML警报会按预期激活和刷新,但它不会发送任何电子邮件......

我尝试了很多电子邮件地址收件人。

此外,在将Google ReCaptcha添加到此表单时,是否有任何特殊措施(关于PHP元素)?

1 个答案:

答案 0 :(得分:1)

所以我测试了这个,我认为它正在做你想要的。

Search

另一个文件(html)保持不变。只需用{。}}替换$name = htmlentities($_POST['name']); $email_from = htmlentities($_POST['email']); $subject = htmlentities($_POST['subject']); $message = htmlentities($_POST['message']); $email_to = 'Admin@Domain.com';//replace with your email $headers = "From: webmaster@example.com" . "\r\n" . "CC: ". $email_from; //This adds a from field, as well as CC's the person submitting the request. //Build the body of the eamil $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email_from . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'message: ' . $message; $success = mail($email_to, "Contact from site X, regarding: ".$subject, $body,$headers); ?> <!DOCTYPE HTML> <html lang="en-US"> <head> <?php if($success){ //If the email was sent correctly?> <script> alert("Thank you for getting in touch. We will contact you as soon as possible."); </script> <?php header('Location: ../index.html'); }else{?> <script> alert("There was an error when sending the email, please try again later."); </script> <?php header('Location: ../index.html'); } //If the email falied?> </head> 中的代码即可。