1

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();
?>
rey
  • 11
  • 4
  • Can you post the entire code where you are building your email please ? – Gabriel Diez Aug 09 '17 at 14:39
  • _“Could it be, that the body blocks the redirection?”_ - only if you actually output it, so that i becomes part of the response ... // Please go read [ask], and then edit your question, so that it contains some actually helpful details. – CBroe Aug 09 '17 at 14:41
  • If you have *anything* producing output before you send a Location header, it will fail, and PHP will log an error saying "headers already sent" - so check your log files. PHPMailer does not produce any output unless you ask it to, so either you have some debug output, or you are leaking some other output. – Synchro Aug 09 '17 at 14:46
  • Consider this before you use 'header' .You should not have printed anything – pedram shabani Aug 09 '17 at 14:47
  • I think you need to specify how you build the body of email as that might be the problem here which is not included in your sample code. – Kyborek Aug 09 '17 at 15:01
  • 2
    @Kyborek Even if I put 'test' into the body, it doesn't redirect.. – rey Aug 09 '17 at 15:07
  • 1
    @rey Can you verify (using developer tools) that the server in fact sends you the Location header? Can you verify there is no other output? Can you try using absolute url like this? `header("Location: https://stackoverflow.com/");` – Kyborek Aug 09 '17 at 15:12
  • `$mail->isHTML(true);`... I think it should be `$mail->IsHTML(true);` (uppercase "I") But I don't know if that is the issue... It's the only thing I see. – Louys Patrice Bessette Aug 09 '17 at 15:33
  • Try to wrap it all inside a `try{}` block... Then `catch (phpmailerException $e) { echo $e->errorMessage(); }` – Louys Patrice Bessette Aug 09 '17 at 15:38
  • @Kyborek the only strange thing I can see is the message "Provisional headers are shown" in headers. And that the page calling the header() "stalled". Not familiar with chromes dev Tools but I hope this helps you. – rey Aug 09 '17 at 15:54
  • Look [here](https://stackoverflow.com/a/21814960/2159528) about "Provisional headers". – Louys Patrice Bessette Aug 09 '17 at 16:09
  • An idea... Try to add the embedded image after the message body is defined. – Louys Patrice Bessette Aug 09 '17 at 16:14
  • @LouysPatriceBessette didn't work unfortunately – rey Aug 09 '17 at 16:20
  • Just to make sure the problem isn't coming from PHPMailer... Try `if(!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; }else{echo "PHPMailer ok."; }` – Louys Patrice Bessette Aug 09 '17 at 16:32
  • 1
    @LouysPatriceBessette Thank you! in PHPMailer is everything ok. I guess i have to check my code for whitespaces now. – rey Aug 09 '17 at 16:34

1 Answers1

0

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.

smtw
  • 13
  • 4