Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/includes/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/includes/smtp_mailer.php

<?php
/**
 * Simple SMTP Mailer for XAMPP environments
 * This bypasses PHP's mail() function and connects directly to SMTP servers
 */

class SimpleSMTPMailer {
    private $smtp_host;
    private $smtp_port;
    private $smtp_username;
    private $smtp_password;
    private $smtp_encryption;
    private $from_email;
    private $from_name;
    private $socket;
    private $last_error = '';

    public function __construct($config) {
        $this->smtp_host = $config['smtp_host'];
        $this->smtp_port = $config['smtp_port'];
        $this->smtp_username = $config['smtp_username'];
        $this->smtp_password = $config['smtp_password'];
        $this->smtp_encryption = $config['smtp_encryption'];
        $this->from_email = $config['from_email'];
        $this->from_name = $config['from_name'];
    }

    public function sendMail($to_email, $subject, $body) {
        try {
            // Connect to SMTP server
            if (!$this->connect()) {
                return false;
            }

            // Perform SMTP handshake
            if (!$this->authenticate()) {
                $this->disconnect();
                return false;
            }

            // Send email
            if (!$this->sendMessage($to_email, $subject, $body)) {
                $this->disconnect();
                return false;
            }

            $this->disconnect();
            return true;

        } catch (Exception $e) {
            $this->last_error = $e->getMessage();
            $this->disconnect();
            return false;
        }
    }

    private function connect() {
        $context = stream_context_create();
        
        if ($this->smtp_encryption === 'ssl') {
            $this->socket = @stream_socket_client(
                "ssl://{$this->smtp_host}:{$this->smtp_port}",
                $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context
            );
        } else {
            $this->socket = @stream_socket_client(
                "{$this->smtp_host}:{$this->smtp_port}",
                $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context
            );
        }

        if (!$this->socket) {
            $this->last_error = "Failed to connect to {$this->smtp_host}:{$this->smtp_port} - $errstr ($errno)";
            return false;
        }

        // Read server greeting
        $response = $this->readResponse();
        if (substr($response, 0, 3) !== '220') {
            $this->last_error = "Invalid server greeting: $response";
            return false;
        }

        return true;
    }

    private function authenticate() {
        // Send EHLO
        $this->sendCommand("EHLO {$this->smtp_host}");
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '250') {
            $this->last_error = "EHLO failed: $response";
            return false;
        }

        // Start TLS if required
        if ($this->smtp_encryption === 'tls') {
            $this->sendCommand("STARTTLS");
            $response = $this->readResponse();
            
            if (substr($response, 0, 3) !== '220') {
                $this->last_error = "STARTTLS failed: $response";
                return false;
            }

            // Enable crypto
            if (!stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
                $this->last_error = "Failed to enable TLS encryption";
                return false;
            }

            // Send EHLO again after TLS
            $this->sendCommand("EHLO {$this->smtp_host}");
            $response = $this->readResponse();
        }

        // Authenticate
        $this->sendCommand("AUTH LOGIN");
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '334') {
            $this->last_error = "AUTH LOGIN failed: $response";
            return false;
        }

        // Send username
        $this->sendCommand(base64_encode($this->smtp_username));
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '334') {
            $this->last_error = "Username authentication failed: $response";
            return false;
        }

        // Send password
        $this->sendCommand(base64_encode($this->smtp_password));
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '235') {
            $this->last_error = "Password authentication failed: $response";
            return false;
        }

        return true;
    }

    private function sendMessage($to_email, $subject, $body) {
        // MAIL FROM
        $this->sendCommand("MAIL FROM:<{$this->from_email}>");
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '250') {
            $this->last_error = "MAIL FROM failed: $response";
            return false;
        }

        // RCPT TO
        $this->sendCommand("RCPT TO:<{$to_email}>");
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '250') {
            $this->last_error = "RCPT TO failed: $response";
            return false;
        }

        // DATA
        $this->sendCommand("DATA");
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '354') {
            $this->last_error = "DATA command failed: $response";
            return false;
        }

        // Email headers and body
        $headers = "From: {$this->from_name} <{$this->from_email}>\r\n";
        $headers .= "To: {$to_email}\r\n";
        $headers .= "Subject: {$subject}\r\n";
        $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
        $headers .= "Date: " . date('r') . "\r\n";
        $headers .= "\r\n";

        $message = $headers . $body . "\r\n.";
        
        $this->sendCommand($message);
        $response = $this->readResponse();
        
        if (substr($response, 0, 3) !== '250') {
            $this->last_error = "Message sending failed: $response";
            return false;
        }

        return true;
    }

    private function sendCommand($command) {
        fwrite($this->socket, $command . "\r\n");
    }

    private function readResponse() {
        $response = '';
        while ($line = fgets($this->socket, 515)) {
            $response .= $line;
            if (substr($line, 3, 1) === ' ') {
                break;
            }
        }
        return trim($response);
    }

    private function disconnect() {
        if ($this->socket) {
            $this->sendCommand("QUIT");
            fclose($this->socket);
            $this->socket = null;
        }
    }

    public function getLastError() {
        return $this->last_error;
    }
}

/**
 * Send email using SMTP (bypassing PHP mail function)
 */
function sendEmailViaSMTP($email_settings, $to_email, $subject, $body) {
    try {
        $mailer = new SimpleSMTPMailer([
            'smtp_host' => $email_settings['smtp_host'],
            'smtp_port' => $email_settings['smtp_port'],
            'smtp_username' => $email_settings['smtp_username'],
            'smtp_password' => $email_settings['smtp_password'],
            'smtp_encryption' => $email_settings['smtp_encryption'],
            'from_email' => $email_settings['from_email'],
            'from_name' => $email_settings['from_name']
        ]);

        $result = $mailer->sendMail($to_email, $subject, $body);
        
        if (!$result) {
            error_log("SMTP Error: " . $mailer->getLastError());
        }
        
        return $result;
        
    } catch (Exception $e) {
        error_log("SMTP Exception: " . $e->getMessage());
        return false;
    }
}
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists