Passed
Branch master (9ff03d)
by compolom
02:32
created

KMail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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