Всех приветствую! Нужно сделать так, чтобы скрипт отправлял письмо на email. Письмо доходит до адресата, но оно почему-то пустое. Что я делаю не так?
Вот заголовки письма:
Date: Mon, 11 Jun 2018 4:53:21 +0300 (MSK/MSD)
From: <from@domen.ru>
X-Mailer: Yamail [ http://yandex.ru ] 5.0
Reply-To: <from@domen.ru>>
X-Priority: 3 (Normal)
Message-ID: <1528692801@salettalocal.ru>
Subject: Тема письма
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="-----SD6ty54H54JDGSHJD"
To: <gfdgfd@mai.ru>
-----SD6ty54H54JDGSHJD
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
<p>Hello world!</p>
-----SD6ty54H54JDGSHJD--
.
<?php
class Mail {
private $smtpConnect;
private $login, $password;
private $path;
private $separator = '-----SD6ty54H54JDGSHJD';
public function __construct($login, $password) {
$this->login = $login;
$this->password = $password;
$this->smtpConnect = fsockopen('mail.saletta.ru', 25, $errno, $errstr, 10);
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, "EHLO mail.saletta.ru\r\n");
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, "AUTH LOGIN\r\n");
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, base64_encode($login) . "\r\n");
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, base64_encode($password) . "\r\n");
$data = $this->getData($this->smtpConnect);
}
public function send($to, $msg, $files = []) {
$this->path = 'templates/texts/emails/' . $msg . '.tpl';
if(!file_exists($this->path)) {
$this->close();
exit('Файл почты <b>' . basename($this->path) . '</b> не существует');
}
$msg = file_get_contents($this->path);
$subject = $this->getSubject($msg);
$text = $this->getHeadersByText($this->getText($msg));
$headers = $this->getHeaders($to, $subject);
if(!empty($files)) $text = $this->addFiles($text, $files);
if(is_array($to)) {
$result = [];
for($i = 0, $count = count($to); $i < $count; $i++) {
$headers .= 'To: <' . $to[$i] . ">\r\n";
$result[] = $this->sendMail($this->login, $to[$i], $headers, $text);
} return $result;
}
$headers .= 'To: <' . $to . ">\r\n";
return $this->sendMail($this->login, $to, $headers, $text);
}
private function sendMail($from, $to, $headers, $text) {
fputs($this->smtpConnect, 'MAIL FROM:' . $from . "\r\n");
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, 'RCPT TO:' . $to . "\r\n");
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, "DATA\r\n");
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, $headers . "\r\n" . $text . "\r\n" . $this->separator . "--\r\n.\r\n");
$data = $this->getData($this->smtpConnect);
fputs($this->smtpConnect, "RSET\r\n");
$data = $this->getData($this->smtpConnect);
return $data;
}
public function close() {
fputs($this->smtpConnect, "QUIT\r\n");
$data = $this->getData($this->smtpConnect);
return $data;
}
private function getData($smtpConnect) {
$data = '';
while($str = fgets($smtpConnect, 515)) {
$data .= $str;
if(substr($str, 3, 1) == ' ') break;
} return $data;
}
private function getHeaders($to, $subject) {
$headers = 'Date: ' . date('D, j M Y G:i:s') . " +0300 (MSK/MSD)\r\n";
$headers .= 'From: <' . $this->login . ">\r\n";
$headers .= "X-Mailer: Yamail [ http://yandex.ru ] 5.0\r\n";
$headers .= 'Reply-To: <' . $this->login . ">\r\n";
$headers .= "X-Priority: 3 (Normal)\r\n";
$headers .= 'Message-ID: <' . time() . '@' . Config::ADDRESS2 . ">\r\n";
$headers .= 'Subject: ' . $subject . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed; boundary=\"" . $this->separator . "\"\r\n";
return $headers;
}
private function getHeadersByText($text) {
$headers = $this->separator . "\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$text = $headers . $text;
return $text;
}
private function getSubject($msg) {
$pattern = '/\{subject\}(.*)\{\/subject\}/';
preg_match($pattern, $msg, $m);
return $m[1];
}
private function getText($msg) {
$pattern = '/\{subject\}(.*)\{\/subject\}/';
$msg = preg_replace($pattern, '', $msg, 1);
return $msg;
}
private function addFiles($text, $files) {
for($i = 0, $count = count($files); $i < $count; $i++) {
$name = basename($files[0]);
$text .= "\r\n\r\n" . $this->separator . "\r\n";
$text .= "Content-Type: application/octet-stream; name=\"$name\"\r\n";
$text .= "Content-transfer-encoding: base64\r\n";
$text .= "Content-Disposition: attachment; filename=\"$name\"\r\n";
$text .= chunk_split(base64_encode(fread(fopen($files[0], 'rb'), filesize($files[0]))));
}
return $text;
}
}