Níže naleznete jednoduchý skript v PHP 7, který využívá nativní funkce PHP pro odeslání emailu pomocí SMTP protokolu bez nutnosti instalace PHPMaileru:
<?php
// Adresy odesílatele a příjemce
$fromEmail = Tato e-mailová adresa je chráněna před spamboty. Pro její zobrazení musíte mít povolen Javascript.';
$fromName = 'Odesílatel';
$toEmail = Tato e-mailová adresa je chráněna před spamboty. Pro její zobrazení musíte mít povolen Javascript.';
$toName = 'Příjemce';
// Připojení k SMTP serveru
$smtpHost = 'smtp.example.com';
$smtpPort = 587;
$smtpUsername = 'smtpusername';
$smtpPassword = 'smtppassword';
// Obsah emailu
$subject = 'Testovací email';
$message = 'Toto je testovací email odeslaný pomocí SMTP protokolu.';
// Nastavení hlaviček emailu
$headers = "From: $fromName <$fromEmail>\r\n";
$headers .= "Reply-To: $fromEmail\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/HTML; charset=UTF-8\r\n";
// Vytvoření parametrů pro připojení k SMTP serveru
$smtpParams = array(
'host' => $smtpHost,
'port' => $smtpPort,
'auth' => true,
'username' => $smtpUsername,
'password' => $smtpPassword
);
// Odeslání emailu
if (smtp_mail($toEmail, $subject, $message, $headers, $smtpParams)) {
echo 'Email byl úspěšně odeslán.';
} else {
echo 'Při odesílání emailu došlo k chybě.';
}
// Funkce pro odeslání emailu pomocí SMTP
function smtp_mail($to, $subject, $message, $headers, $smtpParams) {
$smtpConnection = fsockopen($smtpParams['host'], $smtpParams['port'], $errno, $errstr, 30);
if (!$smtpConnection) {
return false;
}
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '220') {
return false;
}
fputs($smtpConnection, "HELO " . $smtpParams['host'] . "\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '250') {
return false;
}
fputs($smtpConnection, "AUTH LOGIN\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '334') {
return false;
}
fputs($smtpConnection, base64_encode($smtpParams['username']) . "\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '334') {
return false;
}
fputs($smtpConnection, base64_encode($smtpParams['password']) . "\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '235') {
return false;
}
fputs($smtpConnection, "MAIL FROM: <$fromEmail>\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '250') {
return false;
}
fputs($smtpConnection, "RCPT TO: <$to>\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '250') {
return false;
}
fputs($smtpConnection, "DATA\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '354') {
return false;
}
fputs($smtpConnection, "$headers\r\n");
fputs($smtpConnection, "Subject: $subject\r\n");
fputs($smtpConnection, "\r\n");
fputs($smtpConnection, "$message\r\n");
fputs($smtpConnection, ".\r\n");
$smtpResponse = fgets($smtpConnection, 515);
if (substr($smtpResponse, 0, 3) !== '250') {
return false;
}
fputs($smtpConnection, "QUIT\r\n");
fclose($smtpConnection);
return true;
}
?>