chore: Добавлены аннотации к типам
This commit is contained in:
parent
48269bd424
commit
ad920f656c
14 changed files with 127 additions and 127 deletions
88
src/Mail.php
88
src/Mail.php
|
|
@ -4,7 +4,9 @@
|
|||
* Класс для работы с почтой
|
||||
* http://en.wikipedia.org/wiki/MIME
|
||||
*/
|
||||
|
||||
namespace ctiso;
|
||||
|
||||
use ctiso\Path,
|
||||
Exception;
|
||||
|
||||
|
|
@ -16,14 +18,15 @@ class Mail
|
|||
public $content;
|
||||
public $copy;
|
||||
|
||||
private $encoding;
|
||||
private $encoding;
|
||||
private $_notify = null;
|
||||
|
||||
protected $attachment = array ();
|
||||
protected $attachment = array();
|
||||
protected $uniqid;
|
||||
protected $type = "text/plain";
|
||||
|
||||
function __construct() {
|
||||
function __construct()
|
||||
{
|
||||
$this->setEncoding("UTF-8");
|
||||
$this->uniqid = strtoupper(uniqid((string)time())); // Идентефикатор разделителя
|
||||
}
|
||||
|
|
@ -31,32 +34,32 @@ class Mail
|
|||
/**
|
||||
* Установка отправителя
|
||||
*/
|
||||
function from($name)
|
||||
function from(string $name)
|
||||
{
|
||||
// filter_var($name, FILTER_VALIDATE_EMAIL);
|
||||
$this->_from = $name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Установка получателя
|
||||
*/
|
||||
function to($name) // recipient
|
||||
function to(string $name) // recipient
|
||||
{
|
||||
$this->_to = $name;
|
||||
}
|
||||
|
||||
function replyTo($name) // recipient
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
/**
|
||||
* Установка получателей копии
|
||||
*/
|
||||
function copy($name) // recipient cc
|
||||
function copy(string $name) // recipient cc
|
||||
{
|
||||
$this->copy = $name;
|
||||
}
|
||||
|
||||
function notify($notify)
|
||||
|
||||
function notify(string $notify)
|
||||
{
|
||||
$this->_notify = $notify;
|
||||
}
|
||||
|
|
@ -64,23 +67,23 @@ class Mail
|
|||
/**
|
||||
* Тема письма
|
||||
*/
|
||||
function subject($subject)
|
||||
function subject(string $subject)
|
||||
{
|
||||
$this->_subject = $subject;
|
||||
$this->_subject = $subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Текст письма
|
||||
*/
|
||||
function setContent($text)
|
||||
function setContent(string $text)
|
||||
{
|
||||
$this->content = $text;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Кодировка текста в письме
|
||||
*/
|
||||
function setEncoding($encoding)
|
||||
function setEncoding(string $encoding)
|
||||
{
|
||||
$this->encoding = $encoding;
|
||||
}
|
||||
|
|
@ -88,15 +91,13 @@ class Mail
|
|||
/**
|
||||
* Добавление вложения из файла
|
||||
*/
|
||||
function addAttachment($filename, $name = false)
|
||||
function addAttachment(string $filename, $name = false)
|
||||
{
|
||||
assert(is_string($filename));
|
||||
|
||||
if(file_exists($filename)) { // assert ??
|
||||
if (file_exists($filename)) { // assert ??
|
||||
$file = fopen($filename, "rb");
|
||||
if (is_resource($file)) {
|
||||
$data = fread($file, filesize($filename));
|
||||
$this->attachment [] = ($name) ? [$data, $name] : [$data, basename($filename)];
|
||||
$this->attachment[] = ($name) ? [$data, $name] : [$data, basename($filename)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -109,11 +110,9 @@ class Mail
|
|||
/**
|
||||
* Добавление вложения из строки с указанием имени файла
|
||||
*/
|
||||
function addAttachmentRaw($data, $name)
|
||||
function addAttachmentRaw($data, string $name)
|
||||
{
|
||||
assert(is_string($name));
|
||||
|
||||
$this->attachment [] = [$data, $name];
|
||||
$this->attachment[] = [$data, $name];
|
||||
}
|
||||
|
||||
function quote($var, $val)
|
||||
|
|
@ -127,33 +126,33 @@ class Mail
|
|||
*/
|
||||
function mimeTag($name, $value, array $args = [])
|
||||
{
|
||||
assert (is_string($name));
|
||||
assert (is_string($value));
|
||||
assert(is_string($name));
|
||||
assert(is_string($value));
|
||||
|
||||
return $name . ": " . $value . implode("", array_map([$this, 'quote'], array_keys($args), array_values($args))) . PHP_EOL;
|
||||
return $name . ": " . $value . implode("", array_map([$this, 'quote'], array_keys($args), array_values($args))) . PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @see http://tools.ietf.org/html/rfc2047
|
||||
*
|
||||
* @see http://tools.ietf.org/html/rfc2047
|
||||
*/
|
||||
function encodedWord($text, $encoding = 'B')
|
||||
function encodedWord(string $text, string $encoding = 'B')
|
||||
{
|
||||
return "=?{$this->encoding}?$encoding?".base64_encode($text)."?=";
|
||||
return "=?{$this->encoding}?$encoding?" . base64_encode($text) . "?=";
|
||||
}
|
||||
|
||||
/**
|
||||
* Тело сообщения
|
||||
*/
|
||||
function getMessage()
|
||||
function getMessage(): string
|
||||
{
|
||||
$message = "--".$this->uniqid . PHP_EOL;
|
||||
$message = "--" . $this->uniqid . PHP_EOL;
|
||||
$message .= $this->mimeTag("Content-Type", $this->type, ['charset' => $this->encoding]);
|
||||
$message .= $this->mimeTag("Content-Transfer-Encoding", "8bit");
|
||||
$message .= PHP_EOL . $this->content . PHP_EOL . PHP_EOL;
|
||||
|
||||
/*
|
||||
* Вложения
|
||||
* Вложения
|
||||
* http://tools.ietf.org/html/rfc2046#section-5.1.3
|
||||
*/
|
||||
foreach ($this->attachment as $value) {
|
||||
|
|
@ -171,7 +170,7 @@ class Mail
|
|||
/**
|
||||
* Заголовок сообщения
|
||||
*/
|
||||
function getHeader()
|
||||
function getHeader(): string
|
||||
{
|
||||
$head = $this->mimeTag("MIME-Version", "1.0");
|
||||
$head .= $this->mimeTag("From", $this->_from);
|
||||
|
|
@ -188,7 +187,7 @@ class Mail
|
|||
return $head;
|
||||
}
|
||||
|
||||
function getSubject()
|
||||
function getSubject(): string
|
||||
{
|
||||
return $this->encodedWord($this->_subject);
|
||||
}
|
||||
|
|
@ -196,27 +195,28 @@ class Mail
|
|||
/**
|
||||
* Вывод строки для сохранения в формате .eml
|
||||
*/
|
||||
function eml()
|
||||
function eml(): string
|
||||
{
|
||||
return "To: " . $this->_to . PHP_EOL . "Subject: {$this->getSubject()}" . PHP_EOL . $this->getHeader() . $this->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправка почты
|
||||
* Отправка почты
|
||||
*/
|
||||
function send()
|
||||
function send(): bool
|
||||
{
|
||||
$result = mail($this->_to, $this->getSubject(), $this->getMessage(), $this->getHeader());
|
||||
if(! $result) {
|
||||
if (! $result) {
|
||||
file_put_contents(Path::resolveFile("send.eml"), $this->eml());
|
||||
throw new Exception('Невозможно отправить почту');
|
||||
throw new Exception('Невозможно отправить почту');
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function clear() {
|
||||
function clear(): void
|
||||
{
|
||||
foreach ($this->attachment as $key => &$value) {
|
||||
unset($this->attachment[$key]);
|
||||
unset($this->attachment[$key]);
|
||||
}
|
||||
$this->attachment = [];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue