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