PHP电子邮件表单错误

时间:2012-02-13 12:11:16

标签: php html email

我的PHP发送表单遇到了一些问题。按下提交按钮时,我没有收到电子邮件。但是,我确实通过了成功" SUCCESS"页。

任何人都可以查看我的文件吗?

提前致谢。

Contact.php

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Contact I Messenbrink.eu</title>
<link href="_css/main.css" rel="stylesheet" type="text/css">
<link href="_css/contact.css" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Quattrocento+Sans' rel='stylesheet' type='text/css'>
<!-- Internet Explorer HTML5 enabling script: -->

    <!--[if IE]>

        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <style type="text/css">

            .clear {
                zoom: 1;
                display: block;
            }

        </style>

    <![endif]-->
<!--<script src="js/jquery.placeholder.js"></script>
<script src="js/modernizr-min.js"></script>
<script>
if (!Modernizr.input.placeholder){
 $('input[placeholder], textarea[placeholder]').placeholder();
}
</script>
<script src="js/jquery.form.js"></script>
<script src="js/jquery.validate.js"></script>
<script>
$(function(){
$('#contact').validate({
submitHandler: function(form) {
$(form).ajaxSubmit({
url: 'process.php',
success: function() {
$('#contact').hide();
$('#contact-form').append("<p class='thanks'>Thanks! Your request has been sent.</p>")
}
});
}
});
});
</script>-->


</head>

<body>

<section id="page"> <!-- Defining the #page section with the section tag -->

            <header> <!-- Defining the header section of the page with the appropriate tag -->

                <hgroup>
            <img id="logo" src="_Images/Logo_messenbrink_tryout.png" height="45"/>
            <h3>Film &middot; Photography &middot; Graphic &middot; Web</h3>
                </hgroup>

                    <div class="socialbuttons">

        <div id="email_button"><a href="Contact.html"><img src="_Images/SocialButtons/email.png" /></a></div>

        <div id="deviant"><a href="http://messenbrink.deviantart.com/gallery/"><img src="_Images/SocialButtons/deviant.png" /></a></div>


            <div id="social_facebook">

                <div id="facebook_like_button_holder">  
                    <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
                    <fb:like href="http://www.messenbrink.eu" send="false" layout="button_count" width="100" show_faces="false" font=""></fb:like>

                    <div id="fake_facebook_button"></div>

                </div>

                <script type="text/javascript">
                FB.Event.subscribe('edge.create', function(response) {
                window.location = "http://www.messenbrink.eu";
                });
                </script>

            </div>


        <div id="custom_twitter_button">
            <a href="https://twitter.com/share?url=https%3A%2F%2Fdev.twitter.com%2Fpages%2Ftweet-button" target="_blank"></a>
<!--            <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
-->     </div>

<!--        <div id="stumble">

            <div id="stumble_button_holder">
                <su:badge layout="6"></su:badge>

            </div>

                <div id="stumble_button_new" style="background-image:url(_Images/SocialButtons/stumble.png); background-size:24px;"></div>


             <script type="text/javascript"> 
             (function() { 
                 var li = document.createElement('script'); li.type = 'text/javascript'; li.async = true; 
                  li.src = 'https://platform.stumbleupon.com/1/widgets.js'; 
                  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(li, s); 
             })(); 
             </script>
        </div>
-->       
    </div>

               <?php include("includes/navigation.php");?>

            <div id="n1"/>
            </header>

            <section id="articles"> <!-- A new section with the articles -->

                <!-- Article 1 start -->

                <article id="article1"> <!-- The new article tag. The id is supplied so it can be scrolled into view. -->
<!--Contact Form-->
<!--Contact Form-->
<div id="contact-form">    

