]> _ Git - cubeextranet.git/commitdiff
Send mail via Mailjet + fixes. WIP #5051 @3
authorstephen@cubedesigners.com <stephen@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Mon, 13 Jun 2022 16:45:30 +0000 (16:45 +0000)
committerstephen@cubedesigners.com <stephen@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Mon, 13 Jun 2022 16:45:30 +0000 (16:45 +0000)
inc/ws/Controlleur/class.ws.services.php
inc/ws/Util/html5/master/class.ws.html5.compiler.php

index 10d0e83e3a077b85dc1245797f7e8184f99687f1..664409162e6df85ca8fe06ca2d2673db80b2fe18 100644 (file)
@@ -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 <pro@cfoc.com>';
+        $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
 
index 1058b78ae073e5df39151b62305db82c28264d69..0151d4bcc0c72394b6144cc8cec986169b2df17e 100644 (file)
@@ -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;