Отправляйте электронные письма ESMTP с использованием PHP по прямой SMTP

У меня есть почтовый сервер на Synology DiskStation. Я хочу отправлять электронные письма, используя php, но функция почты PHP не работает. Я прочитал, что почтовый сервер Synology использует ESMTP, а PHP использует sendmail, поэтому функция mail не работает, и мне нужно создать свое сообщение, используя прямой SMTP. Я нашел это:

<?php
function myMail($from, $namefrom, $to, $nameto, $subject, $message) {
$smtpServer = "xxx.xxx.xxx.xxx";
$port = "25";
$timeout = "45";                 // typical timeout. try 45 for slow servers
$username = "[email protected]";
$password = "secret";
$localhost = "example.com";
$newLine = "\r\n";
$secure = 0;
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect)) {
$output = "Failed to connect: $smtpResponse";
echo $output;
return $output;
}
else {
$logArray['connection'] = "<p>Connected to: $smtpResponse";
echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";

fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}

И это возвращает:
соединение принято
220 example.com ESMTP Postfix

продолжающийся
Но мое письмо не приходит.
Спасибо за помощь.

ПРИМЕЧАНИЕ. Имя хоста было заменено на example.com.

4

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]