From 2077a6d1734563dc1478ab7f65a3c2cf3b42f137 Mon Sep 17 00:00:00 2001 From: "stephen@cubedesigners.com" Date: Mon, 13 Jun 2022 16:45:30 +0000 Subject: [PATCH] Send mail via Mailjet + fixes. WIP #5051 @3 --- inc/ws/Controlleur/class.ws.services.php | 56 ++++++++++++------- .../html5/master/class.ws.html5.compiler.php | 6 +- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/inc/ws/Controlleur/class.ws.services.php b/inc/ws/Controlleur/class.ws.services.php index 10d0e83e3..664409162 100644 --- a/inc/ws/Controlleur/class.ws.services.php +++ b/inc/ws/Controlleur/class.ws.services.php @@ -1502,6 +1502,7 @@ class wsServices extends cubeFlashGateway $details_sheet->setCellValueByColumnAndRow(0, $current_row, $user_detail['label'] . ' :'); $details_sheet->setCellValueByColumnAndRow(1, $current_row, $user_detail['value']); + $details_sheet->getRowDimension($current_row)->setRowHeight(-1); // Auto row height from content $current_row++; } @@ -1510,8 +1511,7 @@ class wsServices extends cubeFlashGateway $details_sheet->getColumnDimensionByColumn(1)->setAutoSize(true); //==== Save the Excel spreadsheet to disk temporarily - // $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); // The .xlsx extension seems to prevent the e-mail from delivering... - $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5'); // Older .xls format is fine for our needs + $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); // .xlsx format $xls_file = CubeIT_Files::tempnam(); $writer->save($xls_file); @@ -1524,28 +1524,44 @@ class wsServices extends cubeFlashGateway $user_details['email']['value'], ]; $return_address = 'postmaster@fluidbook.com'; - $email_from = "CFOC <$return_address>"; - $email_reply_to = 'CFOC '; + $email_from_name = "CFOC"; + $email_reply_to = 'pro@cfoc.com'; $email_subject = $_POST['email_subject'] ?? 'Récapitulatif de votre commande CFOC'; $email_body = "Bonjour,\n\nVotre commande est en cours de traitement (récapitulatif en pièce jointe). Nous vous recontacterons dans les meilleurs délais.\n\nCordialement,\n\nL'équipe CFOC"; // Send the email - $mail = new cubeMail(); - $mail->returnPath = $return_address; - $mail->charset = 'UTF-8'; - $mail->to = implode(',', $email_to); - $mail->cc = implode(',', $email_cc); - $mail->from = $email_from; - $mail->replyTo = $email_reply_to; - $mail->subject = $email_subject; - $mail->body = $email_body; - - // Attach the generated spreadsheet file - // I found when using the .xlsx extension, the mail isn't delivered. Interestingly, it's not the actual format - // that causes the problem because a .xlsx file named as .xls would be delivered. It must be a mail server - // configuration but the older version of Excel is fine for our needs, so that's what is used for the format. - $mail->addFile('CFOC-' . date('YmdHis') . '.xls', $xls_file); - $result['success'] = $mail->send(); + $transport = new CubeIT_Mail_Transport_Mailjet(); + $mail = new CubeIT_Mail_Mailjet(); + $mail->setFrom($return_address, $email_from_name); + $mail->setReplyTo($email_reply_to, $email_from_name); + $mail->setReturnPath($return_address); + foreach ($email_to as $to_recipient) { + $mail->addTo($to_recipient); + } + foreach ($email_cc as $cc_recipient) { + $mail->addCc($cc_recipient); + } + $mail->setSubject($email_subject); + $mail->setBodyText($email_body); + + $attachment_filename = 'CFOC-' . date('YmdHis') . '.xlsx'; + + $mail->createAttachment( + file_get_contents($xls_file), + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + Zend_Mime::DISPOSITION_ATTACHMENT, + Zend_Mime::ENCODING_BASE64, + $attachment_filename + ); + + try { + print_r($mail->send($transport)); + $result['success'] = true; + } catch (Exception $e){ + error_log('#### CFOC: Failed sending message via Mailjet ####'); + error_log('ERROR: ' . $e->getMessage()); + $result['success'] = false; + } unlink($xls_file); // Tidy up temporary file after sending diff --git a/inc/ws/Util/html5/master/class.ws.html5.compiler.php b/inc/ws/Util/html5/master/class.ws.html5.compiler.php index 1058b78ae..0151d4bcc 100644 --- a/inc/ws/Util/html5/master/class.ws.html5.compiler.php +++ b/inc/ws/Util/html5/master/class.ws.html5.compiler.php @@ -780,7 +780,7 @@ class wsHTML5Compiler if (file_exists($referencesFile) || CubeIT_Util_Url::isDistant($referencesFile)) { $raw_data = wsUtil::excelToArray($referencesFile); - $first_sheet_rows = reset($raw_data); // First sheet's data will be returned since it's the first array element + $first_sheet_rows = reset($raw_data); // First row's data will be returned since it's the first array element // Expected headings are: EXCLU, LIGNE, EAN, REF, DESIGNATION, COULEUR, QTE MINI, PRIX TTC $column_headings = array_shift($first_sheet_rows); // We assume the first row will be the headings, so we slice it off @@ -799,6 +799,10 @@ class wsHTML5Compiler // For the 'EXCLU' field, this should be converted to a boolean $row['EXCLU'] = ('exclu boutique' === $row['EXCLU']); + // Make sure the PRIX TTC field doesn't have any stray commas or spaces in the numbers + // because this will cause unexpected problems for the calculations + $row['PRIX TTC'] = str_replace([',', ' '], '', $row['PRIX TTC']); + // The EAN and REF are required, so if they don't exist, we can assume it's not a valid row if (empty($row['EAN']) || empty($row['REF'])) { continue; -- 2.39.5