diff --git a/send.php b/send.php index 54ed5b9..10182ec 100644 --- a/send.php +++ b/send.php @@ -81,12 +81,61 @@ $headers .= "Reply-To: $email\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; $headers .= "X-Mailer: Cuitunet-Web\r\n"; -// Lähetys -error_log("cuitunet.fi send.php: Lähetetään posti osoitteeseen $to, aihe: $subject"); -$sent = @mail($to, $subject, $body, $headers); -error_log("cuitunet.fi send.php: mail() palautti: " . ($sent ? 'true' : 'false') . ", error: " . error_get_last()['message'] ?? 'none'); +// Lähetys suoraan SMTP:llä MX-palvelimelle (ohittaa Pleskin postfixin) +function sendViaSMTP(string $from, string $to, string $subject, string $body, string $replyTo): string { + $mx = 'mx.mail2.fi'; + $port = 25; + $hostname = 'cuitunet.fi'; -if ($sent) { + $sock = @fsockopen($mx, $port, $errno, $errstr, 10); + if (!$sock) return "Yhteys MX-palvelimeen epäonnistui: $errstr ($errno)"; + + $resp = fgets($sock, 512); + if (substr($resp, 0, 3) !== '220') { fclose($sock); return "MX hylkäsi yhteyden: $resp"; } + + $cmds = [ + "EHLO $hostname", + "MAIL FROM:<$from>", + "RCPT TO:<$to>", + "DATA", + ]; + foreach ($cmds as $cmd) { + fwrite($sock, "$cmd\r\n"); + $resp = fgets($sock, 512); + $code = substr($resp, 0, 3); + // EHLO voi palauttaa monta riviä + if ($cmd === "EHLO $hostname") { + while (substr($resp, 3, 1) === '-') { $resp = fgets($sock, 512); } + } + if ($cmd === 'DATA' && $code !== '354') { fwrite($sock, "QUIT\r\n"); fclose($sock); return "DATA hylätty: $resp"; } + elseif ($cmd !== 'DATA' && $code[0] !== '2') { fwrite($sock, "QUIT\r\n"); fclose($sock); return "SMTP virhe ($cmd): $resp"; } + } + + // Viesti + $msg = "From: $from\r\n"; + $msg .= "To: $to\r\n"; + $msg .= "Reply-To: $replyTo\r\n"; + $msg .= "Subject: $subject\r\n"; + $msg .= "Content-Type: text/plain; charset=UTF-8\r\n"; + $msg .= "X-Mailer: Cuitunet-Web\r\n"; + $msg .= "Date: " . date('r') . "\r\n"; + $msg .= "Message-ID: <" . uniqid('cuitunet-') . "@$hostname>\r\n"; + $msg .= "\r\n"; + $msg .= str_replace("\n.", "\n..", $body); // Dot-stuffing + $msg .= "\r\n.\r\n"; + + fwrite($sock, $msg); + $resp = fgets($sock, 512); + fwrite($sock, "QUIT\r\n"); + fclose($sock); + + return (substr($resp, 0, 3) === '250') ? '' : "Lähetys epäonnistui: $resp"; +} + +$fromAddr = 'sivusto@cuitunet.fi'; +$smtpError = sendViaSMTP($fromAddr, $to, $subject, $body, $email); + +if (empty($smtpError)) { // Tallenna rate limit file_put_contents($rateLimitFile, $now); @@ -97,6 +146,7 @@ if ($sent) { echo json_encode(['success' => true]); } else { + error_log("cuitunet.fi send.php SMTP error: $smtpError"); http_response_code(500); echo json_encode(['success' => false, 'error' => 'Viestin lähetys epäonnistui. Yritä myöhemmin uudelleen.']); }