Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • Mail
  • MailMimePart
  • SendmailMailer

Interfaces

  • IMailer
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Mail;
 13: 
 14: use Nette,
 15:     Nette\String;
 16: 
 17: 
 18: 
 19: /**
 20:  * Mail provides functionality to compose and send both text and MIME-compliant multipart e-mail messages.
 21:  *
 22:  * @author     David Grudl
 23:  *
 24:  * @property   string $from
 25:  * @property   string $subject
 26:  * @property   string $returnPath
 27:  * @property   int $priority
 28:  * @property   string $htmlBody
 29:  */
 30: class Mail extends MailMimePart
 31: {
 32:     /**#@+ Priority */
 33:     const HIGH = 1;
 34:     const NORMAL = 3;
 35:     const LOW = 5;
 36:     /**#@-*/
 37: 
 38:     /** @var IMailer */
 39:     public static $defaultMailer = 'Nette\Mail\SendmailMailer';
 40: 
 41:     /** @var array */
 42:     public static $defaultHeaders = array(
 43:         'MIME-Version' => '1.0',
 44:         'X-Mailer' => 'Nette Framework',
 45:     );
 46: 
 47:     /** @var IMailer */
 48:     private $mailer;
 49: 
 50:     /** @var array */
 51:     private $attachments = array();
 52: 
 53:     /** @var array */
 54:     private $inlines = array();
 55: 
 56:     /** @var mixed */
 57:     private $html;
 58: 
 59:     /** @var string */
 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:      * Sets the sender of the message.
 76:      * @param  string  e-mail or format "John Doe" <doe@example.com>
 77:      * @param  string
 78:      * @return Mail  provides a fluent interface
 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:      * Returns the sender of the message.
 90:      * @return array
 91:      */
 92:     public function getFrom()
 93:     {
 94:         return $this->getHeader('From');
 95:     }
 96: 
 97: 
 98: 
 99:     /**
100:      * Adds the reply-to address.
101:      * @param  string  e-mail or format "John Doe" <doe@example.com>
102:      * @param  string
103:      * @return Mail  provides a fluent interface
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:      * Sets the subject of the message.
115:      * @param  string
116:      * @return Mail  provides a fluent interface
117:      */
118:     public function setSubject($subject)
119:     {
120:         $this->setHeader('Subject', $subject);
121:         return $this;
122:     }
123: 
124: 
125: 
126:     /**
127:      * Returns the subject of the message.
128:      * @return string
129:      */
130:     public function getSubject()
131:     {
132:         return $this->getHeader('Subject');
133:     }
134: 
135: 
136: 
137:     /**
138:      * Adds email recipient.
139:      * @param  string  e-mail or format "John Doe" <doe@example.com>
140:      * @param  string
141:      * @return Mail  provides a fluent interface
142:      */
143:     public function addTo($email, $name = NULL) // addRecipient()
144:     {
145:         $this->setHeader('To', $this->formatEmail($email, $name), TRUE);
146:         return $this;
147:     }
148: 
149: 
150: 
151:     /**
152:      * Adds carbon copy email recipient.
153:      * @param  string  e-mail or format "John Doe" <doe@example.com>
154:      * @param  string
155:      * @return Mail  provides a fluent interface
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:      * Adds blind carbon copy email recipient.
167:      * @param  string  e-mail or format "John Doe" <doe@example.com>
168:      * @param  string
169:      * @return Mail  provides a fluent interface
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:      * Formats recipient e-mail.
181:      * @param  string
182:      * @param  string
183:      * @return array
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:      * Sets the Return-Path header of the message.
198:      * @param  string  e-mail
199:      * @return Mail  provides a fluent interface
200:      */
201:     public function setReturnPath($email)
202:     {
203:         $this->setHeader('Return-Path', $email);
204:         return $this;
205:     }
206: 
207: 
208: 
209:     /**
210:      * Returns the Return-Path header.
211:      * @return string
212:      */
213:     public function getReturnPath()
214:     {
215:         return $this->getHeader('From');
216:     }
217: 
218: 
219: 
220:     /**
221:      * Sets email priority.
222:      * @param  int
223:      * @return Mail  provides a fluent interface
224:      */
225:     public function setPriority($priority)
226:     {
227:         $this->setHeader('X-Priority', (int) $priority);
228:         return $this;
229:     }
230: 
231: 
232: 
233:     /**
234:      * Returns email priority.
235:      * @return int
236:      */
237:     public function getPriority()
238:     {
239:         return $this->getHeader('X-Priority');
240:     }
241: 
242: 
243: 
244:     /**
245:      * Sets HTML body.
246:      * @param  string|Nette\Templates\ITemplate
247:      * @param  mixed base-path or FALSE to disable parsing
248:      * @return Mail  provides a fluent interface
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:      * Gets HTML body.
261:      * @return mixed
262:      */
263:     public function getHtmlBody()
264:     {
265:         return $this->html;
266:     }
267: 
268: 
269: 
270:     /**
271:      * Adds embedded file.
272:      * @param  string
273:      * @param  string
274:      * @param  string
275:      * @return MailMimePart
276:      */
277:     public function addEmbeddedFile($file, $content = NULL, $contentType = NULL)
278:     {
279:         return $this->inlines[$file] = $this->createAttachment($file, $content, $contentType, 'inline')
280:             ->setHeader('Content-ID', '<' . md5(uniqid('', TRUE)) . '>');
281:     }
282: 
283: 
284: 
285:     /**
286:      * Adds attachment.
287:      * @param  string
288:      * @param  string
289:      * @param  string
290:      * @return MailMimePart
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:      * Creates file MIME part.
301:      * @return MailMimePart
302:      */
303:     private function createAttachment($file, $content, $contentType, $disposition)
304:     {
305:         $part = new MailMimePart;
306:         if ($content === NULL) {
307:             if (!is_file($file)) {
308:                 throw new \FileNotFoundException("File '$file' not found.");
309:             }
310:             if (!$contentType && $info = getimagesize($file)) {
311:                 $contentType = $info['mime'];
312:             }
313:             $part->setBody(file_get_contents($file));
314:         } else {
315:             $part->setBody((string) $content);
316:         }
317:         $part->setContentType($contentType ? $contentType : 'application/octet-stream');
318:         $part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64);
319:         $part->setHeader('Content-Disposition', $disposition . '; filename="' . String::fixEncoding(basename($file)) . '"');
320:         return $part;
321:     }
322: 
323: 
324: 
325:     /********************* building and sending ****************d*g**/
326: 
327: 
328: 
329:     /**
330:      * Sends e-mail.
331:      * @return void
332:      */
333:     public function send()
334:     {
335:         $this->getMailer()->send($this->build());
336:     }
337: 
338: 
339: 
340:     /**
341:      * Sets the mailer.
342:      * @param  IMailer
343:      * @return Mail  provides a fluent interface
344:      */
345:     public function setMailer(IMailer $mailer)
346:     {
347:         $this->mailer = $mailer;
348:         return $this;
349:     }
350: 
351: 
352: 
353:     /**
354:      * Returns mailer.
355:      * @return IMailer
356:      */
357:     public function getMailer()
358:     {
359:         if ($this->mailer === NULL) {
360:             $this->mailer = is_object(self::$defaultMailer) ? self::$defaultMailer : new self::$defaultMailer;
361:         }
362:         return $this->mailer;
363:     }
364: 
365: 
366: 
367:     /**
368:      * Builds e-mail.
369:      * @return void
370:      */
371:     protected function build()
372:     {
373:         $mail = clone $this;
374:         $hostname = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
375:         $mail->setHeader('Message-ID', '<' . md5(uniqid('', TRUE)) . "@$hostname>");
376: 
377:         $mail->buildHtml();
378:         $mail->buildText();
379: 
380:         $cursor = $mail;
381:         if ($mail->attachments) {
382:             $tmp = $cursor->setContentType('multipart/mixed');
383:             $cursor = $cursor->addPart();
384:             foreach ($mail->attachments as $value) {
385:                 $tmp->addPart($value);
386:             }
387:         }
388: 
389:         if ($mail->html != NULL) { // intentionally ==
390:             $tmp = $cursor->setContentType('multipart/alternative');
391:             $cursor = $cursor->addPart();
392:             $alt = $tmp->addPart();
393:             if ($mail->inlines) {
394:                 $tmp = $alt->setContentType('multipart/related');
395:                 $alt = $alt->addPart();
396:                 foreach ($mail->inlines as $name => $value) {
397:                     $tmp->addPart($value);
398:                 }
399:             }
400:             $alt->setContentType('text/html', 'UTF-8')
401:                 ->setEncoding(preg_match('#[\x80-\xFF]#', $mail->html) ? self::ENCODING_8BIT : self::ENCODING_7BIT)
402:                 ->setBody($mail->html);
403:         }
404: 
405:         $text = $mail->getBody();
406:         $mail->setBody(NULL);
407:         $cursor->setContentType('text/plain', 'UTF-8')
408:             ->setEncoding(preg_match('#[\x80-\xFF]#', $text) ? self::ENCODING_8BIT : self::ENCODING_7BIT)
409:             ->setBody($text);
410: 
411:         return $mail;
412:     }
413: 
414: 
415: 
416:     /**
417:      * Builds HTML content.
418:      * @return void
419:      */
420:     protected function buildHtml()
421:     {
422:         if ($this->html instanceof Nette\Templates\ITemplate) {
423:             $this->html->mail = $this;
424:             if ($this->basePath === NULL && $this->html instanceof Nette\Templates\IFileTemplate) {
425:                 $this->basePath = dirname($this->html->getFile());
426:             }
427:             $this->html = $this->html->__toString(TRUE);
428:         }
429: 
430:         if ($this->basePath !== FALSE) {
431:             $cids = array();
432:             preg_match_all('#(src\s*=\s*|background\s*=\s*|url\()(["\'])(?![a-z]+:|[/\\#])(.+?)\\2#i', $this->html, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
433:             foreach (array_reverse($matches) as $m) {
434:                 $file = rtrim($this->basePath, '/\\') . '/' . $m[3][0];
435:                 $cid = isset($cids[$file]) ? $cids[$file] : $cids[$file] = substr($this->addEmbeddedFile($file)->getHeader("Content-ID"), 1, -1);
436:                 $this->html = substr_replace($this->html, "{$m[1][0]}{$m[2][0]}cid:$cid{$m[2][0]}", $m[0][1], strlen($m[0][0]));
437:             }
438:         }
439: 
440:         if (!$this->getSubject() && preg_match('#<title>(.+?)</title>#is', $this->html, $matches)) {
441:             $this->setSubject(html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8'));
442:         }
443:     }
444: 
445: 
446: 
447:     /**
448:      * Builds text content.
449:      * @return void
450:      */
451:     protected function buildText()
452:     {
453:         $text = $this->getBody();
454:         if ($text instanceof Nette\Templates\ITemplate) {
455:             $text->mail = $this;
456:             $this->setBody($text->__toString(TRUE));
457: 
458:         } elseif ($text == NULL && $this->html != NULL) { // intentionally ==
459:             $text = preg_replace('#<(style|script|head).*</\\1>#Uis', '', $this->html);
460:             $text = preg_replace('#<t[dh][ >]#i', " $0", $text);
461:             $text = preg_replace('#[ \t\r\n]+#', ' ', $text);
462:             $text = preg_replace('#<(/?p|/?h\d|li|br|/tr)[ >/]#i', "\n$0", $text);
463: 
464:             $text = html_entity_decode(strip_tags($text), ENT_QUOTES, 'UTF-8');
465:             $this->setBody(trim($text));
466:         }
467:     }
468: 
469: }
470: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0