New mailer class

master
Pavel Belyaev 4 years ago
parent ccc0888d0a
commit bc4c0ddddc

@ -1,12 +1,13 @@
<?php <?php
/****************************************************************************** /********************************************************************************
| LE MAIL v0.1 25.08.2021 by Pavel Belyaev, https://github.com/TechResearchRu | | LE MAIL v0.1.1 04.09.2021 by Pavel Belyaev, https://github.com/TechResearchRu |
| данный функционал требует тестирования, распространяется КАКЕСТЬ | | данный функционал требует тестирования, распространяется КАКЕСТЬ |
******************************************************************************/ ********************************************************************************/
class LE_MAIL class LE_MAIL
{ {
protected $eol="\r\n", $separator="123LE", $charset="windows-1251", $sep; protected $eol="\r\n", $separator="qwe112233qwe", $charset="windows-1251";
protected $sep;
protected function encode($body) protected function encode($body)
{ {
@ -53,16 +54,17 @@ class LE_MAIL
protected function html_wrapper($text) protected function html_wrapper($text)
{ {
$html ='<html><head><META http-equiv="Content-Type" content="text/html; charset='.$this->charset.'"></head>'; $html ='<html><head><META http-equiv="Content-Type" content="text/html; charset='.$this->charset.'"></head>';
$html .='<body>'.$this->encode($text).'</body></html>'; $html .='<body>'.$text.'</body></html>';
return $html; return $html;
} }
protected function html_message($text) protected function html_message($text)
{ {
$text = $this->encode($text);
$text = $this->html_wrapper($text); $text = $this->html_wrapper($text);
$eol = $this->eol; $eol = $this->eol;
$body = $this->sep.'Content-Type: text/html; charset="'.$this->charset.'"'.$eol; $body = $this->sep.'Content-Type: text/html; charset="'.$this->charset.'"'.$eol;
$body .= "Content-Transfer-Encoding: Quot-Printed\n".$eol; $body .= "Content-Transfer-Encoding: Quot-Printed\n\n".$eol;
$body .= $text . $eol; $body .= $text . $eol;
return $body; return $body;
} }

@ -6,6 +6,7 @@ if(!defined("I")) die;
//статический класс для отправки уведомлений //статический класс для отправки уведомлений
class LE_MAIL class LE_MAIL
{ {
/*
//отправка текстового письма Mail::send("электронный@адрес", "адрес отправителя", "текст письма", "тема_письма") //отправка текстового письма Mail::send("электронный@адрес", "адрес отправителя", "текст письма", "тема_письма")
public static function send($address,$sender,$mail_body,$subject) public static function send($address,$sender,$mail_body,$subject)
{ {
@ -57,57 +58,126 @@ class LE_MAIL
$multipart .= $message_part."--$boundary--\n"; $multipart .= $message_part."--$boundary--\n";
if(!mail($address, $subject, $multipart, $headers)) {echo "К сожалению, письмо не отправлено"; exit();} if(!mail($address, $subject, $multipart, $headers)) {echo "К сожалению, письмо не отправлено"; exit();}
} }
*/
public $eol="\r\n";
public function encode($body)
{
return iconv('UTF-8', 'windows-1251//TRANSLIT', $body);
}
public function gen_message($mailfrom,$mailto,$body,$subject,$file=false) public function subject($subject)
{ {
$separator = md5(time()); $subject = base64_encode($this->encode($subject));
return '=?windows-1251?B?'.$subject.'?=';
}
$filename = 'myfile'; //type=0 - html, 1 - multipath/mixed
$path = 'your path goes here'; public function headers($from,$type,$boundary)
$file = $path . "/" . $filename; {
$eol = $this->eol;
$sep = $this->separator;
//1
$headers = "";
$headers .="MIME-Version: 1.0".$eol;
$mailto = 'mail@mail.com'; //if ($type)
$subject = 'Subject'; $headers .="Content-Type: multipart/mixed; boundary=\"$sep\"\n";
$message = 'My message'; //else
// $headers .="Content-Type: text/html; charset=windows-1251".$eol;
$content = file_get_contents($file); $headers .="From: ".$from.$eol;
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
//$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
//$headers .= "This is a MIME encoded message." . $eol;
// carriage return type (RFC) return $headers;
$eol = "\r\n";
// main header (multipart mandatory) }
$headers = "From: name <test@test.com>" . $eol; public $separator="qwe112233qwe";
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message public function attach_info($path)
$body = "--" . $separator . $eol; {
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol; $path_parts = pathinfo($path);
$body .= "Content-Transfer-Encoding: 8bit" . $eol; $filename = is_set($res['filename']) ? $res['filename'] : '';
$body .= $message . $eol; $file_ext = is_set($res['extension']) ? ".".$res['extension'] : '';
$file = $filename.$file_ext;
$mime = mime_content_type($path);
if ($mime===false) $mime="application/octet-stream";
return [$file,$mime];
}
public function attachment($path=null)
{
if ($path===null || $path===false || !is_file($path)) return false;
$eol = $this->eol;
$sep = $this->separator;
list ($file,$mime) = $this->attach_info($path)
// attachment $content = file_get_contents($path);
$body .= "--" . $separator . $eol; $content = chunk_split(base64_encode($content));
$body .= "--" . $sep . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol; $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol; $body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol; $body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol; $body .= $content . $eol;
$body .= "--" . $separator . "--"; return $body;
}
//SEND Mail public function html_wrapper($text)
if (mail($mailto, $subject, $body, $headers)) { {
echo "mail send ... OK"; // or use booleans here $html ='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional //EN">';
} else { $html .='<html><head><META http-equiv="Content-Type" content="text/html; charset=windows-1251"></head>';
echo "mail send ... ERROR!"; $html .='<body>'.$text.'</body></html>';
print_r( error_get_last() );
return $html;
}
public function html_message($text)
{
$text = $this->encode($text);
$text = $this->html_wrapper($text);
$eol = $this->eol;
$sep = $this->separator;
$body = "--" . $sep . $eol;
$body .= 'Content-Type: text/html; charset="windows-1251"'.$eol;
$body .= "Content-Transfer-Encoding: Quot-Printed\n\n".$eol;
//$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $text . $eol;
return $body;
}
public function gen_message($mailfrom,$mailto,$body,$subject,$files=false)
{
$this->separator = md5(time());
$eol = $this->eol;
// main header (multipart mandatory)
$headers = $this->headers($from,($file!==null),$boundary);
// message
$body = $this->html_message($message);
if ($files!==false && is_string($files)) $files = [$files];
if (is_array($files))
{
foreach($files as $k=>$path)
{
$body.=$this->attachment($path);
}
} }
$body .= "--" . $this->separator . "--";
//SEND Mail
return mail($mailto, $subject, $body, $headers);
} }

@ -1,4 +1,4 @@
<? <?php
/** /**
* TEST BETA, need refactoring * TEST BETA, need refactoring

Loading…
Cancel
Save