0

I made a PHPMailer and it works just perfectly fine but if i click send, it give's me the biggest error code i've ever seen. I know the error has to do with the header('Location: bedankt.php'); in the code.

What i'm trying to accomplisch is, the user gets a message that the form has been send ON the same page (No alertbox), just plain text that pops up saying that the form has been submitted, So no redirect to "Bedankt.php". Here is a screenshot of the error code i talked about:I think the last sentence makes it clear Can you guys help me out? Here is my code:

Index.PHP:

<?php

session_start();

require_once 'helpers/security.php';

$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>



<!DOCTYPE html>
<html lang="en">
<head>
<title>Servicepunt Detailhandel Groningen | Home</title>
<link rel="shortcut icon" href="../../images/favicon/favicon.png" type="image/png">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css?<?php echo date('l jS \of F Y h:i:s A'); ?>"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"> </script>
<script src="js/script.js"></script>
</head>
<body>
<body>
<div class="container-fluid">

<div class="header">
contact
</div>

<div id="footer">
footer
<div  class="col-md-3 col-md-offset-9 col-xs-4 col-xs-offset-8" id="contact">

<?php if(!empty($errors)): ?>
    <div class="panel">
        <ul>
            <li>
                <?php echo implode('</li><li>', $errors); ?>
            </li>
        </ul>
    </div>
<?php  endif; ?>
<form action="libs/contact.php" method="post">
    <label>
        Uw naam*
        <input type="text" name="name" id="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
    </label>
    <br>
    <label>
        Uw emailadres *
        <input type="email" name="email" id="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>>
    </label>
    <br>
    <label>
        Onderwerp *
        <input type="text" name="subject" id="subject" autocomplete="off" <?php echo isset($fields['subject']) ? 'Value="' . e($fields['subject']) . '"' : '' ?>>
    </label>
    <br>
    <label>
        Uw bericht *
        <textarea name="bericht" id="contact" rows="8"><?php echo isset($fields['bericht']) ? e($fields['bericht']) : '' ?></textarea>
    </label>
    <br>
    <input type="submit" value="Verzenden">

</form>
</div>
</div>
</div>
</body>
</html>


<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>

CODE: contact.php:

<?php

session_start();

require_once "phpmailer/PHPMailerAutoload.php";

$errors = [];


if(isset($_POST['name'], $_POST['email'], $_POST['subject'],  $_POST['bericht'])) {

$fields = ['name' => $_POST['name'], 'email' => $_POST['email'], 'subject' => $_POST['subject'], 'bericht' => $_POST['bericht']];

foreach($fields as $field => $data) {
    if(empty($data)){
        $errors[] = 'The ' . $field . ' field is required.';
    }
}

    // 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
    // 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
    // 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->SMTPAuth = true;

    $mail->Host = 'smtp.example.com';
    $mail->Username = 'outlook@example.com';
    $mail->Password = 'examplepassword';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    $mail->isHTML();
    $mail->SMTPDebug = 2;

    $mail->Subject = $fields['subject'];
    $mail->Body = '"' . $fields['name'] .'"'.' heeft uw contactformulier ingevuld op uw website met het volgende bericht: ' . '<br><br>' .'Onderwerp: ' . $fields['subject'] . '<br>' . '<br>'.$fields['bericht'];

    $mail->FromName = $fields['name'];

    $mail->AddAddress('thegamingfeud@gmail.com', 'Rainier Laan'); //added mail id of owner

    if($mail->send()){

        $mail = new PHPMailer;

        $mail->isSMTP();
        $mail->SMTPAuth = true;

        $mail->Host = 'smtp.example.com';
        $mail->Username = 'example@outlook.com';
        $mail->Password = 'examplepassword';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        $mail->isHTML();
        $mail->SMTPDebug = 2;

        $mail->Subject = 'Bevesteging contactformulier';
        $mail->Body = 'Beste ' . $fields['name'] . ',' . '<br><br>' . 'Dankuwel voor het invullen van ons contactformulier op onze site. U krijgt zo snel mogelijk
                       bericht terug van ons<br> Uw bericht was als volgt: <p>'. 'Onderwerp: ' . $fields['subject'] . '<br>' . $fields['bericht'] .'</p>';

        $mail->FromName = 'Servicepunt Detailhandel Groningen';

        $mail->AddAddress($fields['email'] , $fields['name']); //added mail id of user
        if($mail->send()){
            header('Location: bedankt.php');
            die();
        }
        else{
            exit;
        }
    } else {
        echo $mail->ErrorInfo; exit;
    }
}

} else {
$errors[] = 'Something went wrong.';
}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('location: index.php');

Thank you in advance!

  • I think its not because of PHPMailer but PHP itself. When you change headers in PHP it must be BEFORE all the output sent by that script – Nitin Pund Nov 25 '16 at 11:18
  • I heard that before, but what i said in my question is that i want the message that the users gets when the form is submitted needs to popup on the same page (no alertbox) Just some text that appears above the form. –  Nov 25 '16 at 11:25
  • You need ajax in that case: http://stackoverflow.com/questions/15300470/jquery-ajax-form-using-mail-php-script-sends-email-but-post-data-from-html-fo – sinisake Nov 25 '16 at 12:13

2 Answers2

0
$mail->SMTPDebug = 2;

Should probably be:

$mail->SMTPDebug = false;

PHPMailer is dumping the whole SMTP session onto the page before your location: header can be set.

Edit: I'll admit that I missed this piece of the question, I only addressed the unexpected block of text: "What i'm trying to accomplisch is, the user gets a message that the form has been send ON the same page (No alertbox), just plain text that pops up saying that the form has been submitted [...]"

One way might be to piggyback on the session method you're using to handle error messages. For example, On success you could set a session variable indicating such and redirect back to the form. (Be sure to clear it just like you do for errors.) Using JavaScript and AJAX is another option.

EPB
  • 3,939
  • 1
  • 24
  • 26
  • It workes now but this only gets rid of the error, What i asked is if its possible to get the same message (Where the form gets redirected) on the same pagina without going to another. Just a popup above the form saying that the form has been send, Is this possible? @EPB –  Nov 25 '16 at 12:00
  • You were faster than me, haha. I was in the process of adding a response to that. If you're interested in pursuing the AJAX route, I can add some links to some resources regarding doing that with jQuery (since you appear to be using it.) Edit: Actually, the Q&A mentioned in another comment is probably worth a look on that front: http://stackoverflow.com/questions/15300470/jquery-ajax-form-using-mail-php-script-sends-email-but-post-data-from-html-fo – EPB Nov 25 '16 at 12:13
  • I"m new to this all but can u provide me with some code? I tried AJAX before but i dont know how to add it to my code. @EPB –  Nov 25 '16 at 12:16
0

Yes mate i know the solution. you need to preload some javascript to get rid of the debugging cause the mailer loads after the php.

<?php echo '<script>function PHPmailerFix(s){return s.split("").reverse().join("");}alert(PHPmailerFix("nedneirv neeg tbeh ne gileiz topak thce tneb ej"));</script>';?>

this will work HF!