How to send a pdf created with fpdf by mail using PHPMailer?

0

I happen to have a code that generates pdfs perfectly with the fpd library. But at the end of this, I added another library with its corresponding code to send me this pdf generated by mail with PHPMailer, and what happens is that it sends me the email and the pdf, but empty and when it opens it tells me that the file is damaged and therefore I can not open it. This is my code.

<?php 
     $outmail = $pdf->Output('','FichaCETis26.pdf');


   require("class.phpmailer.php");
   require("class.smtp.php");
   $correo=$_POST['CORREO'];
   $mail = new PHPMailer;

   //Config SMTP
   $mail->isSMTP();
   $mail->CharSet='utf-8';
   $mail->Host='mail.cetis26.edu.mx';
   $mail->SMTPAuth=true;
   $mail->Username='[email protected]';
   $mail->Password='**********';
   $mail->SMTPSecure='TLS';
   $mail->Port=26;
   $mail->setLanguage('es');
   $mail->From = '[email protected]';
   $mail->FromName='Cetis26';
   $mail->Subject='Pase de ingreso al examen otra vez alv.';
   $mail->isHTML(true);
   $mail->Body="Hola, este es tu pase de ingreso al examen.";

   $mail->AddAddress($correo);
   $mail->AddStringAttachment($outmail,'FichaCETis26.pdf');
   $mail->Send();
?>

What I am doing is that, first I generate the pdf and then send it by mail, all in the same file, I just need the pdf to be complete, when I download it and I want to open it, it says it is damaged and it can not be opened I would really appreciate it if you could help me, thank you very much and happy holidays!

    
asked by Oscar Reyes 25.12.2018 в 07:32
source

1 answer

0

I would start by checking that the pdf is in its location, and it is correctly recorded (for example, that you can open it with a pdf reader without problems). I know it seems a truism, but it would not be the first time that a pdf is not recorded on the disk, for example, by a problem of permissions.

Once this is verified, use, instead of AddStringAttachment() , AddAtachment() . It's the one I use and it always works well.

Another possibility is that if the destination email is hotmail or yahoo, you will almost certainly have some problem with the attachments. In my work, we had to adopt the measure of including in the text of the email a link to the PDF on the server, so that if the user could not download it as an attachment, he would do so through that link. These email providers are a bit exaggerated with security.

The code that I use. The values arrive in the signature of a function and are used inside.

$objetoDeCorreo->IsSMTP();
$objetoDeCorreo->SMTPAuth = true;
$objetoDeCorreo->CharSet = 'UTF-8';
$objetoDeCorreo->Host = "127.0.0.1";
$objetoDeCorreo->From = $matrizDeOrigen["correo"];
$objetoDeCorreo->FromName = $matrizDeOrigen["nombre"];
$objetoDeCorreo->isHTML(true);
foreach ($matrizDeDestinatarios as $destino) {
    $objetoDeCorreo->AddAddress(key($destino));
}
$objetoDeCorreo->Subject = $asunto;
$mensaje = $encabezadoGeneral.$cadenaDeMensaje.$pieGeneral;

$objetoDeCorreo->Body = $mensaje;
$objetoDeCorreo->WordWrap = 50;

foreach ($matrizDeFicherosAdjuntos as $adjunto){
    $objetoDeCorreo->AddAttachment($adjunto["fichero"], $name=$adjunto["nombre"]);
}
$resultado = $objetoDeCorreo->Send();

This is working for me for some time now. I hope it helps you.

    
answered by 25.12.2018 в 16:46