dousi6192 2015-11-14 02:23
浏览 33

当提交按钮按下时,“PHP表单”电子邮件未被发送到收件箱...不明白为什么?

On the front end everything appears to be functioning; the 'Your message has been successfully sent!' appears when the submit button is pressed on the contact section (located on the page index.php - the only page on the website). Nevertheless, when I check the email address the php form is supposed to have been sent to, there is nothing received.... Can anyone suggest why?

HTML:

<form id="validationForm" method="post" action="index.php">

                        <label for="anti" id="antiLabel">This is an anti 's'-to-the-'pam' field. Accordingly, do not delete or enter any information here.</label>
                        <input name="anti" id="antiInput"/> 

                        <label for="name" class="label"></label>
                        <input name="name" id="name" class="input" placeholder="Name..." required/> 

                        <label for="email" class="label"></label>
                        <input name="email" id="email" class="input" placeholder="Email Address..." required/> 

                        <label for="subject" class="label"></label>
                        <input name="subject" id="subject" class="input" placeholder="Subject..." required/> 

                        <label for="message" id="labelMessage"></label>
                        <textarea name="message" id="inputMessage" placeholder="Message..." required></textarea>

                        <input id="submitButton" type="submit" value="Submit" class="input"/>

                </form>

jQuery:

<script>

$("#validationForm").submit(function(event){

        var errorMessage="";

        event.preventDefault();

            /* Regex validation of email address */

        if (!isValidEmailAddress($("#email").val())) {
            errorMessage="*Please enter a valid email address*";}


        if (errorMessage=="") {
            $("#error").html("");
            // send email via ajax
            $.post('index.php', {
                    name: $('#validationForm').find('input[name=name]').val(),
                    email: $('#validationForm').find('input[name=email]').val(),
                    subject: $('#validationForm').find('input[name=subject]').val(),
                    message: $('#validationForm').find('textarea[name=message]').val()
            }, function() {
                // after the ajax response and email sent alert the user
                alert("Your message has been successfully sent!");
            });
        } else {
            $("#error").html(errorMessage);
        }
    });

        /* '!' concerned with false i.e. if not a valid email */

    function isValidEmailAddress(emailAddress) {
        var pattern = new 
        RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
    };

    function ValidateEmailOnFly() {
        if (isValidEmailAddress($("#email").val()) || !$('#validationForm').find('input[name=email]').val()) {
            $("#error").html('');
        }
        else {
            $("#error").html('*Please enter a valid email address*');
        }
    }

    $(document).ready( function() {
        $('#validationForm').find('input[name=email]').blur( ValidateEmailOnFly );
    });

PHP:

<?php 
// check if the form was submitted and only then send the email
if(!empty($_POST["name"])) {
$ToEmail = '123@abc.co.uk'; 
$EmailSubject = 'Site contact form'; 

$mailheader = "From: ".$_POST["email"]."
"; 
$mailheader .= "Reply-To: ".$_POST["email"]."
"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1
"; 

$MESSAGE_BODY = "Name: ".$_POST["name"]."<br />"; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br />"; 
$MESSAGE_BODY .= "Subject: ".nl2br($_POST["subject"])."<br />"; 
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"]).""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害