58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'mail/MailService.php';
|
|
|
|
// This script should be run by a cron job once a week.
|
|
|
|
function get_new_deals_since($date) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT * FROM deals WHERE purchase_date >= ? ORDER BY purchase_date DESC');
|
|
$stmt->execute([$date]);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
function get_subscribed_users() {
|
|
$pdo = db();
|
|
$stmt = $pdo->query('SELECT id, email, username FROM users WHERE receive_weekly_summary = 1');
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
function build_summary_email_body($deals, $username) {
|
|
if (empty($deals)) {
|
|
return [
|
|
'html' => "<p>Hi {$username},</p><p>No new deals were added in the last 7 days.</p>",
|
|
'text' => "Hi {$username},\n\nNo new deals were added in the last 7 days."
|
|
];
|
|
}
|
|
|
|
$html = "<p>Hi {$username},</p><p>Here is a summary of the new deals from the last 7 days:</p><ul>";
|
|
$text = "Hi {$username},\n\nHere is a summary of the new deals from the last 7 days:\n\n";
|
|
|
|
foreach ($deals as $deal) {
|
|
$html .= "<li><strong>" . htmlspecialchars($deal['name']) . "</strong> - $" . htmlspecialchars($deal['price']) . "</li>";
|
|
$text .= "- " . $deal['name'] . " - $" . $deal['price'] . "\n";
|
|
}
|
|
|
|
$html .= "</ul><p>Log in to your account to see more details.</p>";
|
|
$text .= "\nLog in to your account to see more details.";
|
|
|
|
return ['html' => $html, 'text' => $text];
|
|
}
|
|
|
|
$seven_days_ago = date('Y-m-d', strtotime('-7 days'));
|
|
$new_deals = get_new_deals_since($seven_days_ago);
|
|
$subscribed_users = get_subscribed_users();
|
|
|
|
foreach ($subscribed_users as $user) {
|
|
$email_body = build_summary_email_body($new_deals, $user['username']);
|
|
MailService::sendMail(
|
|
$user['email'],
|
|
'Weekly Deal Summary',
|
|
$email_body['html'],
|
|
$email_body['text']
|
|
);
|
|
}
|
|
|
|
echo "Weekly summary emails sent to " . count($subscribed_users) . " users.\n";
|
|
|