【问题标题】:jQuery + PHP is sending duplicate blank emailsjQuery + PHP 发送重复的空白电子邮件
【发布时间】:2013-01-11 07:02:07
【问题描述】:

正在尝试设置联系表单。表单提交电子邮件和可选的抄送发件人。如果启用了 javascript,表单将提交到 contact-ajax.php(用感谢消息替换表单),否则提交到 contact.php(重定向到感谢页面)。备用表单工作正常,但每次 ajax 表单触发时,它也会发送一封完全空白的电子邮件(如果选中了 CC 选项,则发送给发件人和收件人)。我无法弄清楚为什么会这样。任何见解将不胜感激。

表单 HTML:

<form method="post" id="email_form" name="email_form" action="contact.php">
    <label for="name">Name:</label><br />
    <input type="text" name="name" id="name" placeholder="John Doe" /><br />
    <label for="email">Email Address:</label><br />
    <input type="text" name="email" id="email" placeholder="johndoe@gmail.com" /><br />
    <label for="subject">Subject </label></td><br />
    <input type="text" name="subject" id="subject" placeholder="Subject" /><br />
    <label for="message">Message:</label></td><br />
    <textarea cols="35" rows="5" name="message" id="message" placeholder="Your Message"></textarea><br />
    <input type="checkbox" name="cc" id="cc" checked value="Yes" /><label for="cc"> Send yourself a copy of this message</label><br />
    <input class="submit pulldown button" name="submit" id="submit" type="submit" value="Send Message" />
</form>

Javascript:

$(document).ready(function() {
$('#email_form #submit').click(function(event) {
    event.preventDefault();
    $.ajaxSetup ({
        cache: false
        });
    $.ajax({
        type: 'POST',
        url: 'contact-ajax.php',
        data: $("#email_form").serializeArray(),
        success: function(msg) {
            if(msg=='Mail sent') {
                alert(msg);
                }
            else {
                alert('Error sending mail. Please try again.');
                }
            },
        error: function(ob,errStr) {
            alert('Error sending mail. Please try again.');
            }
        });
    return false;
    });
});

脚本调用的PHP表单(contact-ajax.php):

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $email_subject = $_POST['subject'];
    $message = $_POST['message'];
    $today = date('r');
    $email_from = "address@domain.tld";
    $to = "address@domain.tld";
    $email_body = "On $today $name wrote:\n"."$message";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    if (isset($_POST['cc'])) {
        $headers .= "CC: $visitor_email \r\n";
        }
    $headers .= "X-Mailer: PHP/". phpversion();
    mail($to,$email_subject,$email_body,$headers);
    if(mail($to, $subject, $body, $headers)) {
        die('Mail sent');
        }
    else {
        die('Error: Mail failed');
        }
    }
?>

非 Ajax php 脚本 (contact.php) 相同,只是它以 header('Location: thanks_page.html'); 而不是最后一个 if 语句结尾。

空白电子邮件不包含主题或消息正文,但“发件人”、“回复”和“抄送”字段均按照正确(非空)消息中的内容填写。几天来我一直试图找出问题所在,但似乎无法找到它。

【问题讨论】:

  • 您是否在 javascript 提交后检查过您的帖子数据是否包含任何内容?如果没有,则将您的 $("#email_form").serializeArray() 更改为数据: $("#email_form").serialize()

标签: php jquery forms email


【解决方案1】:

除了所有关于为什么你收到两封邮件的答案之外,第二封邮件是空的还有一个相当明显的原因:

mail($to,$email_subject,$email_body,$headers);
if(mail($to, $subject, $body, $headers)) 

在 if 语句中,您有 $subject$body 而不是 $email_subject$email_body

【讨论】:

    【解决方案2】:

    mail函数发送邮件..你写了2次mail()..足够检查if中的返回值

    mail($to,$email_subject,$email_body,$headers);
    if(mail($to, $subject, $body, $headers)) { ......
    

    只能这样使用:

    if(mail($to, $subject, $body, $headers)) { //mail sent 
    } else { 
    //mail not sent 
    }
    

    更新 使用PHPMailer 易于使用,我认为比简单的php 邮件功能更好。

    【讨论】:

    • 哈!我怎么错过了?第一行直接来自我的contact.php。我一直在使用另一个ajax脚本作为示例模板,我想我一定忘记在这个过程中修改邮件功能。现在工作!谢谢 - 当你盯着代码看时,显而易见的答案总是你错过的!
    【解决方案3】:

    Bence 有一个有效点,您调用了两次邮件函数...

    我不太确定为什么有些信息是空白的,但你能在 PHP 中做到这一点吗? :

    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    if (isset($_POST['cc'])) {
        $headers .= "CC: $visitor_email \r\n";
        }
    

    我总是这样串联:

    $headers = "From: ".$email_from." \r\n";
    $headers .= "Reply-To: ".$visitor_email." \r\n";
    if (isset($_POST['cc'])) {
        $headers .= "CC: ".$visitor_email." \r\n";
        }
    

    【讨论】:

      【解决方案4】:

      我相信是这样的:

      if (isset($_POST['cc'])) {
          $headers .= "CC: $visitor_email \r\n";
      }
      $headers .= "X-Mailer: PHP/". phpversion();
      mail($to,$email_subject,$email_body,$headers); <-- Sends FIRST email
      if(mail($to, $subject, $body, $headers)) { <-- Sends ANOTHER email
          die('Mail sent');
          }
      else {
          die('Error: Mail failed');
          }
      }
      

      不妨试试:

      $retVal = mail($to,$email_subject,$email_body,$headers);
      if($retVal) {
      

      谢谢, 马特

      【讨论】: