带有Sendemail的HTML表单

时间:2017-03-08 14:32:02

标签: javascript php html email

我无法弄清楚问题的来源! mail()工作正常(我已经用简单的php文件测试了它)但我的表单没有发送任何电子邮件!

HTML

  <section id="contact">
    <div id="google-map" class="wow fadeIn" data-latitude="35.7777622" data-longitude="10.8285004" data-wow-duration="1000ms" data-wow-delay="400ms"></div>
    <div id="contact-us" class="parallax">
      <div class="container">
        <div class="row">
          <div class="heading text-center col-sm-8 col-sm-offset-2 wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
            <h2>Contact</h2>
            <p>Send a message</p>
          </div>
        </div>
        <div class="contact-form wow fadeIn" data-wow-duration="1000ms" data-wow-delay="600ms">
          <div class="row">
            <div class="col-sm-6">
              <form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
                <div class="row  wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
                  <div class="col-sm-6">
                    <div class="form-group">
                      <input type="text" name="name" class="form-control" placeholder="Name" required="required">
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <div class="form-group">
                      <input type="email" name="email" class="form-control" placeholder="Email" required="required">
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <input type="text" name="subject" class="form-control" placeholder="Sub" required="required">
                </div>
                <div class="form-group">
                  <textarea name="message" id="message" class="form-control" rows="4" placeholder="Type your message" required="required"></textarea>
                </div>                        
                <div class="form-group">
                  <button type="submit" class="btn-submit">Send</button>
                </div>
              </form>   
            </div>
            <div class="col-sm-6">                           
            </div>
          </div>
        </div>
      </div>
    </div>        
  </section><!--/#contact-->

main.js

// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),
        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Progress...</p>').fadeIn() );
        }
    }).done(function(data){
        form_status.html('<p class="text-success">Thanks for your message</p>').delay(3000).fadeOut();
    });
});

sendemail.php

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$from       = @trim(stripslashes($_POST['email'])); 
$subject    = @trim(stripslashes($_POST['subject'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to         = 'MyEmail@RemovedFor.privacy';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $message, $headers);

die;
你能帮我解决一下这个问题吗?当我向sendemail.php文件添加简单邮件(&#39; MyEmail@RemovedFor.privacy',&#39;我的主题&#39;,&#39;消息&#39;)时,每次表单提交后,我收到一封电子邮件!但我从未收到过邮件($ to,$ subject,$ message,$ headers);

非常感谢

2 个答案:

答案 0 :(得分:1)

您根据用户的输入欺骗From标头。不要这样做。

电子邮件的发件人标题必须准确代表电子邮件的发件人 - 如果您在此标题中插入另一个值,许多邮件服务器将拒绝您的邮件或将其标记为垃圾邮件,因为生成的邮件将失败{ {3}}和/或SPF检查。

使用From标头准确表示邮件的来源。例如,如果您的网站是example.com,请将From标头设置为From: webmaster@example.com。包括用户在邮件正文中或Reply-To标题中设置的电子邮件地址和名称。

答案 1 :(得分:0)

我认为Headers需要是一个字符串。

$headers  = '';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion() . "\r\n";

http://php.net/manual/en/function.mail.php

您还应该过滤$ _POST

http://php.net/manual/en/function.filter-input.php

我认为您还需要通过ajax请求发送数据。

$.ajax({
  url: $(this).attr('action'),
  type: "POST",
  data: $(this).serialize(),