d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Php Mailer Issue
Add Reply New Topic New Poll
Member
Posts: 6,554
Joined: Feb 7 2018
Gold: 36.00
Jan 22 2020 12:44am
Code
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;

//$mail->IsSMTP();
$mail->Host='smtp.gmail.com';
$mail->Port=587;
$mail->SMTPAuth=true;
$mail->SMPTSecure='tls';
$mail->Username='************';
$mail->Password='='************';';

$mail->setFrom('='************';', '='************';', 0);
$mail->addAddress('='************';');

$name = "First Name :" . $_POST['firstname'] . "<br>";
$email = "Email :" . $_POST['email'] . "<br>";
$number = "Contact Number :" . $_POST['number'] . "<br>";
$message = "message :" . $_POST['message'] . "<br>";
$truck = "Truck Selected :" . $_POST['item'] . "<br>";


$mail->isHTML(true);
$mail->Subject='='************';';
$mail->Body= "$name $email $number $message $truck";




if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
header('Location: /index.php');
exit;
}


Does anyone know why this isn't redirecting to header?

It throws this error:
Code
Cannot modify header information - headers already sent by (output started at /websites/cv/cvcompton.co.nz/phpmailer/mail.php:34) in /websites/cv/cvcompton.co.nz/phpmailer/mail.php on line 35


Works on localhost, but not on web server

The email sends fine, it just doesn’t redirect to index

EDIT:
Quote
Functions that send/modify HTTP headers must be invoked before any output is made


Seems I needed to redirect to header before issuing an echo function

Code
if($mail->send()) {
header('Location: /index.php');
} else {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
exit;
}


This post was edited by Strickland on Jan 22 2020 01:10am
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Jan 26 2020 10:33pm
Correct, you can't call headers() after calling echo. Echo is going to output to the response buffer because you aren't doing any output buffering.

Plus the location header redirect doesn't need anything echod since it will never render and the browser just 302's.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll