diff --git a/includes/Core/Notifications/EmailManager.php b/includes/Core/Notifications/EmailManager.php index fa21fc2..9a6b797 100644 --- a/includes/Core/Notifications/EmailManager.php +++ b/includes/Core/Notifications/EmailManager.php @@ -115,19 +115,33 @@ class EmailManager { * @param WC_Order $order */ public function send_order_processing_email($order_id, $order = null) { + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] send_order_processing_email triggered for order #' . $order_id); + } + if (!$order) { $order = wc_get_order($order_id); } if (!$order) { + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] Order not found for ID: ' . $order_id); + } return; } // Check if event is enabled if (!$this->is_event_enabled('order_processing', 'email', 'customer')) { + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] order_processing email is disabled in settings'); + } return; } + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] Sending order_processing email for order #' . $order_id); + } + // Send email $this->send_email('order_processing', 'customer', $order); } @@ -364,6 +378,10 @@ class EmailManager { * @param array $extra_data */ private function send_email($event_id, $recipient_type, $data, $extra_data = []) { + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] send_email called - Event: ' . $event_id . ', Recipient: ' . $recipient_type); + } + // Get email renderer $renderer = EmailRenderer::instance(); @@ -371,16 +389,27 @@ class EmailManager { $email = $renderer->render($event_id, $recipient_type, $data, $extra_data); if (!$email) { + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] Email rendering failed for event: ' . $event_id); + } return; } + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] Email rendered successfully - To: ' . $email['to'] . ', Subject: ' . $email['subject']); + } + // Send email via wp_mail $headers = [ 'Content-Type: text/html; charset=UTF-8', 'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>', ]; - wp_mail($email['to'], $email['subject'], $email['body'], $headers); + $sent = wp_mail($email['to'], $email['subject'], $email['body'], $headers); + + if (defined('WP_DEBUG') && WP_DEBUG) { + error_log('[EmailManager] wp_mail called - Result: ' . ($sent ? 'success' : 'failed')); + } // Log email sent do_action('woonoow_email_sent', $event_id, $recipient_type, $email); diff --git a/test-email-flow.php b/test-email-flow.php new file mode 100644 index 0000000..3bb0e99 --- /dev/null +++ b/test-email-flow.php @@ -0,0 +1,255 @@ + + + + + Email Flow Diagnostic + + + +

Email Flow Diagnostic Tool

+ +

1. System Status

+ ✓ ENABLED' : '✗ DISABLED'; + echo "
$status - Notification System Mode: $system_mode
"; + + // Check email channel + $email_enabled = get_option('woonoow_email_notifications_enabled', false); + $status = $email_enabled ? '✓ ENABLED' : '✗ DISABLED'; + echo "
$status - Email Channel: " . ($email_enabled ? 'enabled' : 'disabled') . "
"; + + // Check Action Scheduler + $as_exists = function_exists('as_enqueue_async_action'); + $status = $as_exists ? '✓ AVAILABLE' : '⚠ NOT AVAILABLE'; + echo "
$status - Action Scheduler: " . ($as_exists ? 'available' : 'not available (using wp-cron)') . "
"; + ?> + +

2. Event Configuration

+ ✗ FAIL - No events configured!"; + } else { + echo "
✓ PASS - " . count($events) . " events configured
"; + echo "
";
+        foreach ($events as $event_id => $event_data) {
+            $email_enabled = $event_data['channels']['email']['enabled'] ?? false;
+            $status_icon = $email_enabled ? '✓' : '✗';
+            echo "$status_icon $event_id: email " . ($email_enabled ? 'enabled' : 'disabled') . "\n";
+        }
+        echo "
"; + } + ?> + +

3. Email Queue Status

+ get_results( + "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE 'woonoow_mail_%' LIMIT 10" + ); + + if (empty($queued_emails)) { + echo "
⚠ INFO - No emails currently queued
"; + } else { + echo "
✓ INFO - " . count($queued_emails) . " emails in queue
"; + echo "
";
+        foreach ($queued_emails as $email) {
+            $payload = maybe_unserialize($email->option_value);
+            echo "ID: {$email->option_name}\n";
+            echo "To: " . ($payload['to'] ?? 'unknown') . "\n";
+            echo "Subject: " . ($payload['subject'] ?? 'unknown') . "\n";
+            echo "---\n";
+        }
+        echo "
"; + } + ?> + +

4. Action Scheduler Queue

+ get_var( + "SELECT COUNT(*) FROM {$wpdb->prefix}actionscheduler_actions + WHERE hook = 'woonoow/mail/send' AND status = 'pending'" + ); + $failed = $wpdb->get_var( + "SELECT COUNT(*) FROM {$wpdb->prefix}actionscheduler_actions + WHERE hook = 'woonoow/mail/send' AND status = 'failed'" + ); + $complete = $wpdb->get_var( + "SELECT COUNT(*) FROM {$wpdb->prefix}actionscheduler_actions + WHERE hook = 'woonoow/mail/send' AND status = 'complete' + ORDER BY action_id DESC LIMIT 10" + ); + + echo "
✓ INFO - Pending: $pending
"; + echo "
✓ INFO - Completed (last 10): $complete
"; + + if ($failed > 0) { + echo "
✗ WARN - Failed: $failed
"; + + // Show failed actions + $failed_actions = $wpdb->get_results( + "SELECT * FROM {$wpdb->prefix}actionscheduler_actions + WHERE hook = 'woonoow/mail/send' AND status = 'failed' + ORDER BY action_id DESC LIMIT 5" + ); + + if ($failed_actions) { + echo "
Recent failures:\n";
+                foreach ($failed_actions as $action) {
+                    echo "ID: {$action->action_id}\n";
+                    echo "Args: {$action->args}\n";
+                    echo "Scheduled: {$action->scheduled_date_gmt}\n";
+                    echo "---\n";
+                }
+                echo "
"; + } + } else { + echo "
✓ PASS - Failed: 0
"; + } + } else { + echo "
⚠ WARN - Action Scheduler not available, using wp-cron
"; + } + ?> + +

5. Recent Email Logs

+ ⚠ INFO - No email logs found (enable WP_DEBUG to see logs)"; + } else { + echo "
✓ INFO - " . count($email_logs) . " email log entries found
"; + echo "
";
+            echo implode('', array_slice($email_logs, -10)); // Last 10 entries
+            echo "
"; + } + } else { + echo "
⚠ INFO - Debug log not found (enable WP_DEBUG_LOG)
"; + } + ?> + +

6. Test Actions

+ + "; + + if ($_GET['action'] === 'test_email') { + echo "

Testing Email Queue...

"; + + // Queue a test email + $test_payload = [ + 'to' => get_option('admin_email'), + 'subject' => 'WooNooW Test Email - ' . date('Y-m-d H:i:s'), + 'html' => '

Test Email

This is a test email from WooNooW email queue system.

Time: ' . date('Y-m-d H:i:s') . '

', + 'headers' => ['Content-Type: text/html; charset=UTF-8'], + 'attachments' => [], + ]; + + \WooNooW\Core\Mail\MailQueue::enqueue($test_payload); + + echo "

✓ SUCCESS - Test email queued to: " . get_option('admin_email') . "

"; + echo "

Check your inbox in a few moments. Also check Action Scheduler queue above.

"; + } + + if ($_GET['action'] === 'process_queue') { + echo "

Processing Email Queue...

"; + + if (function_exists('as_run_queue')) { + as_run_queue(); + echo "

✓ SUCCESS - Action Scheduler queue processed

"; + } else { + echo "

⚠ WARN - Action Scheduler not available

"; + } + } + + if ($_GET['action'] === 'test_order_email') { + echo "

Testing Order Email...

"; + + // Get a recent order + $orders = wc_get_orders(['limit' => 1, 'orderby' => 'date', 'order' => 'DESC']); + + if (empty($orders)) { + echo "

✗ FAIL - No orders found. Create a test order first.

"; + } else { + $order = $orders[0]; + echo "

Using order #" . $order->get_id() . "

"; + + // Trigger order processing email + $email_manager = \WooNooW\Core\Notifications\EmailManager::instance(); + $email_manager->send_order_processing_email($order->get_id(), $order); + + echo "

✓ SUCCESS - Order processing email triggered

"; + echo "

Check Action Scheduler queue and email logs above.

"; + } + } + + echo ""; + } + ?> + +
+ + + + +
+ +

7. Troubleshooting

+
+

If emails are not sending:

+
    +
  1. Check that "Notification System Mode" is set to "woonoow" (not "woocommerce")
  2. +
  3. Check that "Email Channel" is enabled
  4. +
  5. Check that specific events have email enabled in configuration
  6. +
  7. Check Action Scheduler for failed actions
  8. +
  9. Enable WP_DEBUG and WP_DEBUG_LOG to see detailed logs
  10. +
  11. Check your server's mail configuration (SMTP, sendmail, etc.)
  12. +
  13. Test with "Queue Test Email" button above
  14. +
+
+ +
+ Generated: +
+ +