I am trying to send different emails to users of the application, it turns out that if I send more than 2 it gives me the error:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\project\app\PHPMailer\class.smtp.php:239) in C:\xampp\htdocs\project\app\actionAdmin\ insert-offer.php on line 37
The error refers to a header that I have, but what I don't understand is why with 2 it works correctly, it does the header for me, and if there are 3 the header doesn't do it for me anymore, the application does its job of sending the mails correctly, but it does not redirect me. I don't really know what to do anymore. Goodnight
foreach( $email as $email ){
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "corominesempleo20";
$mail->SetFrom($email['email']);
$mail->Subject = $titulo;
$mail->Body = $cuerpo;
$mail->AddAddress($email['email']);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
$carpeta = "../actionAdmin/files/";
opendir($carpeta);
$destino = $carpeta.$_FILES['imagen']['name'];
copy($_FILES['imagen']['tmp_name'],$destino);
$nombreImg = $_FILES['imagen']['name'];
$administrador->agregarOferta($titulo,$empresa,$email,$sector,$localidad,$descripcion,$nombreImg,$destino);
$administrador->getEmail('Oferta de Empleo nueva','Estos son los datos de la oferta');
$_SESSION['insertado'] = 'insertado';
header("Location: ../views/panel-admin.php");
exit();
}
You can't use
header()
after you have sent content to the browser and you have aecho
after sending each message.Yes, because in some configurations PHP has an output buffer enabled, where it stores content until it completes a certain amount of data.
When the buffer is full, PHP sends the output, including headers, either text/plain or text/html, so the browser knows what to display.
In the previous step the buffer was filled, content (including headers) was sent and that's why you get the error message.
Solution? Read this answer and try to adapt it to your needs.