2 votes

What to send with phpMailer as an attachment a $pdf created?

I explain clearly the problem: My file .php does two things: 1) Creates a pdf (verified already, that is stored in the variable $pdf) and 2)Sends an email using phpMailer.

What the problem?: The file $pdf I can send with $mail->AddAttachment, since $pdf has not had its encode. For which I did the following:

$pdfdoc = $pdf->Output("", "S");
$pdflisto = chunk_split(base64_encode($pdfdoc));

By having a "course" $pdflisto I try to send by:

$mail->AddAttachment($pdflisto, "Cliente.pdf");

But, despite of that comes the email, no attachment.

What I need to know or what do I need help?

What error I am committing, in either the encode or the addAttachment, that through phpMailer does not come with the attachment?


Akyna Points 106

Solution of the problem: use $mail->addStringAttachment($pdfdoc, 'Cliente.pdf'); where String, you do not need the chunk_split. Only for it to be a String.

Obvié the line two of

             $pdfdoc = $pdf->Output("", "S"); 
             pdflisto = chunk_split(base64_encode($pdfdoc));

And what is sent is only $pdfdoc with the $mail->addStringAttachment. I Hope it also serves to those who are using phpMailer.


Ruben D Duno M Points 21

Do not remove the two lines just a

$pdfdoc = $pdf->Output("", "S"); 
pdflisto = chunk_split(base64_encode($pdfdoc));

you should be as well

$pdfdoc = $pdf->Output("", "S");

I did and it worked perfectly


Ruben D Duno M Points 21

This is my code and it works perfectly.

<?php $message =ob_get_contents();
include("../../libs/mpdf/mpdf.php"); //Include mPDF Class 

$mpdf = new mPDF(); 
$emailAttachment = $mpdf->Output('','S');
//$emailAttachment = chunk_split(base64_encode($emailAttachment));    

$mail = new PHPMailer();
//From email address and name
$mail->From = "fg@gmail.com";
$mail->FromName = "fgfgf.com - Factura Virtual";

//To address and name

//Address to which recipient will reply

//Send HTML or Plain Text email

$mail->Subject = "Subject Text";
$mail->Body = $message;
$mail->AltBody = "This is the plain text version of the email content";
$mail->AddStringAttachment($emailAttachment, 'file.pdf', 'base64', 'application/pdf');// attachment
    echo "Mailer Error: " . $mail->ErrorInfo;
    echo "Message has been sent successfully";


Néstor Lim Points 1

it occurs to me that you could save the file temporarily, as we need your path to include it as an attachment in the email.

I leave the function correct for this case you posit:

   $mail->AddAttachment('carpeta_del_fichero_pdf', $name = 'NombreDelFichero',  $encoding = 'base64', $type = 'application/pdf');


abrahamhs Points 2455

Use this function:

function enviar_correo($destinatarios, $mail_asunto, $mail_contendio, $from, $from_name, $archivos_adjuntos_ruta,$archivos_adjuntos_temp){
$mail= new PHPMailer(); // defaults to using php "mail()"
$mail->CharSet = 'UTF-8';
$body= $mail_contendio;
$mail->IsSMTP(); // telling the protocol to use SMTP
$mail->Host = "tu.host.com"; // SMTP server
$mail->From = $from;
$mail->FromName = $from_name;
$mail->Subject = $mail_asunto;
$destinatarios=explode(",", $destinatarios);
foreach($destinatarios as $un_destinatario){
$mail->AddAddress($un_destinatario); //destinatarios
return false;
foreach($archivos_adjuntos_ruta as $archivo){
$mail->AddAttachment($archivo); // attachment
foreach($archivos_adjuntos_temp as $nombrearchivo=>$contenidoArchivo){
$mail->AddStringAttachment($contenidoArchivo,$nombrearch ivo,'base64');
$mail->Timeout = 20;
if($mail->Send()) {
return array(true);
}else {
return array(false,"Mailer Error: ".$mail->ErrorInfo);
$archivos_adjuntos_temp=array(utf8_decode($strfile PDF)=>$strContenidoPdf,utf8_decode($strNomArch)=>$ strContenidoXml);
enviar_correo(...,array(),archivos_adjuntos_temp);//los archivos estan en variables temporales
enviar_correo(...,$archivos_adjuntos_ruta,array()) ;//los archivos estan en rutas en disco
enviar_correo(...,$archivos_adjuntos_ruta,archivos _adjuntos_temp);//ambas opciones al mismo tiempo


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: