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


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.

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

   $mail = new PHPMailer;

   //Config SMTP
   $mail->Username='[email protected]';
   $mail->From = '[email protected]';
   $mail->Subject='Pase de ingreso al examen otra vez alv.';
   $mail->Body="Hola, este es tu pase de ingreso al examen.";


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

1 answer


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->SMTPAuth = true;
$objetoDeCorreo->CharSet = 'UTF-8';
$objetoDeCorreo->Host = "";
$objetoDeCorreo->From = $matrizDeOrigen["correo"];
$objetoDeCorreo->FromName = $matrizDeOrigen["nombre"];
foreach ($matrizDeDestinatarios as $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