Files
email_reports/send_report.php
Frank John Begornia 1c19c86205
All checks were successful
Deploy Production Email Reports (Unified) / deploy (push) Successful in 57s
Remove unused dbconfig.php include and add DNS resolution tests
2026-01-02 02:03:17 +08:00

157 lines
6.4 KiB
PHP

<?php
// dbconfig.php not needed - using environment variables directly
require("phpmailer/class.phpmailer.php");
date_default_timezone_set('America/Chicago');
$dateToday = date('Y-m-d');
$dateTimeToday = date("Y-m-d H:i:s");
// Determine which brand to process
$brand = getenv('BRAND') ?: 'crew'; // 'crew' or 'merchbay'
// Brand-specific configuration
$config = [
'crew' => [
'db_name' => getenv('DB_NAME_CREW') ?: 'custom_design',
'db_host' => getenv('DB_HOST_CREW') ?: getenv('DB_HOST') ?: 'mysql',
'db_port' => getenv('DB_PORT_CREW') ?: '3306',
'db_user' => getenv('DB_USER_CREW') ?: 'crew_user',
'db_pass' => getenv('DB_PASS_CREW') ?: getenv('DB_PASS'),
'smtp_user' => 'mail@crewsportswear.com',
'smtp_pass' => getenv('SMTP_PASS_CREW') ?: getenv('SMTP_PASS'),
'email_from' => 'orders@crewsportswear.com',
'email_from_name' => 'CREW Daily Order Report',
'email_to' => 'graphics@crewsportswear.com',
'email_bcc' => ['webmaster@crewsportswear.com', 'angelo@crewsportswear.com', 'production@crewsportswear.com'],
'report_url' => 'https://www.crewsportswear.com/email_reports/daily_reports_with_image.php',
'admin_url' => 'https://admin.crewsportswear.app',
'csv_dir' => 'daily_order_reports_crew',
'log_prefix' => '[CREW]'
],
'merchbay' => [
'db_name' => getenv('DB_NAME_MERCHBAY') ?: 'merchbay_laravel',
'db_host' => getenv('DB_HOST_MERCHBAY') ?: getenv('DB_HOST') ?: 'mysql',
'db_port' => getenv('DB_PORT_MERCHBAY') ?: '3306',
'db_user' => getenv('DB_USER_MERCHBAY') ?: 'merchbay_user',
'db_pass' => getenv('DB_PASS_MERCHBAY') ?: getenv('DB_PASS'),
'smtp_user' => 'support@merchbay.com',
'smtp_pass' => getenv('SMTP_PASS_MERCHBAY') ?: getenv('SMTP_PASS'),
'email_from' => 'orders@merchbay.com',
'email_from_name' => 'Merchbay Daily Order Report',
'email_to' => 'graphics@crewsportswear.com',
'email_bcc' => ['webmaster@crewsportswear.com', 'production@crewsportswear.com'],
'report_url' => 'https://www.crewsportswear.com/email_reports_merchbay/daily_reports_with_image.php',
'admin_url' => 'https://merchbay.app',
'csv_dir' => 'daily_order_reports_merchbay',
'log_prefix' => '[MERCHBAY]'
]
];
$cfg = $config[$brand];
// Connect to brand-specific database
try {
$conn = new PDO(
"mysql:host={$cfg['db_host']};port={$cfg['db_port']};dbname={$cfg['db_name']}",
$cfg['db_user'],
$cfg['db_pass']
);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $pe) {
$msg = $dateTimeToday . "\t\t\t{$cfg['log_prefix']} Database error: " . $pe->getMessage() . "\n";
file_put_contents('email.log', print_r($msg, true), FILE_APPEND);
die('Connection error, because: ' .$pe->getMessage());
}
// Query orders
$q = $conn->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();
// Create CSV directory if it doesn't exist
if (!file_exists($cfg['csv_dir'])) {
mkdir($cfg['csv_dir'], 0755, true);
}
$filename = $cfg['csv_dir'] . '/daily_order_report_'.$dateToday.'.csv';
$headers = array('StoreName', 'ProductName', 'NAME', 'NUMBER', 'Size', 'JerseySize', 'ShortsSize', 'Price', 'Quantity', 'TotalPrice', 'Tax', 'DateCreated', 'InvoiceNumber', 'Payer_Email');
$fp = fopen($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);
}
fclose($fp);
// Build email body
$report_link = $cfg['report_url'] . "?d=" . $dateToday;
$body = "<html>";
$body .= "<body>";
$body .= "<p>Please download the attached file for today's order report.</p>";
$body .= "<br><br>";
$body .= "<p>For the order report with image please <a href='".$report_link."'>click here</a>.</p>";
$body .= "<p>####################################</p>";
$body .= "<p>Order Details per Invoice</p>";
foreach($invoice_array as $invoice){
$v = explode("|", $invoice);
$body .= "<div>Invoice Number: <a href='" . $cfg['admin_url'] . "/admin/orders/view/" . $v['0'] . "/print'>" . $v['1'] . "</a></div>";
}
$body .= "</body>";
$body .= "</html>";
// Send email
$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';
$mail->Username = $cfg['smtp_user'];
$mail->Password = $cfg['smtp_pass'];
$mail->SetFrom($cfg['email_from'], $cfg['email_from_name']);
$mail->addAddress($cfg['email_to']);
foreach ($cfg['email_bcc'] as $bcc) {
$mail->addBCC($bcc);
}
$mail->addAttachment($filename);
$mail->Subject = "{$cfg['email_from_name']} - $dateToday";
$mail->Body = $body;
$mail->ErrorInfo;
$mail->IsHTML(true);
if(!$mail->Send()){
$msg = $dateTimeToday . "\t\t\t{$cfg['log_prefix']} Mailer Error: " . $mail->ErrorInfo . "\n";
file_put_contents('email.log', print_r($msg, true), FILE_APPEND);
} else {
$msg = $dateTimeToday . "\t\t\t{$cfg['log_prefix']} successfully sent ({$result} orders)\n";
file_put_contents('email.log', print_r($msg, true), FILE_APPEND);
}
} else {
$msg = $dateTimeToday . "\t\t\t{$cfg['log_prefix']} No order for today\n";
file_put_contents('email.log', print_r($msg, true), FILE_APPEND);
}