1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Compolomus\Kmail; |
4
|
|
|
|
5
|
|
|
class KMail |
6
|
|
|
{ |
7
|
|
|
const VERSION = '1.1'; |
8
|
|
|
|
9
|
|
|
const RELEASE_DATE = '2017-06-26'; |
10
|
|
|
|
11
|
|
|
private $bound; |
12
|
|
|
|
13
|
|
|
private $header; |
14
|
|
|
|
15
|
|
|
private $subject = '(No subject)'; |
16
|
|
|
|
17
|
|
|
private $adress = []; |
18
|
|
|
|
19
|
|
|
private $body; |
20
|
|
|
|
21
|
|
|
public function __construct(string $text) |
22
|
|
|
{ |
23
|
|
|
$this->addBodyMessage($text); |
24
|
|
|
$this->bound(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function sendMailUtf8(string $adress, string $subject, string $message, string $header): bool |
28
|
|
|
{ |
29
|
|
|
return mail($adress, '=?UTF-8?B?' . base64_encode($subject) . '?=', $message, |
30
|
|
|
'MIME-Version: 1.0' . PHP_EOL . $header); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function send(): ?\Exception |
34
|
|
|
{ |
35
|
|
|
if (sizeof($this->adress) == 0) { |
36
|
|
|
throw new \Exception('Отсутствует адресат'); |
37
|
|
|
} else { |
38
|
|
|
$this->getHeader(); |
39
|
|
|
foreach ($this->adress as $adress) { |
40
|
|
|
$this->sendMailUtf8($adress, $this->getSubject(), $this->getBody(), $this->getHeader()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function addAdress(string $mail): KMail |
47
|
|
|
{ |
48
|
|
|
$this->adress[] = $mail; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function addBodyMessage(string $text): void |
53
|
|
|
{ |
54
|
|
|
$this->body .= '--' . $this->bound() . PHP_EOL . |
55
|
|
|
'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL . |
56
|
|
|
'Content-Disposition: inline' . PHP_EOL . |
57
|
|
|
'Content-Transfer-Encoding: base64' . PHP_EOL . PHP_EOL . |
58
|
|
|
chunk_split(base64_encode($text)) . PHP_EOL; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function addFile(string $fileName, string $fileStream): KMail |
62
|
|
|
{ |
63
|
|
|
$this->body .= PHP_EOL . '--' . $this->bound() . PHP_EOL . |
64
|
|
|
'Content-Type: application/octet-stream; name="' . $fileName . '"' . PHP_EOL . |
65
|
|
|
'Content-Disposition: attachment;' . PHP_EOL . |
66
|
|
|
' filename="' . $fileName . '"' . PHP_EOL . |
67
|
|
|
'Content-Transfer-Encoding: base64' . PHP_EOL . PHP_EOL . chunk_split(base64_encode($fileStream)); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function endBody(): string |
73
|
|
|
{ |
74
|
|
|
return '--' . $this->bound() . '--'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function getBody(): string |
78
|
|
|
{ |
79
|
|
|
return $this->body . $this->endBody(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function bound(): string |
83
|
|
|
{ |
84
|
|
|
if (!$this->bound) { |
85
|
|
|
$this->bound = '_=_Multipart_Boundary_' . substr(md5(uniqid()), 0, 8); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->bound; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setSubject(string $subject): KMail |
92
|
|
|
{ |
93
|
|
|
if ($subject) { |
94
|
|
|
$this->subject = $subject; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function getSubject(): string |
101
|
|
|
{ |
102
|
|
|
return $this->subject; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function getHeader(): string |
106
|
|
|
{ |
107
|
|
|
if (!$this->header) { |
108
|
|
|
$this->header = 'Content-Type: multipart/mixed; boundary="' . $this->bound() . '"' . PHP_EOL . 'X-Mailer: PHP' . PHP_EOL . 'Reply-To: No reply' . PHP_EOL; |
109
|
|
|
} |
110
|
|
|
return $this->header; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function debug(): void |
114
|
|
|
{ |
115
|
|
|
echo '<pre>' . print_r($this, true) . '</pre>'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|