fix: Improve email sending functionality and debugging

- Remove problematic  filter implementation
- Add improved email debugging and logging
- Enhance error handling for wp_mail failures
- Add helpful email troubleshooting information
- Add SMTP configuration debugging

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
bengizmo 2025-05-20 16:48:23 -03:00
parent 8a9f86718f
commit 64af743cdd
3 changed files with 147 additions and 35 deletions

View file

@ -295,12 +295,33 @@ class HVAC_Email_Attendees_Data {
}
}
// Add sender information
// Add sender information from the logged-in trainer
$current_user = wp_get_current_user();
$from_name = $current_user->display_name;
$from_email = $current_user->user_email;
// Get trainer profile data if available
$trainer_name = $current_user->display_name;
$trainer_email = $current_user->user_email;
// Check if user is a trainer and has profile data
if (in_array('hvac_trainer', $current_user->roles)) {
// Try to get trainer business name first
$business_name = get_user_meta($current_user->ID, 'business_name', true);
if (!empty($business_name)) {
$trainer_name = $business_name;
}
// Try to get trainer contact email if different
$contact_email = get_user_meta($current_user->ID, 'contact_email', true);
if (!empty($contact_email) && is_email($contact_email)) {
$trainer_email = $contact_email;
}
}
$from_name = $trainer_name;
$from_email = $trainer_email;
$headers[] = 'From: ' . $from_name . ' <' . $from_email . '>';
$debug_log .= "From: {$from_name} <{$from_email}>\n";
$debug_log .= "User role: " . implode(', ', $current_user->roles) . "\n";
// Process recipients
$all_attendees = $this->get_attendees();
@ -379,43 +400,42 @@ class HVAC_Email_Attendees_Data {
$debug_log .= "Subject: {$email_subject}\n";
$debug_log .= "Headers: " . print_r($headers, true) . "\n";
// Add error logger for wp_mail
// Note: consolidated error logging is added below
// Add detailed logging
$debug_log .= "Headers: " . print_r($headers, true) . "\n";
$debug_log .= "Sending mail with wp_mail()\n";
// Add robust error logging
add_action('wp_mail_failed', function($wp_error) use (&$debug_log) {
$debug_log .= "Mail error: " . $wp_error->get_error_message() . "\n";
$debug_log .= "Error data: " . print_r($wp_error->get_error_data(), true) . "\n";
if (class_exists('HVAC_Logger')) {
HVAC_Logger::error('WordPress Mail Error: ' . $wp_error->get_error_message(), 'Email System');
HVAC_Logger::error('WordPress Mail Error: ' . $wp_error->get_error_message() . ' - ' . print_r($wp_error->get_error_data(), true), '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;
// Try to log environment information
$debug_log .= "Mail environment:\n";
$debug_log .= "WordPress version: " . get_bloginfo('version') . "\n";
if (function_exists('phpversion')) {
$debug_log .= "PHP version: " . phpversion() . "\n";
}
// Check if WP Mail SMTP is active
$active_plugins = get_option('active_plugins', array());
$wp_mail_smtp_active = false;
foreach ($active_plugins as $plugin) {
if (strpos($plugin, 'wp-mail-smtp') !== false) {
$wp_mail_smtp_active = true;
$debug_log .= "WP Mail SMTP plugin is active\n";
break;
}
// Return null to let wp_mail proceed with its normal process
return $null;
}, 10, 2);
}
// Send with standard wp_mail
$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 ) {

View file

@ -118,6 +118,25 @@ class HVAC_Email_Debug {
$current_user = wp_get_current_user();
$output .= "Current user: " . $current_user->display_name . " (" . $current_user->user_email . ")\n";
$output .= "User roles: " . implode(', ', $current_user->roles) . "\n";
// Check additional user profile data
$output .= "\n=== USER PROFILE DATA ===\n";
$business_name = get_user_meta($current_user->ID, 'business_name', true);
$contact_email = get_user_meta($current_user->ID, 'contact_email', true);
$phone = get_user_meta($current_user->ID, 'phone', true);
$output .= "Business Name: " . ($business_name ? $business_name : 'Not set') . "\n";
$output .= "Contact Email: " . ($contact_email ? $contact_email : 'Not set') . "\n";
$output .= "Phone: " . ($phone ? $phone : 'Not set') . "\n";
// List all meta fields for debugging
$output .= "\nAll user meta fields:\n";
$user_meta = get_user_meta($current_user->ID);
foreach ($user_meta as $key => $value) {
if (is_array($value) && isset($value[0])) {
$output .= "- {$key}: " . (strlen($value[0]) > 50 ? substr($value[0], 0, 50) . "..." : $value[0]) . "\n";
}
}
}
// Check event
@ -210,17 +229,59 @@ class HVAC_Email_Debug {
// Test email functionality
$output .= "\n=== EMAIL FUNCTIONALITY ===\n";
$output .= "WordPress mail function available: " . (function_exists('wp_mail') ? 'Yes' : 'No') . "\n";
$output .= "PHP mail function available: " . (function_exists('mail') ? 'Yes' : 'No') . "\n";
// Get mail settings
$output .= "Mail configuration:\n";
$output .= "\nMail 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";
// Check for mail plugins
$output .= "\nMail plugins:\n";
$all_plugins = get_option('active_plugins', array());
$mail_plugins_found = false;
$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";
foreach ($all_plugins as $plugin) {
if (strpos($plugin, 'mail') !== false || strpos($plugin, 'smtp') !== false) {
$output .= "- {$plugin}\n";
$mail_plugins_found = true;
}
}
if (!$mail_plugins_found) {
$output .= "No mail plugins detected\n";
}
// Check WP Mail SMTP settings if installed
if (in_array('wp-mail-smtp/wp_mail_smtp.php', $all_plugins)) {
$output .= "\nWP Mail SMTP settings:\n";
$smtp_settings = get_option('wp_mail_smtp', array());
if (!empty($smtp_settings)) {
// Don't show passwords, just configuration status
$output .= "Mailer: " . (isset($smtp_settings['mail']['mailer']) ? $smtp_settings['mail']['mailer'] : 'Not set') . "\n";
$output .= "From Email: " . (isset($smtp_settings['mail']['from_email']) ? $smtp_settings['mail']['from_email'] : 'Not set') . "\n";
$output .= "From Name: " . (isset($smtp_settings['mail']['from_name']) ? $smtp_settings['mail']['from_name'] : 'Not set') . "\n";
$output .= "Return Path: " . (isset($smtp_settings['mail']['return_path']) ? 'Enabled' : 'Disabled') . "\n";
if (isset($smtp_settings['mail']['mailer'])) {
$mailer = $smtp_settings['mail']['mailer'];
$output .= "SMTP Host: " . (isset($smtp_settings[$mailer]['host']) ? 'Configured' : 'Not configured') . "\n";
$output .= "SMTP Encryption: " . (isset($smtp_settings[$mailer]['encryption']) ? $smtp_settings[$mailer]['encryption'] : 'Not set') . "\n";
$output .= "SMTP Auth: " . (isset($smtp_settings[$mailer]['auth']) ? 'Enabled' : 'Disabled') . "\n";
$output .= "SMTP Port: " . (isset($smtp_settings[$mailer]['port']) ? $smtp_settings[$mailer]['port'] : 'Not set') . "\n";
}
} else {
$output .= "WP Mail SMTP settings not found\n";
}
}
// Add test button that will send an actual test email
$output .= "\nEmail testing:\n";
$output .= "If you need to send a test email, please use one of these options:\n";
$output .= "1. Use the form on this page with a single recipient\n";
$output .= "2. If using WP Mail SMTP, go to WP Mail SMTP settings and use their test email feature\n";
$output .= "3. Contact your hosting provider if mail is still not working\n";
// Return debug output
wp_send_json_success($output);

View file

@ -251,6 +251,37 @@ $site_title = get_bloginfo( 'name' );
<input type="text" name="email_cc" id="email_cc" value="<?php echo isset( $_POST['email_cc'] ) ? esc_attr( $_POST['email_cc'] ) : ''; ?>" placeholder="<?php _e( 'Separate multiple emails with commas', 'hvac-community-events' ); ?>">
</div>
<?php
// Get sender information for display
$current_user = wp_get_current_user();
$sender_name = $current_user->display_name;
$sender_email = $current_user->user_email;
// Get trainer profile data if available
if (in_array('hvac_trainer', $current_user->roles)) {
$business_name = get_user_meta($current_user->ID, 'business_name', true);
if (!empty($business_name)) {
$sender_name = $business_name;
}
$contact_email = get_user_meta($current_user->ID, 'contact_email', true);
if (!empty($contact_email) && is_email($contact_email)) {
$sender_email = $contact_email;
}
}
?>
<div class="hvac-email-info" style="background-color: #e7f5ff; margin-bottom: 20px; padding: 15px; border-radius: 5px; border: 1px solid #a3c4e1;">
<p><?php _e( 'Email will be sent from:', 'hvac-community-events' ); ?> <strong><?php echo esc_html($sender_name); ?> &lt;<?php echo esc_html($sender_email); ?>&gt;</strong></p>
<p style="margin-bottom: 5px;"><small><?php _e('Having email issues? Try these steps:', 'hvac-community-events'); ?></small></p>
<ul style="margin-top: 0; font-size: 13px;">
<li><?php _e('Make sure your trainer profile has a valid email address', 'hvac-community-events'); ?></li>
<li><?php _e('Check that recipients have valid email addresses', 'hvac-community-events'); ?></li>
<li><?php _e('Use the "Debug Email System" button at the bottom right corner for more details', 'hvac-community-events'); ?></li>
<li><?php _e('Contact support if emails are still not being delivered', 'hvac-community-events'); ?></li>
</ul>
</div>
<div class="hvac-email-form-row">
<label for="email_message"><?php _e( 'Message:', 'hvac-community-events' ); ?> <span class="required">*</span></label>
<?php