diff --git a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/class-hvac-community-events.php b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/class-hvac-community-events.php index 560e20dd..68577393 100644 --- a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/class-hvac-community-events.php +++ b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/class-hvac-community-events.php @@ -64,7 +64,8 @@ class HVAC_Community_Events { 'certificates/class-certificate-settings.php', // Certificate settings 'certificates/class-certificate-template.php', // Certificate template 'certificates/class-certificate-security.php', // Certificate security - 'certificates/class-certificate-ajax-handler.php' // Certificate AJAX handling + 'certificates/class-certificate-ajax-handler.php', // Certificate AJAX handling + 'community/class-email-debug.php' // Email debugging tools ]; // Make sure Login_Handler is loaded first for shortcode registration $login_handler_path = HVAC_CE_PLUGIN_DIR . 'includes/community/class-login-handler.php'; diff --git a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/community/class-email-attendees-data.php b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/community/class-email-attendees-data.php index 8f1e7ffd..a37d2fe7 100644 --- a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/community/class-email-attendees-data.php +++ b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/community/class-email-attendees-data.php @@ -253,7 +253,14 @@ class HVAC_Email_Attendees_Data { * @return array Result with status and message. */ public function send_email( $recipients, $subject, $message, $cc = '' ) { + // Start debug log + $debug_log = "=== Email Sending Debug ===\n"; + if ( empty( $recipients ) || empty( $subject ) || empty( $message ) ) { + $debug_log .= "Error: Missing required fields\n"; + if (class_exists('HVAC_Logger')) { + HVAC_Logger::error('Email sending failed: Missing required fields', 'Email System'); + } return array( 'success' => false, 'message' => 'Missing required fields (recipients, subject, or message).', @@ -261,6 +268,10 @@ class HVAC_Email_Attendees_Data { } if ( ! $this->is_valid_event() || ! $this->user_can_email_attendees() ) { + $debug_log .= "Error: Permission denied\n"; + if (class_exists('HVAC_Logger')) { + HVAC_Logger::error('Email sending failed: Permission denied', 'Email System'); + } return array( 'success' => false, 'message' => 'You do not have permission to email attendees for this event.', @@ -270,6 +281,7 @@ class HVAC_Email_Attendees_Data { $headers = array('Content-Type: text/html; charset=UTF-8'); $event_details = $this->get_event_details(); $event_title = $event_details['title']; + $debug_log .= "Event: {$event_title} (ID: {$this->event_id})\n"; // Add CC if provided if ( ! empty( $cc ) ) { @@ -278,6 +290,7 @@ class HVAC_Email_Attendees_Data { $cc_email = trim( $cc_email ); if ( is_email( $cc_email ) ) { $headers[] = 'Cc: ' . $cc_email; + $debug_log .= "Added CC: {$cc_email}\n"; } } } @@ -287,48 +300,123 @@ class HVAC_Email_Attendees_Data { $from_name = $current_user->display_name; $from_email = $current_user->user_email; $headers[] = 'From: ' . $from_name . ' <' . $from_email . '>'; + $debug_log .= "From: {$from_name} <{$from_email}>\n"; // Process recipients $all_attendees = $this->get_attendees(); + $debug_log .= "Total attendees found: " . count($all_attendees) . "\n"; + $attendee_emails = array(); $sent_count = 0; $error_count = 0; + $debug_log .= "Recipients provided: " . count($recipients) . "\n"; + // Handle numeric IDs or email addresses foreach ( $recipients as $recipient ) { + $debug_log .= "Processing recipient: {$recipient}\n"; + if ( is_numeric( $recipient ) ) { + $debug_log .= "Recipient is numeric ID\n"; // Find attendee by ID foreach ( $all_attendees as $attendee ) { if ( $attendee['attendee_id'] == $recipient ) { $attendee_emails[$attendee['email']] = $attendee['name']; + $debug_log .= "Matched with attendee: {$attendee['name']} <{$attendee['email']}>\n"; break; } } } elseif ( is_email( $recipient ) ) { + $debug_log .= "Recipient is email address\n"; // Add directly if it's an email $attendee_name = ''; foreach ( $all_attendees as $attendee ) { if ( $attendee['email'] === $recipient ) { $attendee_name = $attendee['name']; + $debug_log .= "Matched with attendee name: {$attendee_name}\n"; break; } } $attendee_emails[$recipient] = $attendee_name; + } else { + $debug_log .= "Invalid recipient format\n"; + } + } + + $debug_log .= "Recipients to email: " . count($attendee_emails) . "\n"; + + if (empty($attendee_emails)) { + $debug_log .= "No valid recipients found! Using fallback to direct send.\n"; + + // Fallback - directly use the first selected email + foreach ($recipients as $recipient) { + if (is_email($recipient)) { + $attendee_emails[$recipient] = ''; + $debug_log .= "Added direct recipient: {$recipient}\n"; + break; + } } } // Subject with event title $email_subject = sprintf( '[%s] %s', $event_title, $subject ); + $debug_log .= "Email subject: {$email_subject}\n"; // Send to each recipient individually for personalization foreach ( $attendee_emails as $email => $name ) { + $debug_log .= "Sending to: {$email}\n"; + // Personalize message with attendee name if available $personalized_message = $message; if ( ! empty( $name ) ) { $personalized_message = "Hello " . $name . ",\n\n" . $message; + $debug_log .= "Personalized with name: {$name}\n"; } - $mail_sent = wp_mail( $email, $email_subject, wpautop( $personalized_message ), $headers ); + // Log complete mail params for debugging + $debug_log .= "Mail parameters:\n"; + $debug_log .= "To: {$email}\n"; + $debug_log .= "Subject: {$email_subject}\n"; + $debug_log .= "Headers: " . print_r($headers, true) . "\n"; + + // Add error logger for wp_mail + add_action('wp_mail_failed', function($wp_error) use (&$debug_log) { + $debug_log .= "Mail error: " . $wp_error->get_error_message() . "\n"; + if (class_exists('HVAC_Logger')) { + HVAC_Logger::error('WordPress Mail Error: ' . $wp_error->get_error_message(), 'Email System'); + } + }); + + // Use server email settings temporarily to avoid host-specific issues + $debug_log .= "Using PHP mail() settings\n"; + add_filter('pre_wp_mail', function($null, $atts) use ($email, $email_subject, $personalized_message, $headers, &$debug_log) { + // Try sending with PHP mail directly + $to = $email; + $subject = $email_subject; + $message = wpautop($personalized_message); + $headers_string = implode("\r\n", $headers); + + $debug_log .= "Using PHP mail() function directly\n"; + + $mail_sent = mail($to, $subject, $message, $headers_string); + + $debug_log .= "PHP mail() result: " . ($mail_sent ? 'Success' : 'Failed') . "\n"; + + // Return true to indicate we've handled the email + if ($mail_sent) { + return true; + } + + // Return null to let wp_mail proceed with its normal process + return $null; + }, 10, 2); + + $mail_sent = wp_mail($email, $email_subject, wpautop($personalized_message), $headers); + + // Remove the filter after sending + remove_filter('pre_wp_mail', function(){}, 10); + + $debug_log .= "wp_mail result: " . ($mail_sent ? 'Success' : 'Failed') . "\n"; if ( $mail_sent ) { $sent_count++; @@ -337,6 +425,11 @@ class HVAC_Email_Attendees_Data { } } + // Log the complete debug information + if (class_exists('HVAC_Logger')) { + HVAC_Logger::info($debug_log, 'Email System'); + } + // Return results if ( $error_count > 0 ) { return array( diff --git a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/community/class-email-debug.php b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/community/class-email-debug.php new file mode 100644 index 00000000..ef6d7afa --- /dev/null +++ b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/community/class-email-debug.php @@ -0,0 +1,244 @@ + +
+ +
+ + + + + display_name . " (" . $current_user->user_email . ")\n"; + $output .= "User roles: " . implode(', ', $current_user->roles) . "\n"; + } + + // Check event + $output .= "\n=== EVENT INFORMATION ===\n"; + $event = get_post($event_id); + if (!$event) { + $output .= "Event ID {$event_id} not found.\n"; + } else { + $output .= "Event ID: {$event_id}\n"; + $output .= "Event title: " . get_the_title($event) . "\n"; + $output .= "Event status: " . get_post_status($event) . "\n"; + $output .= "Event author: " . $event->post_author . "\n"; + + // Check if user can edit event + $output .= "Current user can edit event: " . (get_current_user_id() === (int)$event->post_author || current_user_can('edit_posts') ? 'Yes' : 'No') . "\n"; + } + + // Get attendees + $output .= "\n=== ATTENDEES DATA ===\n"; + require_once HVAC_CE_PLUGIN_DIR . 'includes/community/class-email-attendees-data.php'; + $email_data = new HVAC_Email_Attendees_Data($event_id); + + // Check if event is valid + $output .= "Event is valid: " . ($email_data->is_valid_event() ? 'Yes' : 'No') . "\n"; + $output .= "User can email attendees: " . ($email_data->user_can_email_attendees() ? 'Yes' : 'No') . "\n"; + + // Get attendees + $attendees = $email_data->get_attendees(); + $output .= "Number of attendees found: " . count($attendees) . "\n\n"; + + if (!empty($attendees)) { + $output .= "Attendee details:\n"; + foreach ($attendees as $index => $attendee) { + $output .= "--- Attendee " . ($index + 1) . " ---\n"; + $output .= "Name: " . (!empty($attendee['name']) ? $attendee['name'] : 'No name') . "\n"; + $output .= "Email: " . (!empty($attendee['email']) ? $attendee['email'] : 'No email') . "\n"; + $output .= "Ticket: " . (!empty($attendee['ticket_name']) ? $attendee['ticket_name'] : 'No ticket name') . "\n"; + $output .= "Attendee ID: " . (!empty($attendee['attendee_id']) ? $attendee['attendee_id'] : 'No ID') . "\n"; + $output .= "Order ID: " . (!empty($attendee['order_id']) ? $attendee['order_id'] : 'No order ID') . "\n\n"; + } + } + + // Test direct attendee query + $output .= "\n=== DIRECT ATTENDEE QUERY ===\n"; + $query_args = [ + 'post_type' => 'tribe_tpp_attendees', + 'posts_per_page' => -1, + 'meta_query' => [ + [ + 'key' => '_tribe_tpp_event', + 'value' => $event_id, + 'compare' => '=', + ], + ], + ]; + + $attendees_query = new WP_Query($query_args); + $output .= "Direct query found posts: " . $attendees_query->found_posts . "\n"; + + if ($attendees_query->have_posts()) { + while ($attendees_query->have_posts()) { + $attendees_query->the_post(); + $attendee_id = get_the_ID(); + + $output .= "- Post ID: {$attendee_id}, Title: " . get_the_title() . "\n"; + + // Get metadata + $email = get_post_meta($attendee_id, '_tribe_tickets_email', true); + if (empty($email)) { + $email = get_post_meta($attendee_id, '_tribe_tpp_email', true); + } + + $output .= " Email: " . ($email ?: 'None') . "\n"; + + // Check other important meta + $ticket_id = get_post_meta($attendee_id, '_tribe_tpp_product', true); + $output .= " Ticket ID: " . ($ticket_id ?: 'None') . "\n"; + + if ($ticket_id) { + $output .= " Ticket Title: " . get_the_title($ticket_id) . "\n"; + } + + $output .= " Order ID: " . get_post_meta($attendee_id, '_tribe_tpp_order', true) . "\n"; + $output .= " Check-in: " . get_post_meta($attendee_id, '_tribe_tpp_checkin', true) . "\n"; + $output .= "\n"; + } + wp_reset_postdata(); + } + + // Test email functionality + $output .= "\n=== EMAIL FUNCTIONALITY ===\n"; + $output .= "WordPress mail function available: " . (function_exists('wp_mail') ? 'Yes' : 'No') . "\n"; + + // Get mail settings + $output .= "Mail configuration:\n"; + $admin_email = get_option('admin_email'); + $output .= "Admin email: {$admin_email}\n"; + + $output .= "Mail test: "; + // Don't actually send mail in debug, just check configuration + $output .= "Not sending test email to avoid mail server load\n"; + + $output .= "\nNote: If you need to send a test email, you can use the form on the page with a minimal set of recipients.\n"; + + // Return debug output + wp_send_json_success($output); + } + + /** + * Log mail errors + */ + public static function log_mail_error($wp_error) { + $error_message = $wp_error->get_error_message(); + error_log('WordPress Mail Error: ' . $error_message); + + // Also log to our custom file if logging is enabled + if (class_exists('HVAC_Logger')) { + HVAC_Logger::error('WordPress Mail Error: ' . $error_message, 'Email System'); + } + } +} + +// Initialize the debugging class +HVAC_Email_Debug::init(); \ No newline at end of file