I am working with PHP and MYSQL. In my system I have a form for entering requests and in the form options I can attach multiple PDFs that are later sent by mail with the PHPMAILER library. In addition to this, an Excel (PHPSPREADSHEET) is created with the information entered in the request. This Excel I have managed to save in a folder on my system. What I need and for now I have not solved, is that the Excel is sent by mail along with the other files that the user attached. I have searched various sites but have not been able to do so. I would appreciate it if someone could guide me.
//////////////// I CREATE MY EXCEL AND SAVE IT IN THE LOCAL FOLDER OF MY SYSTEM ////////////
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$sheet = $spreadsheet->getActiveSheet()->setTitle('Solicitud');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('./EXCEL/Solicitud 10.xlsx');
//////////////SEND EMAIL WITH THE ATTACHMENTS ENTERED BY THE USER////////////////////
$archivos = $_FILES['archivos'];
$nombre_archivos = $archivos['name'];
$ruta_archivos = $archivos['tmp_name'];
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // Activar autenticación SMTP
$mail->Host = 'XXXXXX'; // SMTP Server
$mail->Username = '[email protected]'; // Cuenta SMTP
$mail->Password = 'xxxxxxx'; // Password de la cuenta
$mail->isHTML(true);
$i = 0;
foreach ($ruta_archivos as $rutas_archivos)
{
$mail->AddAttachment($rutas_archivos,$nombre_archivos[$i]);
$i++;
}
$mail->ClearAllRecipients(); // Elimina direcciones anteriores si se usa iteradamente
$mail->setFrom('[email protected]', 'Sistema');
$mail->addAddress('xxxxx@xxxxxx');
$mail->Subject = 'Solicitud';
$Message = '<html><head>'.PHP_EOL;
$Message .= '</head><body>'. PHP_EOL;
$Message .= '<p>Le informamos que fue ingresada Solicitud,
para <B>'.$nombre.'</B>, según detalles en planilla adjunta </p>'. PHP_EOL;
$Message .= '</body></html>'. PHP_EOL;
$mail->Body = $Message;
$mail->send();
I already solved it, I leave the code, in case someone needs it.
Did you check the generation of the file that it is done correctly? if so, verify that it is in $filename and $filepath before loading the code that sends the mail. In case you always want to attach it beyond what comes from the form that brings the files, you can add it, for that you would do it in the following way.
Cheers
Have you considered saving the excel as a pdf?
in this other post it is shown how to do the step from excel to pdf! How to convert from Excel to PDF