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?

4voto

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.

1voto

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

1voto

Ruben D Duno M Points 21

This is my code and it works perfectly.

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

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

 require_once('phpmailer1/class.phpmailer.php');
require_once('phpmailer1/class.smtp.php');
$mail = new PHPMailer();
//From email address and name
$mail->From = "fg@gmail.com";
$mail->FromName = "fgfgf.com - Factura Virtual";

//To address and name
$mail->addAddress("fgf@gmail.com");

//Address to which recipient will reply

//Send HTML or Plain Text email
$mail->isHTML(true);

$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
if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}$mpdf->Output();
?>

0voto

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');

0voto

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;
$mail->MsgHTML($body);
$destinatarios=explode(",", $destinatarios);
if(!empty($destinatarios)){
foreach($destinatarios as $un_destinatario){
$mail->AddAddress($un_destinatario); //destinatarios
}
}else{
return false;
}
if(!empty($archivos_adjuntos_ruta)){
foreach($archivos_adjuntos_ruta as $archivo){
$mail->AddAttachment($archivo); // attachment
}
}
if(!empty($archivos_adjuntos_temp)){
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_ruta=array($path1,path2);
$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.com

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:

X