1

I am trying to use PHPMailer but I get a 500 internal server error, but the weird thing is that I do still get the mail in my inbox. This is the code I am using (stars for my password for security reasons)

    <?php
require 'PHPMailerAutoload.php';

$name = htmlspecialchars($_POST['name']);
$subject = htmlspecialchars($_POST['subject']);
$txt = $_POST['content'];
$email = strtolower($_POST['email']);

$mail = new PHPMailer;

//$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'wo***oom@gmail.com';                 // SMTP username
$mail->Password = '***';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->setFrom($email, 'Mailer');
$mail->addAddress('wo***oom@gmail.com');     // Add a recipient

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $subject;
$mail->Body    = 'Email van: <b>'. $name .'</b>.<br> Bericht:<br>'. $txt .'';
$mail->AltBody = 'Email van: '. $name .'. Bericht: '. $txt .'';

if(!$mail->send()) {
    echo 'Bericht is niet verstuurd.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    header("contact.php");
}
header("contact.php");
?>

I get the mail with the right headers, content and sender in my inbox but the error stays

wouter1246
  • 21
  • 2
  • 2
    Check your apache / nginx web server logs to get the details of what caused the 500. [edit] your post to include these error logs. – Matt Clark Nov 20 '17 at 21:23
  • 1
    Why do you use `header` like you do after the email was sent? Shouldn't you add `Location: ` ? – Alon Eitan Nov 20 '17 at 21:29
  • I'm sorry, I'm not skilled with server handling and the only logs I find are referencing the robot.txt and various images, there is a file with error_logs but those are all empty – wouter1246 Nov 20 '17 at 21:47
  • II'd be tempted to change your code to `header('Location: contact.php\n\n');` and remove the redirect right at the very bottom before your closing `?>` tag. Your `else` block is doing the redirection, and you're doing it again further down. Also - if you _do_ get an error - you're echoing the error, and then telling it to redirect - is that the sort of behaviour you want it to do? – ash Nov 20 '17 at 21:50
  • 1
    You are right, my header placement was wrong and I replaced it with header('Location: contact.php'); – wouter1246 Nov 20 '17 at 21:55

0 Answers0