<form id="contact_messenbrinkeu" action="sendform.php" method="post">
<fieldset id="form">

            <label for="name">Name</label><br>
            <input type="text" name="name" placeholder="Your Name"title="enter your name" class="alpha required"><br>

            <label for="email">E-mail</label><br>
            <input type="email" name="email" placeholder="mail@example.com" title="Enter your e-mail address" class="mail required"><br>

            <label for="phone">Phone</label><br>
            <input type="text" name="phone" placeholder="+45 12 34 56 78" class="numeric"><br>

            <label for="website">Website</label><br>
            <input type="text" name="website" placeholder="www.example.com" id="website"><br>

            <label for="message">Message</label><br id="message">
            <textarea name="message" class="required"></textarea>

            </fieldset>
            <fieldset id="button">

            <button type="submit">>>&nbsp;&nbsp;&nbsp;Send</button>
            <button type="reset">reset&nbsp;&nbsp;&nbsp;<<</button>
</fieldset>
</form>

</div><!-- /end #contact-form -->

</article><!-- Article 1 end -->
</section>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
jQuery.noConflict();
jQuery(document).ready(function($){
    // when submit button is pressed
    $("#contact_messenbrinkeu").submit(function() {

        var pass = true;

        var errors = {
            required    : 'this field is required',
            email       : 'enter a valid email address',
            numeric     : 'enter a number without spaces, dots or commas',
            alpha       : 'this field accepts only letters &amp; spaces'
        };

        var tests = {
            email       : /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
            numeric     : /^[0-9]+$/,
            alpha       : /^[a-zA-Z ]+$/
        };

        // clear error messages
        $(".error").removeClass();
        $(".error-message").remove();

        function displayError(el, type) {
            $(el).parent().addClass("error").find('label').append('<span class=\"error-message\"> &#8211; ' + errors[type] + '</span>');
        }

        $('.required, .email, .numeric, .alpha').each(function(){
            var value       = $(this).val();
            var valueExists = value.length === 0 ? false : true;

            var required    = $(this).hasClass('required');
            var email       = $(this).hasClass('email');
            var numeric     = $(this).hasClass('numeric');
            var alpha       = $(this).hasClass('alpha');

            if (required && value.length===0) {
                displayError(this,'required');
                pass=false;
            }

            else if (email && valueExists && !tests.email.test(value)) {
                displayError(this,'email');
                pass=false;
            }

            else if (numeric && valueExists && !tests.numeric.test(value)) {
                displayError(this,'numeric');
                pass=false;
            }

            else if (alpha && valueExists && !tests.alpha.test(value)) {
                displayError(this,'alpha');
                pass=false;
            }
        });

        return pass;
    });
});
/*]]>*/
</script>

</body>
</html>

sendform.php

