How to attach a file in direct PHPMailer with URL?

1

Today I have a problem and it is as follows, I am using PHPMailer and I am trying to attach a file that is in the following path link and my code is as follows:

$url = 'http://app.pimsaseguros.com/_files/_img/_holidays/040616-160454_img001.pdf';
$mail->addAttachment($url,'solicitud.pdf');

and if you send me the email but without the attachment.

    
asked by Ing. Marquez Adam 07.04.2016 в 21:25
source

2 answers

1
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 08.04.2016 / 21:39
source
1

In the PHPMailer class code ( online source on GitHub ) , you can see that in the definition of addAttachment it says that the attached file must have a path in the file system and that it returns false if the file can not be opened:

* Add an attachment from a path on the filesystem.
* Returns false if the file could not be found or read.

That's why you do not add the attached file (you pass a URL) and it fails silently. It really is not silent, just do not check if it fails or not, you should add something to check if the result is false or not:

if (!$mail->addAttachment($url,'solicitud.pdf')) {
    // código si el archivo no se pudo abrir o leer
}

If the file you want to attach is on a remote server or is a URL, then instead of using addAttachment you could use addStringAttachment . In the method definition is specified (my translation):

* Añade una cadena o un binario como adjunto (no en el sistema de archivos)
* Este método puede ser usado para adjuntar ascii o datos binarios
* como por ejemplo un registro BLOB de una base de datos.

One solution to attach the file would be to combine the function file_get_contents (which read a file as string) with addStringAttachment (I have not tested the code, it may contain bugs and it would be good if you added some kind of validation for both functions):

$url = 'http://app.pimsaseguros.com/_files/_img/_holidays/040616-160454_img001.pdf';
$fichero = file_get_contents($url);
$mail->addStringAttachment($fichero, 'solicitud.pdf');
    
answered by 08.04.2016 в 04:59