简体   繁体   中英

Redirecting contact form in SwiftMailer to success page doesn't work

I don't know a thing about php but with little help and research I managed to set up working contact form with Swift Mailer. It's sending messages alright but I want it to redirect people, after sending message, to thank_you.html page. I tried everything I could but it does not work. Could you help me? Here is the code:

<?php

include("Swift/lib/swift_required.php"); 
$host = 'xxxxxx.xxxxxx@gmail.com';
$password = 'xxxxx'; 
$subject = "zapytanie ze strony"; 
$body = "Zglaszajacy: ".$_POST["fullname"]."\r\n";
$body .= "Telefon: ".$_POST["phone"]."\r\n";
$body .= "E-mail: ".$_POST["email"]."\r\n";
$body .= "Tresc: ".$_POST["description"]."\r\n";

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

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

$message = Swift_Message::newInstance($subject)
->setFrom(array($host => 'Klient')) 
->setTo(array('xxxxxxx@xxxx.gmail.com'=> 'xxxxxx'))
->setBody($body);

$result = $mailer->send($message); 
if ($result) 
{ 
    header('Location: http://www.xxxxx.org/thank_you.html'); 
}
echo $result;


?>

I added that part myself:

if ($result) 
{ 
    header('Location: http://www.xxxxx.org/thank_you.html'); 
}

and it does not work. Mail is sent but nothing is happening with form. It just stays there. Please treat me like a total layman in this one ;)

From the php documentation for header() :

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.

It's likely that some whitespace is being output either by your code or the included swift mailer code before reaching your call to header .

Try using ob_start() at the start of your code, then ob_end_flush() at the end.

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