<?php
    ini_set("display_errors", "0");
    $post_data = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );

    $name = $post_data["name"];
    $email = $post_data["email"];
    $phone = $post_data["phone"];
    $website = $post_data["website"];
    $message = $post_data["message"];

    # select data that needs validation
    $validate = array(
        'required'  => array($name,$email,$message),
        'validEmail'    => array($email),
        'validNumber'   => array($phone),
        'validAlpha'    => array($name)
    );
    $formcontent = "Name: $name \nE-Mail: $email \nPhone: $phone \nWebsite: $website \nMessage: $message \n";
    $formcontent = wordwrap($formcontent, 70, "\n", true);

    $recipient = "email@yourdomain.com"; 
    $subject = "Contact Messenbrink.eu"; 
    $mailheader = "From: $email \r\n";

    function sendMail() {
        global $formcontent, $recipient, $subject, $mailheader;
        mail($recipient, $subject, $formcontent, $mailheader);
    }

    # error messages
    $errorsMsgs = array(
        'required'  => 'Please fill out all required fields.',
        'validEmail'    => 'is an invalid email address.',
        'validNumber'   => 'is an invalid number.',
        'validAlpha'    => 'contains invalid characters. This field only accepts letters and spaces.'
    );

    $errorMarkup    = "<h1>We found a few errors :-(</h1><h2>Please fix these errors and try again</h2><ol>";
    $errorMarkupEnd = "</ol>";
    $successMarkup  = "<h1>Success!</h1><h2>Your form was sent successfully.</h2>";
    $backMarkup     = "<a href=\"" . $_SERVER['HTTP_REFERER'] . "\">Back to form</a>";

    # begin state
    $valid = true;

    # loop through fields of error types
    foreach ($validate as $type => $fields) {
        # loop through values of fields to be tested
        foreach ($fields as $value) {
            # throw error if value is required and not entered
            if ($type === 'required' && strlen($value) === 0) {
                $errorMarkup .= "<li>$errorsMsgs[$type]</li>";
                $valid = false;
                break;
            }
            else if (
                $type === 'validEmail'  && !filter_var($value, FILTER_VALIDATE_EMAIL) ||
                $type === 'validNumber' && !preg_match('/^[0-9 ]+$/', $value) ||
                $type === 'validAlpha'  && !preg_match('/^[a-zA-Z ]+$/', $value)
            ) {
                if (strlen($value) === 0) {break;} # skip check if value is not entered
                $errorMarkup .= "<li>\"$value\" $errorsMsgs[$type]</li>";
                $valid = false;
                continue;
            }
        }
    }

    if ($valid) {
        sendMail();
        $body = $successMarkup . $backMarkup;
        $title = "Form sent";
    } else {
        $body = $errorMarkup . $errorMarkupEnd . $backMarkup;
        $title = "Form errors";
    }

    # write html ouput
    echo "<!DOCTYPE html><head><title>$title</title><style type=\"text/css\">body{margin:100px;font:16px/1.5 sans-serif;color:#111}h1{font-size:32px;margin:0;font-weight:bold}h2{font-size:18px;margin:0 0 20px 0}ol,li{list-style-position:inside;padding-left:0;margin-left:0}</style></head><body>$body</body></html>";
?>

3 个答案:

答案 0 :(得分:2)

我建议您启用错误报告和日志记录。参见

Enable error reporting

  • PHP文件:

    error_reporting(E_ALL);
    
  • htaccess的:

    php_value error_reporting 65535
    

Enable displaying of errors

  • PHP文件:

    ini_set('display_errors', true);
    
  • htaccess的:

    php_flag display_errors On
    

Enable error logging

  • PHP文件:

    ini_set('log_errors', true);
    
  • htaccess的:

    php_flag log_errors On
    

Set error log

  • PHP文件:

    ini_set('error_log', '/path/to/php.log');
    
  • htaccess的:

    php_value error_log /path/to/php.log
    

此外,您可能需要检查Web服务器的错误日志和Web服务器的邮件日志。

答案 1 :(得分:0)

您缺少某些标题信息,这些信息会使您的电子邮件更加合法(例如MIME版本,内容类型等),因此邮件可能会被标记为来自Exchange服务器的垃圾邮件,而不会到达目的地。这是我使用的功能,它甚至成功地传递了Hotmail,Gmail和Yahoo邮件。

function mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="")
{
    $subject = nl2br($subject);
    $sendmailbody = nl2br($sendmailbody);
    if($bcc!="")
    {
        $headers = "Bcc: ".$bcc."\n";
    }
    $headers = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=utf-8 \nContent-Transfer-Encoding: 8bit\n";
    $headers .= "X-Priority: 3\n";
    $headers .= "X-MSMail-Priority: Normal\n";
    $headers .= "X-Mailer: PHP/"."MIME-Version: 1.0\n";
    $headers .= "From: " . $from . "\n";
    $headers .= "Content-Type: text/html\n";
    mail("$sendto","$subject","$sendmailbody","$headers");
}

答案 2 :(得分:0)

查看电子邮件并且FILTER_SANITIZE_SPECIAL_CHARS没有清理特殊字符@ @,请检查您是否可以使用静态电子邮件ID获取任何电子邮件,这意味着您的服务器是否配置为使用脚本发送电子邮件?