Attach several files and send them by phpmailer

1

How can I attach several files (more than two files) using an input file with the PHPMailer library?

So far I have been able to send it, but I need to add more attachments.

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth= true;
//$mail->SMTPSecure = "ssl";
$mail->Host ="dominio";
$mail->Username="[email protected]";
$mail->Password ="@@@@@@@@";
$mail->Port = 25;
$mail->From = $email;
$mail->FromName="Notificaciones - ".$mercancia_para;
$mail->AddAddress(Core::$user->email);
$mail->AddAddress("[email protected]");
//$mail->AddCC("[email protected]");
$mail->AddCC("[email protected]");
$mail->IsHTML(true);

$mail->Subject = $asunto;
$mail->Body = $contenido;
$mail->WordWrap = 50;
$mail->MsgHTML($contenido);

if($mail->Send()){
    $respuesta ="<h2 style='color:#0A7A39;'>Solicitud Realizada con exito!</h2>,  <br> <strong>¡Se ha generado el folio de solicitud: ".$folio_ingreso." y se envio notificación via email!</strong>";
    echo $respuesta;
}else{
    $respuesta ="<div class='alert alert-danger'><strong>Error!!!</strong>  Intenta mas tarde! </div>".$mail->ErrorInfo;
    echo $respuesta;
}

Until now I can send emails, only now I want to attach several files in the shipment.

    
asked by Ever 01.11.2017 в 19:05
source

1 answer

1

To send a file from PHP Mailer , the following line of code is enough:

p>
$mail->AddAttachment();

Example of sending multiple files:

$archivo1 = 'uploads/prueba.pdf';
$archivo2 = 'uploads/prueba.mp3';

$mail->AddAttachment($archivo1,$archivo2);

In case you need to send them from a url, you can do it in the following way:

$url = 'https://www.miweb.com/uploads/archivo.pdf'; //url ejemplo del archivo
$fichero = file_get_contents($url); //Aqui guardas el archivo temporalmente.
$mail->addStringAttachment($fichero, 'solicitud.pdf'); //aqui he usado addStringAttachment para enviar el archivo, en segunda instancia le doy un nombre.

Clarification Instead of using different variables of ($archivo1, $archivo2, $archivo3) you can create an array.

  

UPDATE

Because you tell me you need to know how to send multiple files from a form (input type file) I update my answer.

In the html form:

<form action="dondeseenvian.php" method="POST" enctype="multipart/form-data">
      Enviar estos ficheros:<br />
      <input multiple="true" name="archivos[]" type="file" /><br />
      <input type="submit" value="Enviar ficheros" />
    </form>

If you notice it in input type file I included an attribute called multiple="true" which means it allows you to select several files at the same time.

in the name attribute you can see that I use the [] brackets which allow me to send array list .

In your file where the email is sent (driver)

<?php 
$archivos = $_FILES['archivos'];
$nombre_archivos = $archivos['name'];
$ruta_archivos = $archivos['tmp_name'];

require 'mailsend/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->Host = '[email protected]';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]'; 
    $mail->Password = 'clavesmtp';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    $mail->setFrom('[email protected]', 'Nombre remitente');
    $mail->addAddress('[email protected]');

    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';
    $mail->Subject = 'Asunto archivos adjuntos';
    $mail->Body = "Adjuntos se encuentran los archivos";
    $i = 0;
    foreach ($ruta_archivos as $rutas_archivos) {
        $mail->AddAttachment($rutas_archivos,$nombre_archivos[$i]);
        $i++;
    }

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
 ?>

I first received the file by $_FILES after defining two variables one for the names and one for the temporary path of the file, in the second instance create a foreach cycle to multiply the line $mail->AddAttachment($rutas_archivos,$nombre_archivos[$i]); the number of times that there is a file .

    
answered by 01.11.2017 / 20:26
source