sendmail.php没有重定向

时间:2015-05-03 00:19:21

标签: php html forms email

我有/下面的代码供用户从我的网站发送消息/电子邮件到我的网站电子邮件地址。

它工作正常,持续数周:验证字段内容(如果有空白的'必填字段',它要求"有效"字段内容即电子邮件地址),只要没有空白字段,它就会发送电子邮件并重新定向到感谢页面 - 该页面承认电子邮件已发送。

现在,它已停止正常工作。

它似乎不再像以前那样进行验证 - 因为空白字段没有警告/错误(如果有一个空白字段,它根本就不发送),它仍然会正确地将电子邮件发送到地址(如果没有空白的必填字段),但它不会再重定向到感谢页面。

以下是表单代码:

<form method="post" action="assets/sendmail.php">
<label for="name" class="nameLabel">Name</label>
<input id="name" type="text" name="name" placeholder="Enter your name...">
<label for="email" class="emailLabel">Email</label>
<input id="email" type="text" name="email" placeholder="Enter your email...">
<label for="subject">Subject</label>
<input id="subject" type="text" name="subject" placeholder="Your subject...">
<label for="message" class="messageLabel">Message</label>
<textarea id="message" name="message" placeholder="Your message..."></textarea>
<button type="submit">Send</button>
</form>

这是php代码 - 请注意我已经替换了电子邮件地址,原因很明显:)

<?php

// Email address verification
function isEmail($email) {
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email));
}

if($_POST) {

    // Enter the email where you want to receive the message
    $emailTo = 'me@myemail.com';

    $clientName = trim($_POST['name']);
    $clientEmail = trim($_POST['email']);
    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);

    $array = array();
    $array['nameMessage'] = '';
    $array['emailMessage'] = '';
    $array['messageMessage'] = '';

    if($clientName == '') {
        $array['nameMessage'] = 'Please enter your name.';
    }
    if(!isEmail($clientEmail)) {
        $array['emailMessage'] = 'Please insert a valid email address.';
    }
    if($message == '') {
        $array['messageMessage'] = 'Please enter your message.';
    }
    if($clientName != '' && isEmail($clientEmail) && $message != '') {
        // Send email
    $headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
    mail($emailTo, $subject, $message, $headers);
    }

    //echo json_encode($array);  
    { 
   header("location:../thankyou.html"); 
}

}

?>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

首先,对于电子邮件检查,您也可以使用内置的PHP。

filter_var($clientEmail, FILTER_VALIDATE_EMAIL)

我不知道您输出错误的位置,这可能就是他们没有展示的原因, 我已经添加了对

的支持

我重写了你的代码:

    <?php

if (!empty($_POST)) {

    // Enter the email where you want to receive the message
    $emailTo = 'me@myemail.com';

    $clientName = trim($_POST['name']);
    $clientEmail = trim($_POST['email']);
    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);

    $errors = array();

    if (empty($clientName)) {
        $errors['nameMessage'] = 'Please enter your name.';
    }
    if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
        $errors['emailMessage'] = 'Please insert a valid email address.';
    }
    if (empty($message)) {
        $errors['messageMessage'] = 'Please enter your message.';
    }

    // Are there errors?
    if (count($errors) == 0) {
        // Send email
        $headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
        mail($emailTo, $subject, $message, $headers);
        header("location:../thankyou.html");
    } else {
        foreach ($errors as $err) {
            echo $err . '<br />';
        }
    }
}
?>

答案 1 :(得分:0)

感谢您更新的PHP代码。不幸的是,它没有任何区别。它仍会在填写所有字段时发送电子邮件,如果“主题”为空,它甚至会发送,但它仍然不会重定向到感谢页面。如果姓名,电子邮件地址和消息为空,则不会发送。但它不会显示错误。我试图将两个文件放在父目录中,并从表单中的操作和PHP中的../中删除assets /。如下

<form method="post" action="sendmail.php">
<label for="name" class="nameLabel">Name</label>
<input id="name" type="text" name="name" placeholder="Enter your name...">
<label for="email" class="emailLabel">Email</label>
<input id="email" type="text" name="email" placeholder="Enter your email...">
<label for="subject">Subject</label>
<input id="subject" type="text" name="subject" placeholder="Your subject...">
<label for="message" class="messageLabel">Message</label>
<textarea id="message" name="message" placeholder="Your message..."></textarea>
<button type="submit">Send</button>
</form>

PHP就像您提供的那样(当然更改电子邮件地址)

<?php

if (!empty($_POST)) {

    // Enter the email where you want to receive the message
    $emailTo = 'me@myemail.com';

    $clientName = trim($_POST['name']);
    $clientEmail = trim($_POST['email']);
    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);

    $errors = array();

    if (empty($clientName)) {
        $errors['nameMessage'] = 'Please enter your name.';
    }
    if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
        $errors['emailMessage'] = 'Please insert a valid email address.';
    }
    if (empty($message)) {
        $errors['messageMessage'] = 'Please enter your message.';
    }

    // Are there errors?
    if (count($errors) == 0) {
        // Send email
        $headers = "From: " . $clientName . " <" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
        mail($emailTo, $subject, $message, $headers);
        header("location:thankyou.html");
    }   else {
        foreach ($errors as $err) {
            echo $err . '<br />';
        }
    }
}
?>

如果有错误则显示错误,一旦发送没有重定向。它只是保留在“联系”页面上,仍然显示已完成的表单。无法理解为什么重新指导工作但现在不行。我最初发布时无法登录。