New mailer class

master
Pavel Belyaev 4 years ago
parent ccc0888d0a
commit bc4c0ddddc

@ -1,12 +1,13 @@
<?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
{
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)
{
@ -53,16 +54,17 @@ class LE_MAIL
protected function html_wrapper($text)
{
$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;
}
protected function html_message($text)
{
$text = $this->encode($text);
$text = $this->html_wrapper($text);
$eol = $this->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;
return $body;
}

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

Loading…
Cancel
Save