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:
27: class NMail extends NMailMimePart
28: {
29:
30: const HIGH = 1;
31: const NORMAL = 3;
32: const LOW = 5;
33:
34:
35:
36: public static $defaultMailer = 'NSendmailMailer';
37:
38:
39: public static $defaultHeaders = array(
40: 'MIME-Version' => '1.0',
41: 'X-Mailer' => 'Nette Framework',
42: );
43:
44:
45: private $mailer;
46:
47:
48: private $attachments = array();
49:
50:
51: private $inlines = array();
52:
53:
54: private $html;
55:
56:
57: private $basePath;
58:
59:
60:
61: public function __construct()
62: {
63: foreach (self::$defaultHeaders as $name => $value) {
64: $this->setHeader($name, $value);
65: }
66: $this->setHeader('Date', date('r'));
67: }
68:
69:
70:
71: 72: 73: 74: 75: 76:
77: public function setFrom($email, $name = NULL)
78: {
79: $this->setHeader('From', $this->formatEmail($email, $name));
80: return $this;
81: }
82:
83:
84:
85: 86: 87: 88:
89: public function getFrom()
90: {
91: return $this->getHeader('From');
92: }
93:
94:
95:
96: 97: 98: 99: 100: 101:
102: public function addReplyTo($email, $name = NULL)
103: {
104: $this->setHeader('Reply-To', $this->formatEmail($email, $name), TRUE);
105: return $this;
106: }
107:
108:
109:
110: 111: 112: 113: 114:
115: public function setSubject($subject)
116: {
117: $this->setHeader('Subject', $subject);
118: return $this;
119: }
120:
121:
122:
123: 124: 125: 126:
127: public function getSubject()
128: {
129: return $this->getHeader('Subject');
130: }
131:
132:
133:
134: 135: 136: 137: 138: 139:
140: public function addTo($email, $name = NULL)
141: {
142: $this->setHeader('To', $this->formatEmail($email, $name), TRUE);
143: return $this;
144: }
145:
146:
147:
148: 149: 150: 151: 152: 153:
154: public function addCc($email, $name = NULL)
155: {
156: $this->setHeader('Cc', $this->formatEmail($email, $name), TRUE);
157: return $this;
158: }
159:
160:
161:
162: 163: 164: 165: 166: 167:
168: public function addBcc($email, $name = NULL)
169: {
170: $this->setHeader('Bcc', $this->formatEmail($email, $name), TRUE);
171: return $this;
172: }
173:
174:
175:
176: 177: 178: 179: 180: 181:
182: private function formatEmail($email, $name)
183: {
184: if (!$name && preg_match('#^(.+) +<(.*)>$#', $email, $matches)) {
185: return array($matches[2] => $matches[1]);
186: } else {
187: return array($email => $name);
188: }
189: }
190:
191:
192:
193: 194: 195: 196: 197:
198: public function setReturnPath($email)
199: {
200: $this->setHeader('Return-Path', $email);
201: return $this;
202: }
203:
204:
205:
206: 207: 208: 209:
210: public function getReturnPath()
211: {
212: return $this->getHeader('From');
213: }
214:
215:
216:
217: 218: 219: 220: 221:
222: public function setPriority($priority)
223: {
224: $this->setHeader('X-Priority', (int) $priority);
225: return $this;
226: }
227:
228:
229:
230: 231: 232: 233:
234: public function getPriority()
235: {
236: return $this->getHeader('X-Priority');
237: }
238:
239:
240:
241: 242: 243: 244: 245: 246:
247: public function setHtmlBody($html, $basePath = NULL)
248: {
249: $this->html = $html;
250: $this->basePath = $basePath;
251: return $this;
252: }
253:
254:
255:
256: 257: 258: 259:
260: public function getHtmlBody()
261: {
262: return $this->html;
263: }
264:
265:
266:
267: 268: 269: 270: 271: 272: 273:
274: public function addEmbeddedFile($file, $content = NULL, $contentType = NULL)
275: {
276: return $this->inlines[$file] = $this->createAttachment($file, $content, $contentType, 'inline')
277: ->setHeader('Content-ID', '<' . md5(uniqid('', TRUE)) . '>');
278: }
279:
280:
281:
282: 283: 284: 285: 286: 287: 288:
289: public function addAttachment($file, $content = NULL, $contentType = NULL)
290: {
291: return $this->attachments[] = $this->createAttachment($file, $content, $contentType, 'attachment');
292: }
293:
294:
295:
296: 297: 298: 299:
300: private function createAttachment($file, $content, $contentType, $disposition)
301: {
302: $part = new NMailMimePart;
303: if ($content === NULL) {
304: if (!is_file($file)) {
305: throw new FileNotFoundException("File '$file' not found.");
306: }
307: if (!$contentType && $info = getimagesize($file)) {
308: $contentType = $info['mime'];
309: }
310: $part->setBody(file_get_contents($file));
311: } else {
312: $part->setBody((string) $content);
313: }
314: $part->setContentType($contentType ? $contentType : 'application/octet-stream');
315: $part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64);
316: $part->setHeader('Content-Disposition', $disposition . '; filename="' . NString::fixEncoding(basename($file)) . '"');
317: return $part;
318: }
319:
320:
321:
322:
323:
324:
325:
326: 327: 328: 329:
330: public function send()
331: {
332: $this->getMailer()->send($this->build());
333: }
334:
335:
336:
337: 338: 339: 340: 341:
342: public function setMailer(IMailer $mailer)
343: {
344: $this->mailer = $mailer;
345: return $this;
346: }
347:
348:
349:
350: 351: 352: 353:
354: public function getMailer()
355: {
356: if ($this->mailer === NULL) {
357: if (is_string(self::$defaultMailer) && $a = strrpos(self::$defaultMailer, '\\')) self::$defaultMailer = substr(self::$defaultMailer, $a + 1);
358: $this->mailer = is_object(self::$defaultMailer) ? self::$defaultMailer : new self::$defaultMailer;
359: }
360: return $this->mailer;
361: }
362:
363:
364:
365: 366: 367: 368:
369: protected function build()
370: {
371: $mail = clone $this;
372: $hostname = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
373: $mail->setHeader('Message-ID', '<' . md5(uniqid('', TRUE)) . "@$hostname>");
374:
375: $mail->buildHtml();
376: $mail->buildText();
377:
378: $cursor = $mail;
379: if ($mail->attachments) {
380: $tmp = $cursor->setContentType('multipart/mixed');
381: $cursor = $cursor->addPart();
382: foreach ($mail->attachments as $value) {
383: $tmp->addPart($value);
384: }
385: }
386:
387: if ($mail->html != NULL) {
388: $tmp = $cursor->setContentType('multipart/alternative');
389: $cursor = $cursor->addPart();
390: $alt = $tmp->addPart();
391: if ($mail->inlines) {
392: $tmp = $alt->setContentType('multipart/related');
393: $alt = $alt->addPart();
394: foreach ($mail->inlines as $name => $value) {
395: $tmp->addPart($value);
396: }
397: }
398: $alt->setContentType('text/html', 'UTF-8')
399: ->setEncoding(preg_match('#[\x80-\xFF]#', $mail->html) ? self::ENCODING_8BIT : self::ENCODING_7BIT)
400: ->setBody($mail->html);
401: }
402:
403: $text = $mail->getBody();
404: $mail->setBody(NULL);
405: $cursor->setContentType('text/plain', 'UTF-8')
406: ->setEncoding(preg_match('#[\x80-\xFF]#', $text) ? self::ENCODING_8BIT : self::ENCODING_7BIT)
407: ->setBody($text);
408:
409: return $mail;
410: }
411:
412:
413:
414: 415: 416: 417:
418: protected function buildHtml()
419: {
420: if ($this->html instanceof ITemplate) {
421: $this->html->mail = $this;
422: if ($this->basePath === NULL && $this->html instanceof IFileTemplate) {
423: $this->basePath = dirname($this->html->getFile());
424: }
425: $this->html = $this->html->__toString(TRUE);
426: }
427:
428: if ($this->basePath !== FALSE) {
429: $cids = array();
430: preg_match_all('#(src\s*=\s*|background\s*=\s*|url\()(["\'])(?![a-z]+:|[/\\#])(.+?)\\2#i', $this->html, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
431: foreach (array_reverse($matches) as $m) {
432: $file = rtrim($this->basePath, '/\\') . '/' . $m[3][0];
433: $cid = isset($cids[$file]) ? $cids[$file] : $cids[$file] = substr($this->addEmbeddedFile($file)->getHeader("Content-ID"), 1, -1);
434: $this->html = substr_replace($this->html, "{$m[1][0]}{$m[2][0]}cid:$cid{$m[2][0]}", $m[0][1], strlen($m[0][0]));
435: }
436: }
437:
438: if (!$this->getSubject() && preg_match('#<title>(.+?)</title>#is', $this->html, $matches)) {
439: $this->setSubject(html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8'));
440: }
441: }
442:
443:
444:
445: 446: 447: 448:
449: protected function buildText()
450: {
451: $text = $this->getBody();
452: if ($text instanceof ITemplate) {
453: $text->mail = $this;
454: $this->setBody($text->__toString(TRUE));
455:
456: } elseif ($text == NULL && $this->html != NULL) {
457: $text = preg_replace('#<(style|script|head).*</\\1>#Uis', '', $this->html);
458: $text = preg_replace('#<t[dh][ >]#i', " $0", $text);
459: $text = preg_replace('#[ \t\r\n]+#', ' ', $text);
460: $text = preg_replace('#<(/?p|/?h\d|li|br|/tr)[ >/]#i', "\n$0", $text);
461:
462: $text = html_entity_decode(strip_tags($text), ENT_QUOTES, 'UTF-8');
463: $this->setBody(trim($text));
464: }
465: }
466:
467: }
468: