将邮件成功转发到另一个页面

时间:2014-01-05 19:13:52

标签: php

在这个php sendmail上我怎么能这样做呢,当我发送电子邮件时,我把它转发到一个URL,而不只是说“谢谢你的消息已被发送”。我无法理解我的生活。我试过了

header("Locations: URL") 

但这似乎不起作用。这是我的完整代码

<?php

$name = trim($_POST['name']);
$email = $_POST['email'];
$comments = $_POST['comments'];

$site_owners_email = 'email@example.com'; // Replace this with your own email   address
$site_owners_name = 'example'; // replace with your name

if (strlen($name) < 2) {
    $error['name'] = "Please enter your name";  
}

if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address"; 
}

if (strlen($comments) < 3) {
    $error['comments'] = "Please leave a comment.";
}

if (!$error) {

    require_once('phpMailer/class.phpmailer.php');
    $mail = new PHPMailer();

    $mail->From = $email;
    $mail->FromName = $name;
    $mail->Subject = "Contact Form";
    $mail->AddAddress($site_owners_email, $site_owners_name);
    $mail->Body = $comments;

    $mail->Send();



    echo "<div class='alert-box success'>Thanks " . $name . ". Your message has        been sent.<a href='' class='close' onclick='clearForms()'>&times;</a></div>";

} # end if no error
else {

    $response = (isset($error['name'])) ? "<div class='alert-box alert'>" . $error['name'] . "</div> \n" : null;
    $response .= (isset($error['email'])) ? "<div class='alert-box alert'>" . $error['email'] . "</div> \n" : null;
    $response .= (isset($error['comments'])) ? "<div class='alert-box alert'>" . $error['comments'] . "</div>" : null;

    echo $response;
} # end if there was an error sending

&GT;

2 个答案:

答案 0 :(得分:1)

使用header(..)的方法是正确的。您只需要为页面启用输出缓冲,以确保在操作标题之前没有内容发送给用户。

基本上,确保在发送任何其他内容之前首先在页面上调用ob_start()(即在开始输出缓冲之前不是HTML或echo语句)。

<?php

    ob_start();

    $name = trim($_POST['name']);
    $email = $_POST['email'];
    $comments = $_POST['comments'];

    $site_owners_email = 'email@example.com'; // Replace this with your own email   address
    $site_owners_name = 'example'; // replace with your name

    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name";  
    }

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address"; 
    }

    if (strlen($comments) < 3) {
        $error['comments'] = "Please leave a comment.";
    }

    if (!$error) {
        require_once('phpMailer/class.phpmailer.php');
        $mail = new PHPMailer();

        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->Body = $comments;

        $mail->Send();

        header("Location: www.example.com");

        echo "<div class='alert-box success'>Thanks " . $name . ". Your message has        been sent.<a href='' class='close' onclick='clearForms()'>&times;</a></div>";

    } # end if no error
    else {
        $response = (isset($error['name'])) ? "<div class='alert-box alert'>" . $error['name'] . "</div> \n" : null;
        $response .= (isset($error['email'])) ? "<div class='alert-box alert'>" . $error['email'] . "</div> \n" : null;
        $response .= (isset($error['comments'])) ? "<div class='alert-box alert'>" . $error['comments'] . "</div>" : null;

        echo $response;
    } # end if there was an error sending

    ob_end_flush();
 ?>

答案 1 :(得分:0)

header("Location: http://www.example.com");

而不是“位置”