We help IT Professionals succeed at work.

PHPMailer redirect to success page

Rassac
Rassac asked
on
Hi, I am using the code below in phpmailer() function...

My messages to get sent, however, the page is not redirected to fail.html or success.html.

If I uncomment the echo, I do get them echoed out on screen in between of lots of jargon text like:"SMTP -> get_lines(): $data was "" SMTP -> get_lines(): $str is "220-smartcar.websitewelcome.com ESMTP Exim 4.69 #1 Tue, 01 Sep 2009 16:48:13 -0500 ".

How could I redirect the page to a success/fail.html?

Thanks
if((!$mail->Send()) || (!$mail2->Send())) {
//  echo "Mailer Error: " . $mail2->ErrorInfo;
header("Location: fail.html");exit;
} else {
//  echo "Message sent!";
header("Location: success.html");exit;
}

Open in new window

Comment
Watch Question

Hi there,

is it possible that you printed anything in the output buffer before
calling header()? What dies the php-error.log say?
What can be found in the resulting page source code?

--TheDoctor

Author

Commented:
hmmm actually i'm echoing nothing before... could it be "error_reporting(E_STRICT)"?

Where do I find php-error.log?
Mark BradyPrincipal Data Engineer

Commented:
If you are using WAMPSERVER the open it and you will find the error log file under 'Php' at the bottom of the popup window. If not using WAMPSERVER then do a search for this file  "php_error.log" it will reside somewhere on your machine.
Mark BradyPrincipal Data Engineer
Commented:
You code looks ok to me. Normally if you have output anything to the browser before you run header("location: somepage.php"); commands it will fail but you should get a warning saying Php headers already sent. I would break down your code a bit and try a simple form test to see if header redirection is actually working on your page first.

Comment out you whole mail sending block and try something like this:

echo "<form action='your_page.php' method='POST'>";
echo "<input type='hidden' name='test' value='1'>";
echo "<input type='submit' value='Test Code'></form>";

$test = $_POST['test'];

if ($test == 1) {
echo "It Worked"
}else{ echo "It Failed";
}

// Now run the page and test it. Once it works then add a redirection.

if ($test == 1) {
echo "It Worked";
header("Location: page1.html")
}else{ echo "It Failed";
header("Location: page2.html");
}

Don't worry about the page1.html and page2.html if the redirection is allowed to work you will just get a page not found error so you will know if header redirection is working on your page.

Just a thought.

Author

Commented:
I changed my error reporting to "error_reporting(E_ALL)" and so now I could see the error message i wanted:
Cannot modify header information - headers already sent by (output started at/email/class.smtp.php:1095)

Any idea how  I can fix this problem please?

Thanks!


Author

Commented:
Below are the lines from 1090 to 1102 but I dont know if I should comment these echoes...

line 1095 is the 1st echo:    
        echo "SMTP -> get_lines(): \$data was \"$data\"" .
                 $this->CRLF;

thanks.
  private function get_lines() {
    $data = "";
    while($str = @fgets($this->smtp_conn,515)) {
      if($this->do_debug >= 4) {
        echo "SMTP -> get_lines(): \$data was \"$data\"" .
                 $this->CRLF;
        echo "SMTP -> get_lines(): \$str is \"$str\"" .
                 $this->CRLF;
      }
      $data .= $str;
      if($this->do_debug >= 4) {
        echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
      }

Open in new window

Author

Commented:
seems this is a debugging from phpmailer... maybe you know from where I can set this to off....

Thanks.
Commented:
Ok managed to solve the problem... I set  $SMTPDebug    = false;  in class.phpmailer.php
Mark BradyPrincipal Data Engineer

Commented:
Excellent job !  I'm glad you got it sorted. As I said earlier, "Headers already sent" means you have echo'd something to the browser previously. In those cases I always put my redirect statements before any other output. If that is not possible for some reason, I use a separate mailing form and have the header("location) statement on the new mailer form.

Anyway, I'm glad you found it.

I have no problem with the question being closed.