# SMTP Refactoring Plan ## Current Issues 1. Local server SMTP is working perfectly, but normal SMTP is having authentication issues 2. Mailrelay has its own unique settings 3. The current implementation mixes all SMTP types in the same code, making it difficult to maintain and debug ## Proposed Solution Refactor the SMTP handling code to separate each SMTP type into its own file/class. This will make the code more maintainable and easier to debug. ## Implementation Plan ### 1. Create Separate SMTP Handler Classes Create the following files: 1. `includes/smtp/local_server.php` - For local server SMTP 2. `includes/smtp/normal_smtp.php` - For regular SMTP 3. `includes/smtp/mailrelay.php` - For Mailrelay 4. `includes/smtp/amazon_ec2.php` - For Amazon EC2 Each file will contain a class that implements a common interface for SMTP handling. ### 2. Create SMTP Interface Create an interface that all SMTP handlers must implement: ```php // includes/smtp/smtp_interface.php SMTPAuth = false; $mailer->SMTPAutoTLS = false; // Disable auto TLS for port 25 $mailer->SMTPSecure = ''; // No encryption $mailer->Port = 25; // Add debug options for local server $mailer->SMTPOptions = [ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ] ]; return $mailer; } public function getDefaultSettings() { // Get server IP address $server_ip = $_SERVER['SERVER_ADDR'] ?? 'localhost'; return [ 'type' => 'local', 'smtp_server' => $server_ip, 'port' => 25, 'encryption' => 'none', 'username' => '', 'password' => '', 'description' => "Direct server SMTP using IP: $server_ip" ]; } public function validateSettings($settings) { // Local server settings don't need much validation return [true, '']; } } ``` ### 4. Create SMTP Factory Create a factory class to instantiate the appropriate SMTP handler: ```php // includes/smtp/smtp_factory.php isSMTP(); $mail->Host = $smtp_settings['smtp_server']; // Get the appropriate SMTP handler require_once 'includes/smtp/smtp_factory.php'; $smtpHandler = SMTPFactory::getHandler($smtp_settings['type']); // Configure PHPMailer using the handler $mail = $smtpHandler->configurePHPMailer($mail, $smtp_settings); // Continue with email sending... ``` ### 6. Update smtp_settings.php Modify `smtp_settings.php` to use the new SMTP handlers for validation and default settings: ```php // When creating/updating SMTP settings if ($_POST['action'] === 'create' || $_POST['action'] === 'update') { // Get the SMTP handler for the selected type require_once 'includes/smtp/smtp_factory.php'; $smtpHandler = SMTPFactory::getHandler($_POST['type']); // Get default settings for this type $defaultSettings = $smtpHandler->getDefaultSettings(); // Merge with user input $data = array_merge($defaultSettings, [ 'name' => trim($_POST['name']), 'description' => trim($_POST['description']), 'from_name' => trim($_POST['from_name']), 'from_email' => filter_var(trim($_POST['from_email']), FILTER_SANITIZE_EMAIL), 'reply_to' => filter_var(trim($_POST['reply_to']), FILTER_SANITIZE_EMAIL), 'daily_limit' => empty($_POST['daily_limit']) ? null : (int)$_POST['daily_limit'], 'alert_emails' => array_map('trim', explode(',', $_POST['alert_emails'])), 'is_active' => isset($_POST['is_active']) && $_POST['is_active'] ? 1 : 0 ]); // Validate settings list($isValid, $errorMessage) = $smtpHandler->validateSettings($data); if (!$isValid) { $error_message = $errorMessage; break; } // Continue with database operations... } ``` ## Benefits of This Approach 1. **Separation of Concerns**: Each SMTP type has its own implementation, making the code more maintainable. 2. **Easier Debugging**: Issues with a specific SMTP type can be isolated and fixed without affecting others. 3. **Extensibility**: New SMTP types can be added by creating a new handler class. 4. **Cleaner Code**: The main application code becomes cleaner and more focused. ## Implementation Timeline 1. Create the interface and base classes (1 day) 2. Implement each SMTP handler (2 days) 3. Update bulk_email.php and smtp_settings.php (1 day) 4. Testing and debugging (2 days) ## Conclusion This refactoring will make the SMTP handling code more maintainable and easier to debug. It will also make it easier to fix the issues with normal SMTP authentication while keeping the working local server SMTP functionality intact.