2014-07-01 36 views
0

因此,我正在使用并成功地使用PHPmailer发送电子邮件,但是一旦电子邮件发送,我不确定将页面重定向回到您来自哪个页面的消息,这些消息可能会发送消息?PHP邮件查询

<?php 

if (isset($_POST['hp']) && $_POST['hp'] && $_POST['hp'] != '') {  

} 

else { 

require 'c:\php\includes\PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = ''; // Specify main and backup server 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '';       // SMTP username 
$mail->Password = '';       // SMTP password 
$mail->SMTPSecure = 'ssl';       // Enable encryption, 'ssl' also accepted 
$mail->Port =; 

$mail->From = ''; 
$mail->FromName = ''; 


foreach ($members as $user) { 

$mail->addAddress($user->user_email); 

} 

foreach ($committee as $user) { 

$mail->AddCC($user->user_email); 

} 





$mail->WordWrap = 50;         // Set word wrap to 50 characters 

$mail->isHTML(true);         // Set email format to HTML 

$mail->Subject = $_POST["Subject"]; 
$mail->Body = $_POST["contentfield"]; 





if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    exit; 

} 
} 

?> 

我已将所有的细节都拿出来了,原因很明显。

<?php 
header('Location: /path/to/page'); 
exit; 
+0

你只是问如何重定向用户从PHP代码?所有这些邮件代码(你说是否按预期工作)是否真的与这有关? – David

+0

嗨,是的问题是,当我包括一个重定向,如标题(位置)它会停止发送出于某种原因的电子邮件。 – user3725879

+0

在这种情况下,听起来好像有*在某处出错。你能显示导致这个错误的代码吗?当你调试时,它在哪里失败? – David

回答

2

为什么不是其他人添加到您的if()邮件不发出声明,将用户重定向如果if语句不会失败:使用位置标头在PHP

1

您可以发送HTTP重定向?

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    exit; 

} else { 
    // email sent successfully, redirect to success page 
    header ('Location: sent.php'); 
} 

您的邮箱还是应该送,因为浏览器将只重定向你,如果$mail->send()计算结果为真......而在这一点上的邮件已经发送。

如果您愿意,也可以在if()声明后面加上重定向,这也应该起作用,因为如果if()语句的计算结果为true,它将退出脚本。

+0

当我包括它停止发送电子邮件,也许我把它放在错误的地方? – user3725879

+0

对不起,我现在看到您问的是重定向的位置,而不是如何实际执行重定向。只需将重定向放置在该脚本的底部即可。发送电子邮件的行是包含'$ mail-> send()'的行。在该行之后(并且在最后一个条件块之外)的任何地方都可以工作。 – oliakaoil

0

您不一定需要重定向。您可以返回一个204 No Content,浏览器将保持原样。如果你想返回一个状态码,你可以将结果作为参数传递给重定向,重定向到一个不同的页面,或者在会话中保留一个值,准备显示在下一页上。就像:

if(!$mail->send()) { 
    header('Location: failed.php?error='.rawurlencode($mail->ErrorInfo)); 
} 
    header('Location: success.php'); 
}