1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
<?php
class Mail
{
private $from = ['name' => '', 'email' => ''];
private $to = [];
private $subject = '';
private $message = '';
private $files = [];
private $multipart = false;
private $boundary = '';
private $uniqId = '';
private $replyTo = [];
private $timestamp = null;
const CRLF = "\r\n";
public function __construct()
{
$this->uniqId = '<php-mail-' . md5(microtime()) . mt_rand() . '@git.php.net>';
}
/**
* Return unique id of mail
* @return string unique Id of message in format: '<php-mail-...@git.php.net';
*/
public function getId()
{
return $this->uniqId;
}
/**
* Add parent mail for this mail
* @param string $uniqId unique Id of message in format: '<php-mail-...@git.php.net';
*/
public function addReplyTo($uniqId)
{
$this->replyTo[] = $uniqId;
}
/**
* Add attached text file to mail
* @param string $name unique file name
* @param string $data file content
*/
public function addTextFile($name , $data)
{
$this->files[trim($name)] = chunk_split(base64_encode($data), 76, self::CRLF);
}
/**
* Return length of attached file
* @param string $name unique file name
* @return int file length
*/
public function getFileLength($name)
{
$name = trim($name);
return isset($this->files[$name]) ? strlen($this->files[$name]) : 0;
}
/**
* Delete attached file
* @param string $name unique file name
*/
public function dropFile($name)
{
$name = trim($name);
unset($this->files[$name]);
}
/**
* Set "From" address
* @param string $email email author address
* @param string $name author name
*/
public function setFrom($email, $name = '')
{
$this->from = ['email' => trim($email), 'name' => trim($name)];
}
/**
* Add recipient address
* @param string $email recipient address
* @param string $name recipient name
*/
public function addTo($email, $name = '')
{
$this->to[] = ['email' => trim($email), 'name' => trim($name)];
}
/**
* Set mail subject
* @param string $subject subject
*/
public function setSubject($subject)
{
$this->subject = trim($subject);
}
/**
* Set timestamp
* @param string $timestamp timestamp
*/
public function setTimestamp($timestamp)
{
$this->timestamp = trim($timestamp);
}
/**
* Set mail body text
* @param string $message body text
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* Format header string
* @param string $name header name
* @param string $value header value
* @return string header string
*/
private function makeHeader($name, $value)
{
return $name . ': ' . $value;
}
/**
* Format address string
* @param array $address array with email adress and name
* @return string address string
*/
private function makeAddress(array $address)
{
return $address['name'] ? $this->utf8SafeEncode($address['name'], 100) . ' <'. $address['email'] . '>' : $address['email'];
}
/**
* Cut end encode string by mb_encode_mimeheader
* @param string $value utf8 string
* @param int $maxLenght max length
* @return string encoded string
*/
private function utf8SafeEncode($value, $maxLenght = null)
{
if ($maxLenght) $value = mb_substr($value, 0, $maxLenght);
return mb_encode_mimeheader($value, 'UTF-8', 'Q');
}
/**
* Prepare heade part of mail
* @return string header part of mail
*/
private function makeHeaders()
{
$headers = [];
$headers[] = $this->makeHeader('From', $this->makeAddress($this->from));
$headers[] = $this->makeHeader('Message-ID', $this->uniqId);
if (count($this->replyTo)) {
$replyTo = implode(' ', $this->replyTo);
$headers[] = $this->makeHeader('References', $replyTo);
$headers[] = $this->makeHeader('In-Reply-To', $replyTo);
}
$headers[] = $this->makeHeader('MIME-Version', '1.0');
$headers[] = $this->makeHeader('Date', date(DATE_RFC2822, $this->timestamp ?: time()));
if ($this->multipart) {
$this->boundary = sha1($this->uniqId);
$headers[] = $this->makeHeader('Content-Type', 'multipart/mixed; boundary="' . $this->boundary . '"');
} else {
$headers[] = $this->makeHeader('Content-Type', 'text/plain; charset="utf-8"');
// we use base64 for avoiding some problems sush string length limit, safety encoding etc.
$headers[] = $this->makeHeader('Content-Transfer-Encoding', 'quoted-printable');
}
return implode(self::CRLF , $headers);
}
/**
* Prepare body part of mail
* @return string mail body
*/
private function makeBody()
{
$body = '';
if ($this->multipart) {
$body .= '--' . $this->boundary . self::CRLF;
$body .= $this->makeHeader('Content-Type', 'text/plain; charset="utf-8"') . self::CRLF;
$body .= $this->makeHeader('Content-Transfer-Encoding', 'quoted-printable') . self::CRLF;
$body .= self::CRLF;
$body .= quoted_printable_encode($this->message);
foreach ($this->files as $name => $data) {
$body .= self::CRLF . '--' . $this->boundary . self::CRLF;
$body .= $this->makeHeader('Content-Type', 'text/plain; charset="utf-8"') . self::CRLF;
$body .= $this->makeHeader('Content-Transfer-Encoding', 'base64') . self::CRLF;
$body .= $this->makeHeader('Content-Disposition', 'attachment; filename="' . $name . '"') . self::CRLF;
$body .= self::CRLF;
$body .= $data;
}
$body .= self::CRLF . '--' . $this->boundary . '--';
} else {
$body = quoted_printable_encode($this->message);
}
return $body;
}
/**
* Send current mail
* @return bool
*/
public function send()
{
$this->multipart = (bool) count($this->files);
$receivers = implode(', ', array_map([$this, 'makeAddress'], $this->to));
$subject = $this->utf8SafeEncode($this->subject, 450);
$headers = $this->makeHeaders();
$body = $this->makeBody();
return mail($receivers, $subject, $body, $headers, "-f noreply@php.net");
}
}
|