prepare("SELECT t.StoreName, o.CartKey, o.ProductName, o.NAME, o.NUMBER, o.Size, o.JerseySize, o.ShortsSize, o.Price, o.Quantity, (o.Price * o.Quantity) AS TotalPrice, ((o.Price * o.Quantity) * 0.10) AS Tax, o.DateCreated, pd.InvoiceNumber, pd.Payer_Email FROM orders AS o INNER JOIN payment_details AS pd ON pd.CartKey = o.CartKey INNER JOIN teamstores AS t ON t.Id = o.StoreId WHERE o.DateCreated BETWEEN '$dateToday 00:00:00' AND '$dateToday 23:59:00' ORDER BY o.DateCreated"); $q->execute(); $result = $q->rowCount(); if($result > 0){ $invoice_array = array(); $filename = 'daily_order_report_'.$dateToday.'.csv'; $headers = array('StoreName', 'ProductName', 'NAME', 'NUMBER', 'Size', 'JerseySize', 'ShortsSize', 'Price', 'Quantity', 'TotalPrice', 'Tax', 'DateCreated', 'InvoiceNumber', 'Payer_Email'); $fp = fopen('daily_order_reports/'.$filename, 'w'); fputcsv($fp, $headers); while ($row = $q->fetch()) { $value = $row['CartKey'] . "|" . $row['InvoiceNumber']; if(!in_array($value, $invoice_array, true)){ array_push($invoice_array, $value); } $lineData = array($row['StoreName'], $row['ProductName'], $row['NAME'], $row['NUMBER'], $row['Size'], $row['JerseySize'], $row['ShortsSize'], $row['Price'], $row['Quantity'], $row['TotalPrice'], $row['Tax'], $row['DateCreated'], $row['InvoiceNumber'], $row['Payer_Email']); fputcsv($fp, $lineData); } $report_link = (getenv('APP_URL') ?: 'https://www.crewsportswear.com') . "/email_reports/daily_reports_with_image.php?d=". $dateToday; $body = ""; $body .= ""; $body .= "

Please download the attached file for today's order report.

"; $body .= "

"; $body .= "

For the order report with image please click here.

"; $body .= "

####################################

"; $body .= "

Order Details per Invoice

"; foreach($invoice_array as $invoice){ $v = explode("|", $invoice); $admin_url = getenv('ADMIN_URL') ?: 'https://admin.crewsportswear.app'; $body .= "
Invoice Number: " . $v['1'] . "
"; } $body .= ""; $body .= ""; $mail = new PHPMailer(); $mail->IsSMTP(); $mail->SMTPAuth = true; // $mail->SMTPDebug = 3; // uncomment for debug mode. $mail->SMTPSecure = 'tls'; $mail->Host = getenv('SMTP_HOST') ?: 'smtp.gmail.com'; $mail->Port = getenv('SMTP_PORT') ?: '587'; //gmail account credentials from environment $mail->Username = getenv('SMTP_USER') ?: 'mail@crewsportswear.com'; $mail->Password = getenv('SMTP_PASS') ?: 'tjpwykpttvkjilxh'; $mail->SetFrom('orders@crewsportswear.com', 'CREW Daily Order Report'); // Recipients from environment or defaults $email_to = getenv('EMAIL_TO') ?: 'graphics@crewsportswear.com'; $mail->addAddress($email_to); $email_bcc = getenv('EMAIL_BCC') ?: 'webmaster@crewsportswear.com,angelo@crewsportswear.com,production@crewsportswear.com'; foreach (explode(',', $email_bcc) as $bcc_email) { $mail->addBCC(trim($bcc_email)); } $mail->addAttachment('daily_order_reports/'.$filename); $mail->Subject = 'Daily Report ' . $dateToday; $mail->Body = $body; $mail->ErrorInfo; $mail->IsHTML(true); // return $mail->Send(); if(!$mail->Send()){ $msg = $dateTimeToday . "\t\t\tMailer Error: " . $mail->ErrorInfo .'\n'; file_put_contents('email.log', print_r($msg, true), FILE_APPEND); }else { $msg = $dateTimeToday . "\t\t\tsuccessfully sent\n"; file_put_contents('email.log', print_r($msg, true), FILE_APPEND); } }else{ $msg = $dateTimeToday . "\t\t\tNo order for today\n"; file_put_contents('email.log', print_r($msg, true), FILE_APPEND); } ?>