0

I would like my form (based on Swift Mailer) to redirect to a link by using header();

Problem is I just can't seem to find out where to place the header(); part so that the form is successfully sent and the person is redirect. This is the current code:

<?php

    if(isset($_POST['email'])) {
        // Require the Swift Mailer library
        require_once 'lib/swift_required.php';


        $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl' )
          ->setUsername('xxxx')     
          ->setPassword('xxxx')
          ;


        $mailer = Swift_Mailer::newInstance($transport);



        foreach ($_POST as $key => $value)
            $messageText .= ucfirst($key).": ".$value."\n\n";



        $message = Swift_Message::newInstance('A message from Pivot Template Form')
          ->setFrom(array($_POST['email'] => $_POST['name']))
          ->setTo(array('email@email.com' => 'John Doe'))->setBody($messageText);

        try{
            echo($mailer->send($message));
        }
        catch(Exception $e){
            echo($e->getMessage());
        }
        exit;
    }

?>

Anybody have an idea as to where to add the line?

sjagr
  • 15,983
  • 5
  • 40
  • 67
bobs
  • 1

1 Answers1

0

You're likely having issues figuring this out because of this portion of your code:

try{
    echo($mailer->send($message));
}
catch(Exception $e){
    echo($e->getMessage());
}
// This is where you're probably trying to put header()
exit;

The issue is that by using echo, PHP automatically sends headers to the browser, rendering any other header function useless as the headers have already been sent along with the output. From the docs:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

So you must use a different strategy to convey the message back to the user. Likely something like:

try{
    $mailer->send($message);
    header('Location: mydesiredpage.php?success=1');
}
catch(Exception $e){
    echo($e->getMessage());
}
exit;

And then checking the $_GET['success'] variable in the next page to convey the message to the user.

sjagr
  • 15,983
  • 5
  • 40
  • 67
  • I have used the code now exactly how you explained. Now the message is still sent. But the "message successful" text is not showing up and you're also not redirected. Basically for the user nothing happens. This is the code now: try{ $mailer->send($message); header('Location: http://www.google.com'); } catch(Exception $e){ echo($e->getMessage()); } exit; @sjagr – bobs Nov 07 '14 at 09:26
  • @bobs Similar to the `href` value of an anchor link, you **have** to use a fully correct http format if you're sending someone to a domain outside of your own: `header('Location: http://google.com');` I remind you from my own answer too that you must come up with your own strategy to convey the message back to the user - you will **not** be able to show a message when using a `header` redirect. – sjagr Nov 07 '14 at 14:12
  • @bobs If you _do_ want to show a message on the page while having a redirect happen after a certain delay of time, use a `meta` refresh attribute: `` – sjagr Nov 07 '14 at 14:14