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 .