Send with phpMailer as attachment a $ pdf created?

1

I clearly state the problem: My .php file does two things: 1) Create a pdf (already verified, which is stored in the variable $ pdf ) and 2) Send an email using phpMailer.

The problem ?: The file $ pdf I can not send it 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 "supposed" $ pdflisto I try to send it by:

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

But, despite the arrival of the email, the attachment does not arrive.

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

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

    
asked by Akyna 04.06.2016 в 20:56
source

5 answers

2

Workaround: use $mail->addStringAttachment($pdfdoc, 'Cliente.pdf'); where the String, does not need the chunk_split . Only that it was a String.

I missed line two of

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

And what is sent is only the $ pdfdoc with the $mail->addStringAttachment. I hope it also works for those who are using phpMailer.

    
answered by 09.06.2016 в 09:47
1

Do not delete the two lines just one

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

should be like this

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

I did it and it worked perfectly

    
answered by 16.05.2018 в 15:16
1

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 = "[email protected]";
$mail->FromName = "fgfgf.com - Factura Virtual";

//To address and name
$mail->addAddress("[email protected]");

//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();
?>
    
answered by 16.05.2018 в 15:20
0

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

I leave you the correct function for this case that you pose:

   $mail->AddAttachment('carpeta_del_fichero_pdf', $name = 'NombreDelFichero',  $encoding = 'base64', $type = 'application/pdf');
    
answered by 05.06.2016 в 11:45
0

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
    
answered by 06.06.2016 в 20:18