-2

i cannot seem to make the header function work, I need to redirect to a thank you page after sending the data to my email. The form action goes to rd-mailform.php and I cannot make a redirect from there

How can I do it? I would love to get some help, I cannot make it work

Thanks

This is rd-mailform.php:

<?php

$recipients = 'my@email.com';

try {
require './phpmailer/PHPMailerAutoload.php';

preg_match_all("/([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)/", $recipients, $addresses, PREG_OFFSET_CAPTURE);

if (!count($addresses[0])) {
    die('MF001');
}

if (preg_match('/^(127\.|192\.168\.)/', $_SERVER['REMOTE_ADDR'])) {
    die('MF002');
}

$template = file_get_contents('rd-mailform.tpl');

if (isset($_POST['form-type'])) {
    switch ($_POST['form-type']){
        case 'contact':
            $subject = 'A message from your site visitor';
            break;
        case 'subscribe':
            $subject = 'Subscribe request';
            break;
        case 'order':
            $subject = 'Order request';
            break;
        default:
            $subject = 'A message from your site visitor';
            break;
    }
}else{
    die('MF004');
}

if (isset($_POST['email'])) {
    $template = str_replace(
        array("<!-- #{FromState} -->", "<!-- #{FromEmail} -->"),
        array("Email:", $_POST['email']),
        $template);
}else{
    die('MF003');
}

if (isset($_POST['message'])) {
    $template = str_replace(
        array("<!-- #{MessageState} -->", "<!-- #{MessageDescription} -->"),
        array("Message:", $_POST['message']),
        $template);
}

preg_match("/(<!-- #{BeginInfo} -->)(.|\n)+(<!-- #{EndInfo} -->)/", $template, $tmp, PREG_OFFSET_CAPTURE);
foreach ($_POST as $key => $value) {
    if ($key != "email" && $key != "message" && $key != "form-type" && $key != "g-recaptcha-response" && !empty($value)){
        $info = str_replace(
            array("<!-- #{BeginInfo} -->", "<!-- #{InfoState} -->", "<!-- #{InfoDescription} -->"),
            array("", ucfirst($key) . ':', $value),
            $tmp[0][0]);

        $template = str_replace("<!-- #{EndInfo} -->", $info, $template);
    }
}

$template = str_replace(
    array("<!-- #{Subject} -->", "<!-- #{SiteName} -->"),
    array($subject, $_SERVER['SERVER_NAME']),
    $template);

$mail = new PHPMailer();
$mail->From = $_POST['email'];

if (isset($_POST['name'])){
    $mail->FromName = $_POST['name'];
}else{
    $mail->FromName = "Site Visitor";
}

foreach ($addresses[0] as $key => $value) {
    $mail->addAddress($value[0]);
}

$mail->CharSet = 'utf-8';
$mail->Subject = $subject;
$mail->MsgHTML($template);
$mail->send();

die('MF000');
} catch (phpmailerException $e) {
die('MF254');
} catch (Exception $e) {
die('MF255');
}
  • adding `header('Location: thankyoupage.php');` at the very bottom of your script doesn't work? – Jorden Nov 22 '17 at 00:38
  • header must be called before any out put. [header](http://php.net/manual/en/function.header.php) – Hameed Nov 22 '17 at 01:07

1 Answers1

0

You should simply use header function: http://php.net/manual/en/function.header.php

Do note that you must use this function before any actual output is sent.

Dzontra
  • 80
  • 2
  • 8
  • i'm sorry for this nw question, but where exactly? everywhere I tried it doesn't work. this is my first time at php and don't know where output is sent – Nicolas Braga Nov 22 '17 at 02:12