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