How to send a pdf with PHPMailer

0

I have the following code that generates a pdf

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 10);
$pdf->Image('../../imagenes/3.png' , 1 ,-5, 60 , 50,'PNG');
$pdf->SetFont('Arial', 'B', 15);
$pdf->Ln(15);
$pdf->Cell(120, 18, 'SOLICITUD DE COTIZACION', 0);
$pdf->SetFont('Arial', '', 10);
$pdf->SetXY(160,18);
$pdf->MultiCell(0,3.5,'FOLIO: '.$idCotizacion,0); 
$pdf->SetXY(160,25);
$pdf->MultiCell(0,3.5,'FECHA: '.$fechaCotizacion,0); 
$pdf->Ln(15);
$pdf->SetFont('Arial', 'B',10);
$pdf->Cell(100,10,"DATOS DEL SOLICITANTE");
$pdf->Ln(5);
$pdf->SetFont('Arial', '',10);
$pdf->Cell(100,10,'Nombre: '.$contacto);
$pdf->Ln(5);
$pdf->Cell(100,10,'Telefono: '.$numero);
$pdf->Ln(5);
$pdf->Cell(100,10,'Correo: '.$email);
$pdf->Ln(5);
$pdf->SetFont('Arial', 'B',10);
$pdf->SetXY(120,45);
$pdf->MultiCell(0,3.5,'DATOS DE LA RUTA ',0);
$pdf->SetFont('Arial', '',10);
$pdf->SetXY(120,50);
$pdf->MultiCell(0,3.5,'Origen: '.$origen); 
$pdf->SetXY(120,60);
$pdf->MultiCell(0,3.5,'Destino: '.$destino);
$pdf->SetXY(120,70);
$pdf->MultiCell(0,3.5,'Kilometros: '.$km); 
$pdf->SetXY(10,80);
$pdf->SetFont('Arial', 'B',10);
$pdf->MultiCell(0,3.5,'FECHA DE SERVICIO');
$pdf->SetFont('Arial', '',10);
$pdf->SetXY(10,85);
$pdf->MultiCell(0,3.5,'Fecha de inicio: '.$fechaInicio);
$pdf->SetXY(10,90);
$pdf->MultiCell(0,3.5,'Hora de inicio:  '.$horaInicio);
$pdf->SetXY(10,95);
$pdf->MultiCell(0,3.5,'Fecha de termino: '.$fechaTermino);
$pdf->SetXY(10,100);
$pdf->MultiCell(0,3.5,'Hora de termino:  '.$horaTermino);
$pdf->SetXY(10,105);
$pdf->MultiCell(0,3.5,'Dias de duracion:  '.$duracion);
$pdf->SetXY(120,80);
$pdf->MultiCell(0,3.5,'Numero de Plazas:  '.$numeroPlazas);
$pdf->SetXY(120,90);
$pdf->MultiCell(0,3.5,'Comentarios:  '.$comentarios);
$pdf->Ln(5);
$pdf->Output();

Which works well, what I want to do is that after the pdf has been generated send it to an email with PHPMailer but I do not know how to attach it.

I use this code in the ouput:

$pdf->Output($idCotizacion.".pdf","F");
and then with these lines

$mail->addAttachment( $idCotizacion.'.pdf' );
$mail->Send();

I do the sending, if you keep the pdf but you do not send me the mail, the code to send is this:

$mail=new PHPMailer();
$mail->Mailer="smtp";
$mail->Helo = "www.gmail.com"; //Muy importante para que llegue a hotmail y otros
$mail->SMTPAuth=true;
$mail->Host="smtp.gmail.com";
$mail->Port=25; //depende de lo que te indique tu ISP. El default es 25, pero nuestro ISP lo tiene puesto al 26
$mail->Username="[email protected]";
$mail->Password="********";
$mail->From="[email protected]";
$mail->FromName="Jose Luis";
$mail->Timeout=60;
$mail->IsHTML(true);

//Enviamos el correo
$mail->AddAddress('[email protected]'); //Puede ser Hotmail
$mail->Subject='tu asunto';
    
asked by Jose Luis GP 04.04.2017 в 23:30
source

2 answers

1

First generate your pdf by sending it to a variable:

$archivoPdf = $pdf->Output('','S'); 

Then attach it as base64:

$mail->AddStringAttachment($archivoPdf,'nombreX.pdf','base64');

That way you do everything on the fly and you do not take up any disk space.

    
answered by 05.04.2017 / 17:41
source
0

What you have to do is save it on your server or temporarily, then add it to the message as an attachment with its respective command.

$mail->addAttachment('/var/tmp/file.pdf');
    
answered by 04.04.2017 в 23:56