使用PHP从HTML表单发送邮件

时间:2018-06-12 10:41:43

标签: javascript php

我正在尝试从我的静态网页的联系表单发送电子邮件,但我没有收到该消息,或者该消息因某些原因未发送。

的index.html

    <form id="contac-form" action="action.php" method="post" class="row" name="contact-form">
      <div class="col-md-12 mb-3 text-center">
        <h2>GET IN TOUCH</h2>
      </div>
      <div class="field-group col-md-6">
        <div class="input">
          <input type="text" name="name" id="name">
          <label for="name">Name *</label>
        </div>
      </div>
      <div class="field-group col-md-6">
        <div class="input">
          <input type="email" name="email" id="email">
          <label for="email">Email *</label>
        </div>
        <div class="error-msn text-center" id="error-email">
          <p>Please enter a valid email.</p>
        </div>
      </div>
      <div class="field-group textarea col-md-12">
        <div class="input">
          <textarea name="message" id="message" cols="30" rows="4"></textarea>
          <label for="message">Message *</label>
        </div>
      </div>
      <div class="error-msn text-center" id="succesfull-msn">
        <p>Your message has been sended.</p>
      </div>
      <div class="error-msn text-center" id="error-msn">
        <p>Please fill all the fields.</p>
      </div>
      <div class="col-md-12 submit-btn text-left">
        <input type="button" id="submit" class="btnth" value="SEND">
      </div>
    </form>

的script.js

  function emailValidation(value){
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(value);
  }
  submit.on('click', function(){
    if (name.val() === '' || email.val() === '' || message.val() === '') {
      error.addClass('error');
    } else if (name.val() !== '' && email.val() !== '' && message.val() !== '') {
      error.removeClass('error');
      if (!emailValidation(email.val())) {
        console.log('here');
        erro_email.addClass('error');
      } else {
        erro_email.removeClass('error');
        succesfull_msn.addClass('error');
        contact_form.submit();
        setTimeout(function(){
          succesfull_msn.removeClass('error');
        }, 3000);
        console.log('submited');
      }
    }
  });

action.php的

<?php
  $to = "mianfrigo@gmail.com"; // this is your Email address
  $from = $_POST['email']; // this is the sender's Email address
  $name = $_POST['name'];
  $subject = "Form submission";
  $subject2 = "Copy of your form submission";
  $message = $first_name . " wrote the following:" . "\n\n" . $_POST['message'];
  $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
  $header = "From: $email \r\n";

  $headers = "From:" . $from;
  $headers2 = "From:" . $to;
  mail($to,$subject,$message,$header);
  mail($from,$subject2,$message2,$header); // sends a copy of the message to the sender
?>

表单运行良好,验证工作正常,但提交表单时注意到这里真的发生了什么可能是错误的。

0 个答案:

没有答案