如何在使用PHPMailer发送邮件后重定向

时间:2017-07-18 07:14:18

标签: php phpmailer

如何在使用PHPMailer发送电子邮件时重定向?

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true; 
$mail->SMTPSecure = "tls"; 
// $mail->Host       = "email-smtp.us-east-1.amazonaws.com";
$mail->Host       = "email-smtp.us-west-2.amazonaws.com";
$mail->Username   = "AKIAIVSF45PCGR7NZWNQ";
$mail->Password   = "Am2SBg4vluOvIc1+kycsWpCnxtf3jhGjYCAdBv7YYp/y";
//

$mail->SetFrom('test@gmail.com', 'Z-Reports'); //from (verified email address)
$mail->Subject = "Z-Reports (sent via smtp)"; //subject

//message
$body = emailZReports($total_sales, $inventory);

// $body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//

//recipient
$mail->AddAddress("test@gmail.com", "Z-Reports"); 

//Success
if ($mail->Send()) { 
    echo "Message Sent!";            
}

//Error
if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
}

如何重定向到特定网页而不是显示Message Sent!

5 个答案:

答案 0 :(得分:2)

如果您使用的是SMTP,则在$mail->SMTPDebug未设置为0的情况下,无法重定向到另一页。这是因为它发送了您无法使用header("Location: path/to/redirect")进行修改的标头。设置$mail->SMTPDebug = 0;,以便您可以重定向到$mail->send()之后的另一页。但是只有在完成调试并且邮件成功发送后,您才需要这样做。

答案 1 :(得分:0)

您可以使用php header功能

//Success
    if ($mail->Send()) { 
        header("Location:Yourfile.php");//echo "Message Sent!";            
    }

确保此功能之前没有echo或任何output

答案 2 :(得分:0)

你可以这样使用,

有关详细信息,请参阅Header。 它可以是这样的,

//Success
    if ($mail->Send()) { 
        header('Location: nextpage.php');            
    }

否则,您可以使用Javascript重定向用户。

只需使用

window.location = "http://www.nextpage.com/"

答案 3 :(得分:0)

这有效

if(!$mail->Send()) {
   echo "Message could not be sent.  ";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit; 
 } //echo getcwd(); 
header("Location:index.php") //redirects to a page named index.php;

答案 4 :(得分:0)

//制作另一个重定向脚本

<?php
require_once("mail_function.php");
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
header("Location: form.php");exit; //redirect to form submit page
} else {
  echo "Message sent!";
header("Location: index.php");exit; //redirect to your home page
}
?>

// mail_function.php

<?php
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Mailer   = "smtp";
$mail->SetFrom("xxxx@gmail.com", "web");
$mail->Port = '587';                //Sets the default SMTP server port
$mail->SMTPAuth = true;                     //Sets SMTP authentication.
$mail->Username = 'xxxx@gmail.com';                 //Sets SMTP username
$mail->Password = 'xxxx';                   //Sets SMTP password
$mail->SMTPSecure = 'tls';               //Sets connection prefix.
$mail->FromName = $_POST["name"];
$mail->AddAddress($email);          //Adds a "To" address
$mail->AddCC($_POST["email"], $_POST["name"]);  //Adds a "Cc" address
$mail->WordWrap = 50;           //Sets word wrapping on the body
$mail->IsHTML(true);                    //Sets message type to HTML 
$mail->Subject = "somthing";
$mail->MsgHTML($message_body);
$mail->Body = "somthing";
$result = $mail->Send();
return $result;
?>