init_hooks(); } /** * Initialize hooks */ private function init_hooks() { // Note: PHPMailer configuration is handled by AHA_Mail_SMTP_Core class // This class provides utility methods for testing and status checking add_action('wp_mail_failed', array($this, 'log_mail_error')); } /** * Log email attempt (called from Core class via global) */ private function log_email_attempt($phpmailer) { if (!AHA_Mail_SMTP_Settings::get('enable_logging')) { return; } // Get email details $to_emails = array(); foreach ($phpmailer->getToAddresses() as $to) { $to_emails[] = $to[0]; } $cc_emails = array(); foreach ($phpmailer->getCcAddresses() as $cc) { $cc_emails[] = $cc[0]; } $bcc_emails = array(); foreach ($phpmailer->getBccAddresses() as $bcc) { $bcc_emails[] = $bcc[0]; } $attachments = array(); foreach ($phpmailer->getAttachments() as $attachment) { $attachments[] = basename($attachment[0]); } // Prepare log data $log_data = array( 'to_email' => implode(', ', $to_emails), 'cc_email' => implode(', ', $cc_emails), 'bcc_email' => implode(', ', $bcc_emails), 'subject' => $phpmailer->Subject, 'message' => $phpmailer->Body, 'headers' => $this->get_headers_string($phpmailer), 'attachments' => implode(', ', $attachments), 'status' => 'pending' ); // Store in global for later use global $aha_mail_smtp_current_log; $aha_mail_smtp_current_log = $log_data; } /** * Log mail error */ public function log_mail_error($wp_error) { if (!AHA_Mail_SMTP_Settings::get('enable_logging')) { return; } global $aha_mail_smtp_current_log; if (!empty($aha_mail_smtp_current_log) && is_array($aha_mail_smtp_current_log)) { $aha_mail_smtp_current_log['status'] = 'failed'; $aha_mail_smtp_current_log['error_message'] = is_wp_error($wp_error) ? $wp_error->get_error_message() : 'Unknown error'; AHA_Mail_SMTP_Logger::log_email($aha_mail_smtp_current_log); } } /** * Get headers as string */ private function get_headers_string($phpmailer) { $headers = array(); // From if (!empty($phpmailer->From)) { $from_name = !empty($phpmailer->FromName) ? $phpmailer->FromName : ''; $headers[] = 'From: ' . $from_name . ' <' . $phpmailer->From . '>'; } // Reply-To foreach ($phpmailer->getReplyToAddresses() as $reply_to) { $headers[] = 'Reply-To: ' . $reply_to[1] . ' <' . $reply_to[0] . '>'; } // CC foreach ($phpmailer->getCcAddresses() as $cc) { $headers[] = 'Cc: ' . $cc[1] . ' <' . $cc[0] . '>'; } // BCC foreach ($phpmailer->getBccAddresses() as $bcc) { $headers[] = 'Bcc: ' . $bcc[1] . ' <' . $bcc[0] . '>'; } // Content-Type if ($phpmailer->ContentType) { $headers[] = 'Content-Type: ' . $phpmailer->ContentType . '; charset=' . $phpmailer->CharSet; } return implode("\n", $headers); } /** * Test SMTP connection */ public static function test_connection() { if (!AHA_Mail_SMTP_Settings::is_smtp_configured()) { return new WP_Error('not_configured', __('SMTP is not configured', 'aha-mail-smtp')); } $settings = AHA_Mail_SMTP_Settings::get_smtp_settings(); // Create PHPMailer instance $phpmailer = new PHPMailer\PHPMailer\PHPMailer(true); try { // SMTP configuration $phpmailer->isSMTP(); $phpmailer->Host = $settings['host']; $phpmailer->Port = $settings['port']; // Set encryption if ($settings['encryption'] === 'ssl') { $phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS; } elseif ($settings['encryption'] === 'tls') { $phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; } // Authentication if ($settings['auth']) { $phpmailer->SMTPAuth = true; $phpmailer->Username = $settings['username']; $phpmailer->Password = $settings['password']; } // Test connection $phpmailer->SMTPDebug = 0; // Disable debug output $phpmailer->Timeout = 10; if ($phpmailer->smtpConnect()) { $phpmailer->smtpClose(); return true; } else { return new WP_Error('connection_failed', __('SMTP connection failed', 'aha-mail-smtp')); } } catch (Exception $e) { return new WP_Error('smtp_error', $e->getMessage()); } } /** * Send test email */ public static function send_test_email($to, $subject = '', $message = '') { if (empty($subject)) { $subject = __('AHA Mail SMTP Test Email', 'aha-mail-smtp'); } if (empty($message)) { $message = __('This is a test email sent from AHA Mail SMTP plugin to verify your SMTP configuration is working correctly.', 'aha-mail-smtp'); $message .= "\n\n"; $message .= sprintf(__('Sent from: %s', 'aha-mail-smtp'), get_bloginfo('name')); $message .= "\n"; $message .= sprintf(__('Date: %s', 'aha-mail-smtp'), current_time('mysql')); } // Add test email headers $headers = array( 'X-Mailer: AHA Mail SMTP ' . AHA_MAIL_SMTP_VERSION, 'X-AHA-Mail-SMTP-Test: true' ); $result = wp_mail($to, $subject, $message, $headers); if ($result) { // Log successful test if (AHA_Mail_SMTP_Settings::get('enable_logging')) { global $aha_mail_smtp_current_log; if (!empty($aha_mail_smtp_current_log)) { $aha_mail_smtp_current_log['status'] = 'success'; AHA_Mail_SMTP_Logger::log_email($aha_mail_smtp_current_log); } } return true; } else { return false; } } /** * Get SMTP status */ public static function get_smtp_status() { if (!AHA_Mail_SMTP_Settings::is_smtp_configured()) { return array( 'status' => 'not_configured', 'message' => __('SMTP is not configured', 'aha-mail-smtp') ); } $connection_test = self::test_connection(); if (is_wp_error($connection_test)) { return array( 'status' => 'error', 'message' => $connection_test->get_error_message() ); } return array( 'status' => 'success', 'message' => __('SMTP is configured and working', 'aha-mail-smtp') ); } /** * Get mailer info */ public static function get_mailer_info() { $settings = AHA_Mail_SMTP_Settings::get_smtp_settings(); $providers = AHA_Mail_SMTP_Core::get_smtp_providers(); $current_provider = AHA_Mail_SMTP_Settings::get('provider', 'custom'); return array( 'provider' => isset($providers[$current_provider]) ? $providers[$current_provider]['name'] : 'Custom SMTP', 'host' => $settings['host'], 'port' => $settings['port'], 'encryption' => strtoupper($settings['encryption']), 'authentication' => $settings['auth'] ? __('Yes', 'aha-mail-smtp') : __('No', 'aha-mail-smtp'), 'from_email' => $settings['from_email'], 'from_name' => $settings['from_name'] ); } }