简体   繁体   中英

Redirecting after sending Email with PHPMailer

I have a problem with the redirection after sending an Confirmation Email with PHPMailer . The User doesn't get redirected to the memberIndex.php .

I've tried with:

`$mail->Send();
header("Location:../memberIndex.php");
exit();`

Also turning off Debugging with:

`$mail->SMTPDebug = 0;` 

And all combinations with ob_start/flush/clean etc.

Note: When I left the body of the Mail ( $mail->Body ) empty, it sends an email and redirects as it should. Otherwise, it stays on the registration page. In the Body, I have some HTML-Tags and texts. Could it be, that the body blocks the redirection?

Here is my code where I build the email:

require '../../PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'host';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'username';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->setFrom('info@test.com', 'test');
$mail->addAddress('test@test.com');     // Add a recipient


$mail->isHTML(true);                                  // Set email format to HTML
$mail->AddEmbeddedImage('../img/text_dark.png', 'cs');
$mail->Subject = 'Hi Customer!';

$body = 'test';

$mail->Body = $body;

$mail->Send();
header("Location:../memberIndex.php");
exit();
?>

wrap $mail->send() at least (try...catch would be better) in a if block like:

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    header("Location:../memberIndex.php");
}

to get a clue whats maybe going wrong. Also have look into PHP header manual. Most important is not to have any output before sending a header. No whitspace, no error, no nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM