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