init_hooks(); } /** * Initialize hooks */ private function init_hooks() { add_action('phpmailer_init', array($this, 'configure_phpmailer')); add_filter('wp_mail_from', array($this, 'set_mail_from_email')); add_filter('wp_mail_from_name', array($this, 'set_mail_from_name')); } /** * Get SMTP providers configuration */ public static function get_smtp_providers() { return array( 'generic' => array( 'name' => 'Generic SMTP', 'smtp_host' => '', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => false, 'api_supported' => false, 'api_recommended' => false, 'icon' => 'fas fa-envelope', 'icon_text' => '📧', 'color' => '#3b82f6', 'api_fields' => array() ), 'amazon_ses' => array( 'name' => 'Amazon SES', 'smtp_host' => 'email-smtp.us-east-1.amazonaws.com', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => false, 'api_supported' => true, 'api_recommended' => true, 'icon' => 'fab fa-aws', 'icon_text' => '◆', 'color' => '#ff9900', 'api_fields' => array( 'api_key' => array( 'label' => 'Access Key ID', 'type' => 'text', 'description' => 'Your AWS Access Key ID for Amazon SES.' ), 'api_secret' => array( 'label' => 'Secret Access Key', 'type' => 'password', 'description' => 'Your AWS Secret Access Key for Amazon SES.' ), 'api_region' => array( 'label' => 'Region', 'type' => 'select', 'description' => 'Select your AWS SES region.', 'options' => array( 'us-east-1' => 'US East (N. Virginia)', 'us-west-2' => 'US West (Oregon)', 'eu-west-1' => 'Europe (Ireland)', 'ap-southeast-1' => 'Asia Pacific (Singapore)' ) ) ) ), 'sendgrid' => array( 'name' => 'SendGrid', 'smtp_host' => 'smtp.sendgrid.net', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => false, 'api_supported' => true, 'api_recommended' => true, 'icon' => 'fas fa-paper-plane', 'icon_text' => '▣', 'color' => '#00d4ff', 'api_fields' => array( 'api_key' => array( 'label' => 'API Key', 'type' => 'password', 'description' => 'Your SendGrid API key.' ) ) ), 'mailgun' => array( 'name' => 'Mailgun', 'smtp_host' => 'smtp.mailgun.org', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => false, 'api_supported' => true, 'api_recommended' => true, 'icon' => 'fas fa-rocket', 'icon_text' => '●', 'color' => '#e91e63', 'api_fields' => array( 'api_key' => array( 'label' => 'API Key', 'type' => 'password', 'description' => 'Your Mailgun API key.' ), 'api_domain' => array( 'label' => 'Sending Domain', 'type' => 'text', 'description' => 'Your verified sending domain in Mailgun.' ), 'api_region' => array( 'label' => 'Region', 'type' => 'select', 'description' => 'Select your Mailgun region.', 'options' => array( 'us' => 'US', 'eu' => 'EU' ) ) ) ), 'brevo' => array( 'name' => 'Brevo', 'smtp_host' => 'smtp-relay.brevo.com', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => false, 'api_supported' => true, 'api_recommended' => true, 'icon' => 'fas fa-envelope', 'icon_text' => 'B', 'color' => '#00bcd4', 'api_fields' => array( 'api_key' => array( 'label' => 'API Key', 'type' => 'password', 'description' => 'Your Brevo API key.' ) ) ), 'gmail' => array( 'name' => 'Gmail', 'smtp_host' => 'smtp.gmail.com', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => true, 'api_supported' => true, 'api_recommended' => false, 'icon' => 'fab fa-google', 'icon_text' => 'G', 'color' => '#ea4335', 'api_fields' => array( 'api_key' => array( 'label' => 'API Key', 'type' => 'password', 'description' => 'Your Gmail API key.' ), 'client_id' => array( 'label' => 'Client ID', 'type' => 'text', 'description' => 'Your Google OAuth Client ID.' ), 'client_secret' => array( 'label' => 'Client Secret', 'type' => 'password', 'description' => 'Your Google OAuth Client Secret.' ) ) ), 'outlook' => array( 'name' => 'Outlook', 'smtp_host' => 'smtp-mail.outlook.com', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => true, 'api_supported' => true, 'api_recommended' => false, 'icon' => 'fab fa-microsoft', 'icon_text' => 'O', 'color' => '#0078d4', 'api_fields' => array( 'api_key' => array( 'label' => 'API Key', 'type' => 'password', 'description' => 'Your Microsoft Graph API key.' ), 'client_id' => array( 'label' => 'Client ID', 'type' => 'text', 'description' => 'Your Microsoft OAuth Client ID.' ), 'client_secret' => array( 'label' => 'Client Secret', 'type' => 'password', 'description' => 'Your Microsoft OAuth Client Secret.' ) ) ), 'zoho' => array( 'name' => 'Zoho Mail', 'smtp_host' => 'smtp.zoho.com', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => true, 'api_supported' => false, 'api_recommended' => false, 'icon' => 'fas fa-envelope-open-text', 'icon_text' => 'Z', 'color' => '#c9302c', 'api_fields' => array() ), 'smtp2go' => array( 'name' => 'SMTP2GO', 'smtp_host' => 'mail.smtp2go.com', 'smtp_port' => 587, 'smtp_encryption' => 'tls', 'smtp_auth' => true, 'oauth_supported' => false, 'api_supported' => true, 'api_recommended' => true, 'icon' => 'fas fa-mail-bulk', 'icon_text' => 'S', 'color' => '#4caf50', 'api_fields' => array( 'api_key' => array( 'label' => 'API Key', 'type' => 'password', 'description' => 'Your SMTP2GO API key.' ) ) ) ); } /** * Configure PHPMailer with current settings */ public function configure_phpmailer($phpmailer) { try { $settings = AHA_Mail_SMTP_Settings::get_all_settings(); $provider = isset($settings['provider']) ? $settings['provider'] : 'generic'; $connection_mode = isset($settings['connection_mode']) ? $settings['connection_mode'] : 'smtp'; // If API mode is selected and supported, we assume the mailer will be handled by API class. // We only proceed with PHPMailer configuration if connection mode is 'smtp'. if ($connection_mode === 'api' && AHA_Mail_SMTP_Settings::provider_supports_api($provider)) { return; } // For SMTP connection mode or generic provider if (empty($settings['smtp_host'])) { // If a specific provider is chosen but host is empty, try to get default from provider config $provider_config = isset(self::get_smtp_providers()[$provider]) ? self::get_smtp_providers()[$provider] : null; if ($provider_config && !empty($provider_config['smtp_host'])) { $settings['smtp_host'] = $provider_config['smtp_host']; $settings['smtp_port'] = $provider_config['smtp_port']; $settings['smtp_encryption'] = $provider_config['smtp_encryption']; $settings['smtp_auth'] = $provider_config['smtp_auth']; } else if (empty($settings['smtp_host'])) { // If still no host, cannot proceed with SMTP - allow default mail to work return; } } // Set mailer to SMTP $phpmailer->isSMTP(); // SMTP configuration $phpmailer->Host = $settings['smtp_host']; $phpmailer->Port = $settings['smtp_port']; $phpmailer->SMTPAuth = $settings['smtp_auth']; // Encryption if ($settings['smtp_encryption'] === 'ssl') { $phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS; } elseif ($settings['smtp_encryption'] === 'tls') { $phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; } // Authentication if ($settings['smtp_auth']) { $phpmailer->Username = $settings['smtp_username']; $phpmailer->Password = $settings['smtp_password']; } // From email and name // Always set From email and name, even if empty, to prevent PHPMailer defaults $from_email = !empty($settings['from_email']) ? $settings['from_email'] : get_option('admin_email'); $from_name = !empty($settings['from_name']) ? $settings['from_name'] : get_bloginfo('name'); $phpmailer->setFrom($from_email, $from_name); // Reply-To if (!empty($settings['reply_to_email'])) { $phpmailer->addReplyTo($settings['reply_to_email']); } // Return path if (!empty($settings['return_path'])) { $phpmailer->Sender = $settings['return_path']; } // Debug mode if ($settings['debug_mode']) { $phpmailer->SMTPDebug = 2; $phpmailer->Debugoutput = function($str, $level) { error_log("AHA Mail SMTP Debug: $str"); }; } // Log email attempt if ($settings['enable_logging']) { add_action('wp_mail_failed', array($this, 'log_failed_email')); add_action('wp_mail_succeeded', array($this, 'log_successful_email')); } } catch (Exception $e) { // Log the error but don't break email functionality if (defined('WP_DEBUG') && WP_DEBUG) { error_log('AHA Mail SMTP Error: ' . $e->getMessage()); } // Allow default WordPress mail to work if SMTP fails return; } } /** * Log failed email */ public function log_failed_email($wp_error) { $error_message = $wp_error->get_error_message(); $log_data = array( 'to_email' => '', 'subject' => '', 'message' => '', 'headers' => '', 'attachments' => '', 'status' => 'failed', 'error_message' => $error_message ); AHA_Mail_SMTP_Logger::log_email($log_data); } /** * Log successful email */ public function log_successful_email($mail_data) { $to = is_array($mail_data['to']) ? implode(', ', $mail_data['to']) : $mail_data['to']; $log_data = array( 'to_email' => $to, 'subject' => isset($mail_data['subject']) ? $mail_data['subject'] : '', 'message' => isset($mail_data['message']) ? $mail_data['message'] : '', 'headers' => isset($mail_data['headers']) ? (is_array($mail_data['headers']) ? implode("\n", $mail_data['headers']) : $mail_data['headers']) : '', 'attachments' => isset($mail_data['attachments']) ? (is_array($mail_data['attachments']) ? implode(', ', $mail_data['attachments']) : $mail_data['attachments']) : '', 'status' => 'success' ); AHA_Mail_SMTP_Logger::log_email($log_data); } /** * Test email functionality */ public static function send_test_email($to, $subject, $message) { $headers = array('Content-Type: text/html; charset=UTF-8'); $result = wp_mail($to, $subject, $message, $headers); if ($result) { return array( 'success' => true, 'message' => __('Test email sent successfully!', 'aha-mail-smtp') ); } else { return array( 'success' => false, 'message' => __('Test email failed to send. Please check your settings.', 'aha-mail-smtp') ); } } /** * Get provider configuration by key */ public static function get_provider_config($provider_key) { $providers = self::get_smtp_providers(); return isset($providers[$provider_key]) ? $providers[$provider_key] : null; } /** * Validate SMTP connection */ public static function validate_smtp_connection($host, $port, $encryption, $username, $password) { try { $smtp = new PHPMailer\PHPMailer\SMTP(); $smtp->do_debug = 0; // Connect to SMTP server if (!$smtp->connect($host, $port)) { return array( 'success' => false, 'message' => 'Could not connect to SMTP server' ); } // Start TLS if required if ($encryption === 'tls' && !$smtp->startTLS()) { return array( 'success' => false, 'message' => 'Could not start TLS encryption' ); } // Authenticate if (!empty($username) && !$smtp->authenticate($username, $password)) { return array( 'success' => false, 'message' => 'SMTP authentication failed' ); } $smtp->quit(); return array( 'success' => true, 'message' => 'SMTP connection successful' ); } catch (Exception $e) { return array( 'success' => false, 'message' => $e->getMessage() ); } } /** * Get plugin status information */ public static function get_plugin_status() { $settings = AHA_Mail_SMTP_Settings::get_all_settings(); $status = array( 'configured' => !empty($settings['smtp_host']), 'provider' => $settings['provider'], 'connection_mode' => $settings['connection_mode'], 'from_email' => $settings['from_email'], 'logging_enabled' => $settings['enable_logging'], 'debug_enabled' => $settings['debug_mode'] ); return $status; } /** * Get system information for debugging */ public static function get_system_info() { global $wp_version; return array( 'wordpress_version' => $wp_version, 'plugin_version' => AHA_MAIL_SMTP_VERSION, 'php_version' => PHP_VERSION, 'server_software' => isset($_SERVER['SERVER_SOFTWARE']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])) : 'Unknown', 'openssl_enabled' => extension_loaded('openssl'), 'curl_enabled' => extension_loaded('curl'), 'mail_function_exists' => function_exists('mail'), 'phpmailer_version' => class_exists('PHPMailer\\PHPMailer\\PHPMailer') ? PHPMailer\PHPMailer\PHPMailer::VERSION : 'Not available' ); } /** * Set the from email address for wp_mail. */ public function set_mail_from_email($email) { $settings = AHA_Mail_SMTP_Settings::get_all_settings(); // Check new force_from_email setting (or fallback to legacy force_from) $force_email = !empty($settings['force_from_email']) || (!empty($settings['force_from'])); // If force is enabled, always use the configured email if ($force_email && !empty($settings['from_email'])) { return $settings['from_email']; } // Otherwise use configured email if available, or fall back to original $from_email = !empty($settings['from_email']) ? $settings['from_email'] : $email; return $from_email; } /** * Set the from name for wp_mail. */ public function set_mail_from_name($name) { $settings = AHA_Mail_SMTP_Settings::get_all_settings(); // Check new force_from_name setting (or fallback to legacy force_from) $force_name = !empty($settings['force_from_name']) || (!empty($settings['force_from'])); // If force is enabled, always use the configured name if ($force_name && !empty($settings['from_name'])) { return $settings['from_name']; } // Otherwise use configured name if available, or fall back to original $from_name = !empty($settings['from_name']) ? $settings['from_name'] : $name; return $from_name; } /** * Check if debug mode is enabled */ public static function is_debug_mode() { return (bool) AHA_Mail_SMTP_Settings::get('debug_mode', false); } }