SMTP STARTTLS + auth sivusto@cuitunet.fi kautta smtp.mail2.fi:587
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
82
send.php
82
send.php
@@ -81,47 +81,74 @@ $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 suoraan SMTP:llä MX-palvelimelle (ohittaa Pleskin postfixin)
|
// SMTP-lähetys mail2.fi:n kautta (STARTTLS + auth)
|
||||||
function sendViaSMTP(string $from, string $to, string $subject, string $body, string $replyTo): string {
|
function sendViaSMTP(string $to, string $subject, string $body, string $replyTo): string {
|
||||||
$mx = 'mx.mail2.fi';
|
$smtpHost = 'smtp.mail2.fi';
|
||||||
$port = 25;
|
$smtpPort = 587;
|
||||||
|
$smtpUser = 'sivusto@cuitunet.fi';
|
||||||
|
$smtpPass = 'Passus123!';
|
||||||
|
$fromEmail = 'sivusto@cuitunet.fi';
|
||||||
|
$fromName = 'Cuitunet Saatavuuskysely';
|
||||||
$hostname = 'cuitunet.fi';
|
$hostname = 'cuitunet.fi';
|
||||||
|
|
||||||
$sock = @fsockopen($mx, $port, $errno, $errstr, 10);
|
$sock = @fsockopen($smtpHost, $smtpPort, $errno, $errstr, 10);
|
||||||
if (!$sock) return "Yhteys MX-palvelimeen epäonnistui: $errstr ($errno)";
|
if (!$sock) return "SMTP-yhteys epäonnistui: $errstr ($errno)";
|
||||||
|
|
||||||
$resp = fgets($sock, 512);
|
$resp = fgets($sock, 512);
|
||||||
if (substr($resp, 0, 3) !== '220') { fclose($sock); return "MX hylkäsi yhteyden: $resp"; }
|
if (substr($resp, 0, 3) !== '220') { fclose($sock); return "SMTP hylkäsi: $resp"; }
|
||||||
|
|
||||||
$cmds = [
|
// EHLO
|
||||||
"EHLO $hostname",
|
fwrite($sock, "EHLO $hostname\r\n");
|
||||||
"MAIL FROM:<$from>",
|
$resp = fgets($sock, 512);
|
||||||
"RCPT TO:<$to>",
|
while (substr($resp, 3, 1) === '-') { $resp = fgets($sock, 512); }
|
||||||
"DATA",
|
|
||||||
];
|
// STARTTLS
|
||||||
foreach ($cmds as $cmd) {
|
fwrite($sock, "STARTTLS\r\n");
|
||||||
fwrite($sock, "$cmd\r\n");
|
$resp = fgets($sock, 512);
|
||||||
$resp = fgets($sock, 512);
|
if (substr($resp, 0, 3) !== '220') { fclose($sock); return "STARTTLS epäonnistui: $resp"; }
|
||||||
$code = substr($resp, 0, 3);
|
if (!stream_socket_enable_crypto($sock, true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT)) {
|
||||||
// EHLO voi palauttaa monta riviä
|
fclose($sock); return "TLS handshake epäonnistui";
|
||||||
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"; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EHLO uudelleen TLS:n jälkeen
|
||||||
|
fwrite($sock, "EHLO $hostname\r\n");
|
||||||
|
$resp = fgets($sock, 512);
|
||||||
|
while (substr($resp, 3, 1) === '-') { $resp = fgets($sock, 512); }
|
||||||
|
|
||||||
|
// AUTH LOGIN
|
||||||
|
fwrite($sock, "AUTH LOGIN\r\n");
|
||||||
|
$resp = fgets($sock, 512);
|
||||||
|
if (substr($resp, 0, 3) !== '334') { fclose($sock); return "AUTH ei tuettu: $resp"; }
|
||||||
|
fwrite($sock, base64_encode($smtpUser) . "\r\n");
|
||||||
|
$resp = fgets($sock, 512);
|
||||||
|
if (substr($resp, 0, 3) !== '334') { fclose($sock); return "Käyttäjänimi hylätty: $resp"; }
|
||||||
|
fwrite($sock, base64_encode($smtpPass) . "\r\n");
|
||||||
|
$resp = fgets($sock, 512);
|
||||||
|
if (substr($resp, 0, 3) !== '235') { fclose($sock); return "Kirjautuminen epäonnistui: $resp"; }
|
||||||
|
|
||||||
|
// MAIL FROM / RCPT TO / DATA
|
||||||
|
fwrite($sock, "MAIL FROM:<$fromEmail>\r\n");
|
||||||
|
$resp = fgets($sock, 512);
|
||||||
|
if ($resp[0] !== '2') { fclose($sock); return "MAIL FROM hylätty: $resp"; }
|
||||||
|
|
||||||
|
fwrite($sock, "RCPT TO:<$to>\r\n");
|
||||||
|
$resp = fgets($sock, 512);
|
||||||
|
if ($resp[0] !== '2') { fclose($sock); return "RCPT TO hylätty: $resp"; }
|
||||||
|
|
||||||
|
fwrite($sock, "DATA\r\n");
|
||||||
|
$resp = fgets($sock, 512);
|
||||||
|
if (substr($resp, 0, 3) !== '354') { fclose($sock); return "DATA hylätty: $resp"; }
|
||||||
|
|
||||||
// Viesti
|
// Viesti
|
||||||
$msg = "From: $from\r\n";
|
$msg = "From: $fromName <$fromEmail>\r\n";
|
||||||
$msg .= "To: $to\r\n";
|
$msg .= "To: $to\r\n";
|
||||||
$msg .= "Reply-To: $replyTo\r\n";
|
$msg .= "Reply-To: $replyTo\r\n";
|
||||||
$msg .= "Subject: $subject\r\n";
|
$msg .= "Subject: $subject\r\n";
|
||||||
$msg .= "Content-Type: text/plain; charset=UTF-8\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 .= "Date: " . date('r') . "\r\n";
|
||||||
$msg .= "Message-ID: <" . uniqid('cuitunet-') . "@$hostname>\r\n";
|
$msg .= "Message-ID: <" . uniqid('cuitunet-') . "@$hostname>\r\n";
|
||||||
$msg .= "\r\n";
|
$msg .= "\r\n";
|
||||||
$msg .= str_replace("\n.", "\n..", $body); // Dot-stuffing
|
$msg .= str_replace("\n.", "\n..", $body);
|
||||||
$msg .= "\r\n.\r\n";
|
$msg .= "\r\n.\r\n";
|
||||||
|
|
||||||
fwrite($sock, $msg);
|
fwrite($sock, $msg);
|
||||||
@@ -132,8 +159,7 @@ function sendViaSMTP(string $from, string $to, string $subject, string $body, st
|
|||||||
return (substr($resp, 0, 3) === '250') ? '' : "Lähetys epäonnistui: $resp";
|
return (substr($resp, 0, 3) === '250') ? '' : "Lähetys epäonnistui: $resp";
|
||||||
}
|
}
|
||||||
|
|
||||||
$fromAddr = 'sivusto@cuitunet.fi';
|
$smtpError = sendViaSMTP($to, $subject, $body, $email);
|
||||||
$smtpError = sendViaSMTP($fromAddr, $to, $subject, $body, $email);
|
|
||||||
|
|
||||||
if (empty($smtpError)) {
|
if (empty($smtpError)) {
|
||||||
// Tallenna rate limit
|
// Tallenna rate limit
|
||||||
|
|||||||
Reference in New Issue
Block a user