{email} = recipient email | {user} = username before @ | {domain} = domain after @
'success']); } else { echo json_encode(['status' => 'error', 'msg' => 'Invalid password']); } exit; } // Check Authentication for API Calls if (isset($_POST['action']) && (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true)) { header('Content-Type: application/json'); echo json_encode(['status' => 'error', 'msg' => 'Unauthorized']); exit; } // Handle Logout if (isset($_GET['logout'])) { session_destroy(); header("Location: ?"); exit; } // Compact SMTP Class class MiniSMTP { private $sock; public function send($host, $port, $user, $pass, $from_email, $from_name, $to, $subject, $body) { $this->sock = @fsockopen(($port == 465 ? "ssl://" : "") . $host, $port, $errno, $errstr, 15); if (!$this->sock) return "Connection failed: $errstr"; stream_set_timeout($this->sock, 15); $this->read(); $this->write("EHLO " . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost')); $this->read(); if ($port == 587) { $this->write("STARTTLS"); $this->read(); $crypto = stream_socket_enable_crypto($this->sock, true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT); if (!$crypto) return "TLS setup failed"; $this->write("EHLO " . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost')); $this->read(); } if (!empty($user) && !empty($pass)) { $this->write("AUTH LOGIN"); $this->read(); $this->write(base64_encode($user)); $this->read(); $this->write(base64_encode($pass)); $auth_res = $this->read(); if (substr($auth_res, 0, 3) != '235') return "Auth failed: $auth_res"; } $this->write("MAIL FROM:<$from_email>"); $this->read(); $this->write("RCPT TO:<$to>"); $rcpt_res = $this->read(); if (substr($rcpt_res, 0, 3) != '250') return "RCPT TO failed: $rcpt_res"; $this->write("DATA"); $data_res = $this->read(); if (substr($data_res, 0, 3) != '354') return "DATA failed: $data_res"; $msg_id = "<" . md5(uniqid(microtime(true))) . "@" . explode('@', $from_email)[1] . ">"; $msg = "Date: " . date("r") . "\r\n"; $msg .= "Message-ID: $msg_id\r\n"; $msg .= "From: =?UTF-8?B?".base64_encode($from_name)."?= <$from_email>\r\n"; $msg .= "To: <$to>\r\n"; $msg .= "Subject: =?UTF-8?B?".base64_encode($subject)."?=\r\n"; $msg .= "MIME-Version: 1.0\r\n"; $msg .= "Content-Type: text/html; charset=UTF-8\r\n"; $msg .= "X-Mailer: PHP/" . phpversion() . "\r\n\r\n"; $msg .= $body . "\r\n."; $this->write($msg); $res = $this->read(); $this->write("QUIT"); fclose($this->sock); return substr($res, 0, 3) == '250' ? true : "Delivery Error: $res"; } private function write($data) { fwrite($this->sock, $data . "\r\n"); } private function read() { $res = ""; while ($str = fgets($this->sock, 515)) { $res .= $str; if (substr($str, 3, 1) == " ") break; } return $res; } } // Handle Sending if (isset($_POST['action']) && $_POST['action'] === 'send') { header('Content-Type: application/json'); $to = filter_var($_POST['to'], FILTER_SANITIZE_EMAIL); if (!$to || !filter_var($to, FILTER_VALIDATE_EMAIL)) { echo json_encode(['status' => 'error', 'msg' => 'Invalid email address']); exit; } $send_method = $_POST['send_method'] ?? 'smtp'; if ($send_method === 'php') { // Use PHP mail() function $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; $headers .= "From: =?UTF-8?B?".base64_encode($_POST['from_name'])."?= <".$_POST['from_email'].">\r\n"; $headers .= "X-Mailer: PHP/" . phpversion(); $result = @mail($to, "=?UTF-8?B?".base64_encode($_POST['subject'])."?=", $_POST['body'], $headers); if ($result) { echo json_encode(['status' => 'success']); } else { echo json_encode(['status' => 'error', 'msg' => 'PHP mail() failed on localhost']); } exit; } // Use SMTP $smtp = new MiniSMTP(); $result = $smtp->send( $_POST['host'], $_POST['port'], $_POST['user'], $_POST['pass'], $_POST['from_email'], $_POST['from_name'], $to, $_POST['subject'], $_POST['body'] ); if ($result === true) { echo json_encode(['status' => 'success']); } else { echo json_encode(['status' => 'error', 'msg' => $result]); } exit; } // ========================================== // FRONTEND UI (HTML/CSS/JS) // ========================================== ?>
{email} = recipient email | {user} = username before @ | {domain} = domain after @