Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • NMail
  • NMailMimePart
  • NSendmailMailer
  • NSmtpMailer

Interfaces

  • IMailer

Exceptions

  • NSmtpException
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 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:  * @package Nette\Mail
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * MIME message part.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property   string $encoding
 21:  * @property   string $body
 22:  * @property-read array $headers
 23:  * @package Nette\Mail
 24:  */
 25: class NMailMimePart extends NObject
 26: {
 27:     /** encoding */
 28:     const ENCODING_BASE64 = 'base64',
 29:         ENCODING_7BIT = '7bit',
 30:         ENCODING_8BIT = '8bit',
 31:         ENCODING_QUOTED_PRINTABLE = 'quoted-printable';
 32: 
 33:     /** @internal */
 34:     const EOL = "\r\n";
 35:     const LINE_LENGTH = 76;
 36: 
 37:     /** @var array */
 38:     private $headers = array();
 39: 
 40:     /** @var array */
 41:     private $parts = array();
 42: 
 43:     /** @var string */
 44:     private $body;
 45: 
 46: 
 47: 
 48:     /**
 49:      * Sets a header.
 50:      * @param  string
 51:      * @param  string|array  value or pair email => name
 52:      * @param  bool
 53:      * @return NMailMimePart  provides a fluent interface
 54:      */
 55:     public function setHeader($name, $value, $append = FALSE)
 56:     {
 57:         if (!$name || preg_match('#[^a-z0-9-]#i', $name)) {
 58:             throw new InvalidArgumentException("Header name must be non-empty alphanumeric string, '$name' given.");
 59:         }
 60: 
 61:         if ($value == NULL) { // intentionally ==
 62:             if (!$append) {
 63:                 unset($this->headers[$name]);
 64:             }
 65: 
 66:         } elseif (is_array($value)) { // email
 67:             $tmp = & $this->headers[$name];
 68:             if (!$append || !is_array($tmp)) {
 69:                 $tmp = array();
 70:             }
 71: 
 72:             foreach ($value as $email => $name) {
 73:                 if ($name !== NULL && !NStrings::checkEncoding($name)) {
 74:                     throw new InvalidArgumentException("Name is not valid UTF-8 string.");
 75:                 }
 76: 
 77:                 if (!preg_match('#^[^@",\s]+@[^@",\s]+\.[a-z]{2,10}$#i', $email)) {
 78:                     throw new InvalidArgumentException("Email address '$email' is not valid.");
 79:                 }
 80: 
 81:                 if (preg_match('#[\r\n]#', $name)) {
 82:                     throw new InvalidArgumentException("Name must not contain line separator.");
 83:                 }
 84:                 $tmp[$email] = $name;
 85:             }
 86: 
 87:         } else {
 88:             $value = (string) $value;
 89:             if (!NStrings::checkEncoding($value)) {
 90:                 throw new InvalidArgumentException("Header is not valid UTF-8 string.");
 91:             }
 92:             $this->headers[$name] = preg_replace('#[\r\n]+#', ' ', $value);
 93:         }
 94:         return $this;
 95:     }
 96: 
 97: 
 98: 
 99:     /**
100:      * Returns a header.
101:      * @param  string
102:      * @return mixed
103:      */
104:     public function getHeader($name)
105:     {
106:         return isset($this->headers[$name]) ? $this->headers[$name] : NULL;
107:     }
108: 
109: 
110: 
111:     /**
112:      * Removes a header.
113:      * @param  string
114:      * @return NMailMimePart  provides a fluent interface
115:      */
116:     public function clearHeader($name)
117:     {
118:         unset($this->headers[$name]);
119:         return $this;
120:     }
121: 
122: 
123: 
124:     /**
125:      * Returns an encoded header.
126:      * @param  string
127:      * @param  string
128:      * @return string
129:      */
130:     public function getEncodedHeader($name)
131:     {
132:         $offset = strlen($name) + 2; // colon + space
133: 
134:         if (!isset($this->headers[$name])) {
135:             return NULL;
136: 
137:         } elseif (is_array($this->headers[$name])) {
138:             $s = '';
139:             foreach ($this->headers[$name] as $email => $name) {
140:                 if ($name != NULL) { // intentionally ==
141:                     $s .= self::encodeHeader(
142:                         strpbrk($name, '.,;<@>()[]"=?') ? '"' . addcslashes($name, '"\\') . '"' : $name,
143:                         $offset
144:                     );
145:                     $email = " <$email>";
146:                 }
147:                 $email .= ',';
148:                 if ($s !== '' && $offset + strlen($email) > self::LINE_LENGTH) {
149:                     $s .= self::EOL . "\t";
150:                     $offset = 1;
151:                 }
152:                 $s .= $email;
153:                 $offset += strlen($email);
154:             }
155:             return substr($s, 0, -1); // last comma
156: 
157:         } else {
158:             return self::encodeHeader($this->headers[$name], $offset);
159:         }
160:     }
161: 
162: 
163: 
164:     /**
165:      * Returns all headers.
166:      * @return array
167:      */
168:     public function getHeaders()
169:     {
170:         return $this->headers;
171:     }
172: 
173: 
174: 
175:     /**
176:      * Sets Content-Type header.
177:      * @param  string
178:      * @param  string
179:      * @return NMailMimePart  provides a fluent interface
180:      */
181:     public function setContentType($contentType, $charset = NULL)
182:     {
183:         $this->setHeader('Content-Type', $contentType . ($charset ? "; charset=$charset" : ''));
184:         return $this;
185:     }
186: 
187: 
188: 
189:     /**
190:      * Sets Content-Transfer-Encoding header.
191:      * @param  string
192:      * @return NMailMimePart  provides a fluent interface
193:      */
194:     public function setEncoding($encoding)
195:     {
196:         $this->setHeader('Content-Transfer-Encoding', $encoding);
197:         return $this;
198:     }
199: 
200: 
201: 
202:     /**
203:      * Returns Content-Transfer-Encoding header.
204:      * @return string
205:      */
206:     public function getEncoding()
207:     {
208:         return $this->getHeader('Content-Transfer-Encoding');
209:     }
210: 
211: 
212: 
213:     /**
214:      * Adds or creates new multipart.
215:      * @param  NMailMimePart
216:      * @return NMailMimePart
217:      */
218:     public function addPart(NMailMimePart $part = NULL)
219:     {
220:         return $this->parts[] = $part === NULL ? new self : $part;
221:     }
222: 
223: 
224: 
225:     /**
226:      * Sets textual body.
227:      * @param  mixed
228:      * @return NMailMimePart  provides a fluent interface
229:      */
230:     public function setBody($body)
231:     {
232:         $this->body = $body;
233:         return $this;
234:     }
235: 
236: 
237: 
238:     /**
239:      * Gets textual body.
240:      * @return mixed
241:      */
242:     public function getBody()
243:     {
244:         return $this->body;
245:     }
246: 
247: 
248: 
249:     /********************* building ****************d*g**/
250: 
251: 
252: 
253:     /**
254:      * Returns encoded message.
255:      * @return string
256:      */
257:     public function generateMessage()
258:     {
259:         $output = '';
260:         $boundary = '--------' . NStrings::random();
261: 
262:         foreach ($this->headers as $name => $value) {
263:             $output .= $name . ': ' . $this->getEncodedHeader($name);
264:             if ($this->parts && $name === 'Content-Type') {
265:                 $output .= ';' . self::EOL . "\tboundary=\"$boundary\"";
266:             }
267:             $output .= self::EOL;
268:         }
269:         $output .= self::EOL;
270: 
271:         $body = (string) $this->body;
272:         if ($body !== '') {
273:             switch ($this->getEncoding()) {
274:             case self::ENCODING_QUOTED_PRINTABLE:
275:                 $output .= function_exists('quoted_printable_encode') ? quoted_printable_encode($body) : self::encodeQuotedPrintable($body);
276:                 break;
277: 
278:             case self::ENCODING_BASE64:
279:                 $output .= rtrim(chunk_split(base64_encode($body), self::LINE_LENGTH, self::EOL));
280:                 break;
281: 
282:             case self::ENCODING_7BIT:
283:                 $body = preg_replace('#[\x80-\xFF]+#', '', $body);
284:                 // break intentionally omitted
285: 
286:             case self::ENCODING_8BIT:
287:                 $body = str_replace(array("\x00", "\r"), '', $body);
288:                 $body = str_replace("\n", self::EOL, $body);
289:                 $output .= $body;
290:                 break;
291: 
292:             default:
293:                 throw new InvalidStateException('Unknown encoding.');
294:             }
295:         }
296: 
297:         if ($this->parts) {
298:             if (substr($output, -strlen(self::EOL)) !== self::EOL) {
299:                 $output .= self::EOL;
300:             }
301:             foreach ($this->parts as $part) {
302:                 $output .= '--' . $boundary . self::EOL . $part->generateMessage() . self::EOL;
303:             }
304:             $output .= '--' . $boundary.'--';
305:         }
306: 
307:         return $output;
308:     }
309: 
310: 
311: 
312:     /********************* QuotedPrintable helpers ****************d*g**/
313: 
314: 
315: 
316:     /**
317:      * Converts a 8 bit header to a quoted-printable string.
318:      * @param  string
319:      * @param  string
320:      * @param  int
321:      * @return string
322:      */
323:     private static function encodeHeader($s, & $offset = 0)
324:     {
325:         $o = '';
326:         if ($offset >= 55) { // maximum for iconv_mime_encode
327:             $o = self::EOL . "\t";
328:             $offset = 1;
329:         }
330: 
331:         if (strspn($s, "!\"#$%&\'()*+,-./0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}=? _\r\n\t") === strlen($s)
332:             && ($offset + strlen($s) <= self::LINE_LENGTH)
333:         ) {
334:             $offset += strlen($s);
335:             return $o . $s;
336:         }
337: 
338:         $o .= str_replace("\n ", "\n\t", substr(iconv_mime_encode(str_repeat(' ', $offset), $s, array(
339:             'scheme' => 'B', // Q is broken
340:             'input-charset' => 'UTF-8',
341:             'output-charset' => 'UTF-8',
342:         )), $offset + 2));
343: 
344:         $offset = strlen($o) - strrpos($o, "\n");
345:         return $o;
346:     }
347: 
348: 
349: 
350:     /**
351:      * Converts a 8 bit string to a quoted-printable string.
352:      * @param  string
353:      * @return string
354:      */public static function encodeQuotedPrintable($s)
355:     {
356:         $range = '!"#$%&\'()*+,-./0123456789:;<>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}'; // \x21-\x7E without \x3D
357:         $pos = 0;
358:         $len = 0;
359:         $o = '';
360:         $size = strlen($s);
361:         while ($pos < $size) {
362:             if ($l = strspn($s, $range, $pos)) {
363:                 while ($len + $l > self::LINE_LENGTH - 1) { // 1 = length of suffix =
364:                     $lx = self::LINE_LENGTH - $len - 1;
365:                     $o .= substr($s, $pos, $lx) . '=' . self::EOL;
366:                     $pos += $lx;
367:                     $l -= $lx;
368:                     $len = 0;
369:                 }
370:                 $o .= substr($s, $pos, $l);
371:                 $len += $l;
372:                 $pos += $l;
373: 
374:             } else {
375:                 $len += 3;
376:                 if ($len > self::LINE_LENGTH - 1) {
377:                     $o .= '=' . self::EOL;
378:                     $len = 3;
379:                 }
380:                 $o .= '=' . strtoupper(bin2hex($s[$pos]));
381:                 $pos++;
382:             }
383:         }
384:         return rtrim($o, '=' . self::EOL);
385:     }
386: 
387: }
388: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0