1: <?php
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: class MailMimePart extends Object
27: {
28:
29: const ENCODING_BASE64 = 'base64',
30: ENCODING_7BIT = '7bit',
31: ENCODING_8BIT = '8bit',
32: ENCODING_QUOTED_PRINTABLE = 'quoted-printable';
33:
34:
35: const EOL = "\r\n";
36: const LINE_LENGTH = 76;
37:
38:
39: private $headers = array();
40:
41:
42: private $parts = array();
43:
44:
45: private $body;
46:
47:
48:
49: 50: 51: 52: 53: 54: 55:
56: public function setHeader($name, $value, $append = FALSE)
57: {
58: if (!$name || preg_match('#[^a-z0-9-]#i', $name)) {
59: throw new InvalidArgumentException("Header name must be non-empty alphanumeric string, '$name' given.");
60: }
61:
62: if ($value == NULL) {
63: if (!$append) {
64: unset($this->headers[$name]);
65: }
66:
67: } elseif (is_array($value)) {
68: $tmp = & $this->headers[$name];
69: if (!$append || !is_array($tmp)) {
70: $tmp = array();
71: }
72:
73: foreach ($value as $email => $recipient) {
74: if ($recipient !== NULL && !Strings::checkEncoding($recipient)) {
75: Validators::assert($recipient, 'unicode', "header '$name'");
76: }
77: if (preg_match('#[\r\n]#', $recipient)) {
78: throw new InvalidArgumentException("Name must not contain line separator.");
79: }
80: Validators::assert($email, 'email', "header '$name'");
81: $tmp[$email] = $recipient;
82: }
83:
84: } else {
85: $value = (string) $value;
86: if (!Strings::checkEncoding($value)) {
87: throw new InvalidArgumentException("Header is not valid UTF-8 string.");
88: }
89: $this->headers[$name] = preg_replace('#[\r\n]+#', ' ', $value);
90: }
91: return $this;
92: }
93:
94:
95:
96: 97: 98: 99: 100:
101: public function getHeader($name)
102: {
103: return isset($this->headers[$name]) ? $this->headers[$name] : NULL;
104: }
105:
106:
107:
108: 109: 110: 111: 112:
113: public function clearHeader($name)
114: {
115: unset($this->headers[$name]);
116: return $this;
117: }
118:
119:
120:
121: 122: 123: 124: 125: 126:
127: public function getEncodedHeader($name)
128: {
129: $offset = strlen($name) + 2;
130:
131: if (!isset($this->headers[$name])) {
132: return NULL;
133:
134: } elseif (is_array($this->headers[$name])) {
135: $s = '';
136: foreach ($this->headers[$name] as $email => $name) {
137: if ($name != NULL) {
138: $s .= self::encodeHeader(
139: strpbrk($name, '.,;<@>()[]"=?') ? '"' . addcslashes($name, '"\\') . '"' : $name,
140: $offset
141: );
142: $email = " <$email>";
143: }
144: $email .= ',';
145: if ($s !== '' && $offset + strlen($email) > self::LINE_LENGTH) {
146: $s .= self::EOL . "\t";
147: $offset = 1;
148: }
149: $s .= $email;
150: $offset += strlen($email);
151: }
152: return substr($s, 0, -1);
153:
154: } elseif (preg_match('#^(\S+; (?:file)?name=)"(.*)"$#', $this->headers[$name], $m)) {
155: $offset += strlen($m[1]);
156: return $m[1] . '"' . self::encodeHeader($m[2], $offset) . '"';
157:
158: } else {
159: return self::encodeHeader($this->headers[$name], $offset);
160: }
161: }
162:
163:
164:
165: 166: 167: 168:
169: public function getHeaders()
170: {
171: return $this->headers;
172: }
173:
174:
175:
176: 177: 178: 179: 180: 181:
182: public function setContentType($contentType, $charset = NULL)
183: {
184: $this->setHeader('Content-Type', $contentType . ($charset ? "; charset=$charset" : ''));
185: return $this;
186: }
187:
188:
189:
190: 191: 192: 193: 194:
195: public function setEncoding($encoding)
196: {
197: $this->setHeader('Content-Transfer-Encoding', $encoding);
198: return $this;
199: }
200:
201:
202:
203: 204: 205: 206:
207: public function getEncoding()
208: {
209: return $this->getHeader('Content-Transfer-Encoding');
210: }
211:
212:
213:
214: 215: 216: 217: 218:
219: public function addPart(MailMimePart $part = NULL)
220: {
221: return $this->parts[] = $part === NULL ? new self : $part;
222: }
223:
224:
225:
226: 227: 228: 229: 230:
231: public function setBody($body)
232: {
233: $this->body = $body;
234: return $this;
235: }
236:
237:
238:
239: 240: 241: 242:
243: public function getBody()
244: {
245: return $this->body;
246: }
247:
248:
249:
250:
251:
252:
253:
254: 255: 256: 257:
258: public function generateMessage()
259: {
260: $output = '';
261: $boundary = '--------' . Strings::random();
262:
263: foreach ($this->headers as $name => $value) {
264: $output .= $name . ': ' . $this->getEncodedHeader($name);
265: if ($this->parts && $name === 'Content-Type') {
266: $output .= ';' . self::EOL . "\tboundary=\"$boundary\"";
267: }
268: $output .= self::EOL;
269: }
270: $output .= self::EOL;
271:
272: $body = (string) $this->body;
273: if ($body !== '') {
274: switch ($this->getEncoding()) {
275: case self::ENCODING_QUOTED_PRINTABLE:
276: $output .= function_exists('quoted_printable_encode') ? quoted_printable_encode($body) : self::encodeQuotedPrintable($body);
277: break;
278:
279: case self::ENCODING_BASE64:
280: $output .= rtrim(chunk_split(base64_encode($body), self::LINE_LENGTH, self::EOL));
281: break;
282:
283: case self::ENCODING_7BIT:
284: $body = preg_replace('#[\x80-\xFF]+#', '', $body);
285:
286:
287: case self::ENCODING_8BIT:
288: $body = str_replace(array("\x00", "\r"), '', $body);
289: $body = str_replace("\n", self::EOL, $body);
290: $output .= $body;
291: break;
292:
293: default:
294: throw new InvalidStateException('Unknown encoding.');
295: }
296: }
297:
298: if ($this->parts) {
299: if (substr($output, -strlen(self::EOL)) !== self::EOL) {
300: $output .= self::EOL;
301: }
302: foreach ($this->parts as $part) {
303: $output .= '--' . $boundary . self::EOL . $part->generateMessage() . self::EOL;
304: }
305: $output .= '--' . $boundary.'--';
306: }
307:
308: return $output;
309: }
310:
311:
312:
313:
314:
315:
316:
317: 318: 319: 320: 321: 322: 323:
324: private static function encodeHeader($s, & $offset = 0)
325: {
326: $o = '';
327: if ($offset >= 55) {
328: $o = self::EOL . "\t";
329: $offset = 1;
330: }
331:
332: if (strspn($s, "!\"#$%&\'()*+,-./0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}=? _\r\n\t") === strlen($s)
333: && ($offset + strlen($s) <= self::LINE_LENGTH)
334: ) {
335: $offset += strlen($s);
336: return $o . $s;
337: }
338:
339: $o .= str_replace("\n ", "\n\t", substr(iconv_mime_encode(str_repeat(' ', $offset), $s, array(
340: 'scheme' => 'B',
341: 'input-charset' => 'UTF-8',
342: 'output-charset' => 'UTF-8',
343: )), $offset + 2));
344:
345: $offset = strlen($o) - strrpos($o, "\n");
346: return $o;
347: }
348:
349:
350:
351: 352: 353: 354: 355: public static function encodeQuotedPrintable($s)
356: {
357: $range = '!"#$%&\'()*+,-./0123456789:;<>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}';
358: $pos = 0;
359: $len = 0;
360: $o = '';
361: $size = strlen($s);
362: while ($pos < $size) {
363: if ($l = strspn($s, $range, $pos)) {
364: while ($len + $l > self::LINE_LENGTH - 1) {
365: $lx = self::LINE_LENGTH - $len - 1;
366: $o .= substr($s, $pos, $lx) . '=' . self::EOL;
367: $pos += $lx;
368: $l -= $lx;
369: $len = 0;
370: }
371: $o .= substr($s, $pos, $l);
372: $len += $l;
373: $pos += $l;
374:
375: } else {
376: $len += 3;
377: if ($len > self::LINE_LENGTH - 1) {
378: $o .= '=' . self::EOL;
379: $len = 3;
380: }
381: $o .= '=' . strtoupper(bin2hex($s[$pos]));
382: $pos++;
383: }
384: }
385: return rtrim($o, '=' . self::EOL);
386: }
387:
388: }
389: