【问题标题】:jQuery / AJAX / PHP form "working" but not delivering emailsjQuery / AJAX / PHP 表单“工作”但不发送电子邮件
【发布时间】:2010-01-27 23:39:08
【问题描述】:

这不是我的主机或它应该发送到的电子邮件帐户的问题:这些事情我已经缩小了范围。该表单完美地运行了一天,然后停止了,我的代码没有任何更改。有人对此有意见吗?

我的代码:

插件:http://www.stephaniehenderson.com/jquery.form.js

在头脑中:

<script type="text/javascript">
    $(document).ready(function () {
        var options = {
            target: '#alert'
        };
        $('#contactForm').ajaxForm(options);
    });

    $.fn.clearForm = function () {
        return this.each(function () {
            var type = this.type,
                tag = this.tagName.toLowerCase();
            if (tag == 'form')
                return $(':input', this).clearForm();
            if (type == 'text' || type == 'password' || tag == 'textarea')
                this.value = '';
            else if (type == 'checkbox' || type == 'radio')
                this.checked = false;
            else if (tag == 'select')
                this.selectedIndex = -1;
        });
    };
</script>    

实际形式:

<form id="contactForm" method="post" action="sendmail.php"> 

<fieldset> 

<p>Email Me</p> 
<div id="fieldset_container"> 
<label for="name">Your Name:</label> 
<input type="text" name="name" id="name" /><br /><br /> 

<label for="email">Email:</label> 
<input type="text" name="email" id="email" /><br /><br /> 

<span style="display:none;"> 
<label for="last">Honeypot:</label> 
<input type="text" name="last" value="" id="last" /> 
</span><br /><br /> 

<label for="message">Comments &amp; Inquiries:</label> 
<textarea name="message" id="message" cols="" rows=""></textarea><br/> 
</div> 
<div id="submit_button"> 
<input type="submit" name="submit" id="submit" value="Send It" /> 
</div> 
</fieldset> 

</form> 

<div class="message"><div id="alert"></div></div>

sendmail.php:

<?php
$sendto = 'mypersonal@emailaddress.com';

$subject = 'SH Contact Form';

$errormessage = 'There seems to have been a problem. May I suggest...';

$thanks = "Thanks for the email!";

 //        Message for the bot when it fills in in at all.
$honeypot = "You filled in the honeypot! If you're human, try again!";

//        Various messages displayed when the fields are empty.
$emptyname =  'Entering your name?';
$emptyemail = 'Entering your email address?';
$emptymessage = 'Entering a message?';

//       Various messages displayed when the fields are incorrectly formatted.
$alertname =  'Entering your name using only the standard alphabet?';
$alertemail = 'Entering your email in this format: <i>name@example.com</i>?';
$alertmessage = "Making sure you aren't using any parenthesis or other escaping     characters in the message? Most URLS are fine though!";


//Setting used variables.
$alert = '';
$pass = 0;

// Sanitizing the data, kind of done via error messages first. Twice is better!  ;-)
function clean_var($variable) {
    $variable = strip_tags(stripslashes(trim(rtrim($variable))));
    return $variable;
}

//The first if for honeypot.
if ( empty($_REQUEST['last']) ) {

    // A bunch of if's for all the fields and the error messages.
    if ( empty($_REQUEST['name']) ) {
        $pass = 1;
        $alert .= "<li>" . $emptyname . "</li>";
    } elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] ) ) {
        $pass = 1;
        $alert .= "<li>" . $alertname . "</li>";
    }
    if ( empty($_REQUEST['email']) ) {
        $pass = 1;
        $alert .= "<li>" . $emptyemail . "</li>";
    } elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-  z]{2,3})$", $_REQUEST['email']) ) {
        $pass = 1;
        $alert .= "<li>" . $alertemail . "</li>";
    }
    if ( empty($_REQUEST['message']) ) {
        $pass = 1;
        $alert .= "<li>" . $emptymessage . "</li>";
    } elseif ( ereg( "[][{}()*+?\\^$|]", $_REQUEST['message'] ) ) {
        $pass = 1;
        $alert .= "<li>" . $alertmessage . "</li>";
    }

    //If the user err'd, print the error messages.
    if ( $pass==1 ) {

        //This first line is for ajax/javascript
        echo "<script>$(\".message\").hide(\"slow\").show(\"slow\"); </script>";
        echo "<b>" . $errormessage . "</b>";
        echo "<ul>";
        echo $alert;
        echo "</ul>";

    // If the user didn't err and there is in fact a message, time to email it.
    } elseif (isset($_REQUEST['message'])) {

        //Construct the message.
        $message = "From: " . clean_var($_REQUEST['name']) . "\n";
        $message .= "Email: " . clean_var($_REQUEST['email']) . "\n";
        $message .= "Message: \n" . clean_var($_REQUEST['message']);
        $header = 'From:'. clean_var($_REQUEST['email']);

        //Mail the message - for production
        mail($sendto, $subject, $message, $header, "-fstephanie@stephaniehenderson.com");
        //This is for javascript, 
        echo   "<script>$(\".message\").hide(\"slow\").show(\"slow\").animate({opacity: 1.0},        4000).hide(\"slow\"); $(':input').clearForm() </script>";
        echo $thanks;
        die();

        //Echo the email message - for development
        echo "<br/><br/>" . $message;

    }

//If honeypot is filled, trigger the message that bot likely won't see.
} else {
    echo "<script>$(\".message\").hide(\"slow\").show(\"slow\"); </script>";
    echo $honeypot;
}
?>

【问题讨论】:

  • 它在本地主机上吗?我知道如果 php.ini 中没有定义 smtp 服务器,邮件功能可能会出现问题
  • 您是否检查过邮件没有返回错误,并且所有详细信息都正确传递给它?或者,邮件发件人的电子邮件是否与服务器的地址匹配 - 如果不匹配,则很可能被视为垃圾邮件。
  • trim(rtrim($variable)) 毫无意义——trim 是两端

标签: php jquery ajax forms post


【解决方案1】:

有时 php 中的 mail() 函数会延迟...只要确保它返回 true 就可以了。

【讨论】:

    【解决方案2】:

    这是一个使用过时 php 的表单。将所有 _REQUEST 更改为 _POST

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 2014-05-29
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多