Vaihda mail() suoraan SMTP-lähetykseen mx.mail2.fi:hin

Pleskin postfix ei toimittanut postia oikein. Nyt send.php
lähettää suoraan MX-palvelimelle port 25 kautta.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 09:15:34 +02:00
parent 2e58865f66
commit 0f60ea46d4

View File

@@ -81,12 +81,61 @@ $headers .= "Reply-To: $email\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n"; $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "X-Mailer: Cuitunet-Web\r\n"; $headers .= "X-Mailer: Cuitunet-Web\r\n";
// Lähetys // Lähetys suoraan SMTP:llä MX-palvelimelle (ohittaa Pleskin postfixin)
error_log("cuitunet.fi send.php: Lähetetään posti osoitteeseen $to, aihe: $subject"); function sendViaSMTP(string $from, string $to, string $subject, string $body, string $replyTo): string {
$sent = @mail($to, $subject, $body, $headers); $mx = 'mx.mail2.fi';
error_log("cuitunet.fi send.php: mail() palautti: " . ($sent ? 'true' : 'false') . ", error: " . error_get_last()['message'] ?? 'none'); $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 // Tallenna rate limit
file_put_contents($rateLimitFile, $now); file_put_contents($rateLimitFile, $now);
@@ -97,6 +146,7 @@ if ($sent) {
echo json_encode(['success' => true]); echo json_encode(['success' => true]);
} else { } else {
error_log("cuitunet.fi send.php SMTP error: $smtpError");
http_response_code(500); http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Viestin lähetys epäonnistui. Yritä myöhemmin uudelleen.']); echo json_encode(['success' => false, 'error' => 'Viestin lähetys epäonnistui. Yritä myöhemmin uudelleen.']);
} }