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' => "
Hi {$username},
No new deals were added in the last 7 days.
", 'text' => "Hi {$username},\n\nNo new deals were added in the last 7 days." ]; } $html = "Hi {$username},
Here is a summary of the new deals from the last 7 days:
Log in to your account to see more details.
"; $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";