0

I am having problems getting php to redirect after processing an email message. I've tried all of the solutions mentioned on this page, as well as some involving javascript with no luck. Here are the relevant portions of my code:

function redirect($url) {
ob_start();
header('Location: '.$url);
ob_end_flush();
exit();
}

if ( isset($_REQUEST['sendemail']) ) {
header("Content-Type: text/plain");
header("X-Node: $hostname");
$from = $_REQUEST['from'];
$name = $_REQUEST['name'];
$toemail = "djsuson@gmail.com";
$subject = "Customer question";
$message = $_REQUEST['message'];

ob_start(); //start capturing output buffer because we want to change output to html

$mail = new PHPMailer;

$mail->SMTPDebug = 2;
$mail->IsSMTP();
if ( strpos($hostname, 'cpnl') === FALSE ) //if not cPanel
    $mail->Host = 'relay-hosting.secureserver.net';
else
    $mail->Host = 'localhost';
$mail->SMTPAuth = false;

$mail->From = $from;
$mail->FromName = $name;
$mail->AddAddress($toemail);

$mail->Subject = $subject;
$mail->Body = $message;

$mailresult = $mail->Send();
$mailconversation = nl2br(htmlspecialchars(ob_get_clean()));
if ( !$mailresult ) {
    echo 'FAIL: ' . $mail->ErrorInfo . '<br />' . $mailconversation;
    redirect('http://otterlywoods.com/failure.html');
} else {
    echo $mailconversation;
    redirect('http://otterlywoods.com/success.html');
}

Any help on this would be appreciated.

Dan Suson
  • 11
  • 1
  • 1
    If you show PHP errors while coding or look at error log if you are on production site, you should see [headers already sent error message](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php). Headers must be sent before echo out any body message. You should re-design your process (read the link). – vee Jun 16 '22 at 15:18
  • _"I am having problems"_ - isn't helpful in any way, it's pretty much goes without saying since you're posting a question here. Please always add what actually happens. Also do some proper debugging to narrow the issue down. Do you get any errors/warnings (check the web servers error log). Does anything in that code work? If yes, where exactly does it stop working? – M. Eriksson Jun 16 '22 at 15:19
  • @M.Eriksson: In response to your comment, the code is working fine, with the exception of not redirecting. I get the created email from the web page, no errors are generated that I can find, and I don't see any errors/warnings when I look at the logs made available by goDaddy. It just isn't executing the redirect after sending the email. I have tried to echo out information to try and debug this, but haven't gotten anything that I've found on the web page. – Dan Suson Jun 16 '22 at 18:43
  • So far I have tried - using the redirect() call as shown in the code above - putting the header() call directly in the script where the redirect() call is - creating a variable outside of the nested if statement, putting 'OK'/'FAIL' in the variable depending on what $mailresult contains, and redirecting at the end of the function immediately before the exit() - placing a javascript redirect in the javascript that parses the form output when the send button is clicked - using a variable to capture the request.statusText value to print out/trigger the redirect None of these have worked – Dan Suson Jun 16 '22 at 18:50
  • Remove the other headers. It makes no sense to have a content type when you're going to redirect the user away. You can also try to remove your `ob_*`-functions since I don't see the purpose of them in this case. And your code will call `ob_start()` twice, which could cause issues when flushing. Then you also need to remove your `echo`'s since you can't have _any_ output _at all_ before you call `header()`. Also disable SMTPDebug for PHPMailer (as that generates output as well if enabled.) Read the link @vee posted. – M. Eriksson Jun 16 '22 at 19:43
  • @M.Eriksson, I commented out the ob-* lines in the redirect function, the two header lines after the if ( isset($_REQUEST['sendemail']) ) {, the ob_start after elseif ( $_REQUEST['sendmethod'] == "smtp" ), and echo lines in the mailresult logic statement. It did not change the result. I still got the email but was not redirected off of the page. – Dan Suson Jun 17 '22 at 01:59
  • When writing your code, I recommended to [display all errors (1)](https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings) , [(2)](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). If `SMTPDebug` is not set to off (`0`), when you call `send()` they should echo out the debug message and this body message is out before `header()` which is wrong (as I said before). – vee Jun 17 '22 at 03:58
  • I did everything that @M.Eriksson said above and everything works fine. 1. remove `ob_*` functions inside `redirect()` function as it is no need. 2. Check your open and close `{...}` for `if` condition. It doesn't matched in the code above. 3. `$hostname` is come from no where it should showing undefined variable error. Set this variable or remove it. 4. Remove and `echo` before `header()`. That's all. And enable all errors as I said before. – vee Jun 17 '22 at 04:13
  • I commented out all of the echo and header lines that are in area that I've changed. The mailing code is using a PHPMailer class that appears to set some others. The script that I got and modified from goDaddy also has some Javascript, which creates a table showing the results of the mail call. This is showing the redirect as part of the result. Is there any way of sharing the full code and/or (preferably) taking some of this private? – Dan Suson Jun 24 '22 at 02:02

0 Answers0