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