Source for file MailMimePart.php
Documentation is available at MailMimePart.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
10: * @package Nette\Mail
18: * @copyright Copyright (c) 2004, 2010 David Grudl
19: * @package Nette\Mail
21: * @property string $encoding
22: * @property string $body
23: * @property-read array $headers
34: /**#@+ @ignore internal */
36: const LINE_LENGTH =
76;
40: private $headers =
array();
43: private $parts =
array();
53: * @param string|array value or pair email => name
55: * @return MailMimePart provides a fluent interface
57: public function setHeader($name, $value, $append =
FALSE)
60: throw new InvalidArgumentException("Header name must be non-empty alphanumeric string, '$name' given.");
63: if ($value ==
NULL) { // intentionally ==
65: unset($this->headers[$name]);
69: $tmp =
& $this->headers[$name];
74: foreach ($value as $email =>
$name) {
75: if (!preg_match('#^[^@",\s]+@[^@",\s]+\.[a-z]{2,10}$#i', $email)) {
76: throw new InvalidArgumentException("Email address '$email' is not valid.");
80: throw new InvalidArgumentException("Name cannot contain the line separator.");
82: $tmp[$email] =
$name;
100: return isset($this->headers[$name]) ?
$this->headers[$name] :
NULL;
108: * @return MailMimePart provides a fluent interface
112: unset($this->headers[$name]);
119: * Returns an encoded header.
128: if (!isset($this->headers[$name])) {
133: foreach ($this->headers[$name] as $email =>
$name) {
134: if ($name !=
NULL) { // intentionally ==
135: $s .=
self::encodeQuotedPrintableHeader(
139: $email =
" <$email>";
141: if ($len +
strlen($email) +
1 >
self::LINE_LENGTH) {
142: $s .=
self::EOL .
"\t";
151: return self::encodeQuotedPrintableHeader($this->headers[$name], $charset, $len);
158: * Returns all headers.
163: return $this->headers;
169: * Sets Content-Type header.
172: * @return MailMimePart provides a fluent interface
176: $this->setHeader('Content-Type', $contentType .
($charset ?
"; charset=$charset" :
''));
183: * Sets Content-Transfer-Encoding header.
185: * @return MailMimePart provides a fluent interface
196: * Returns Content-Transfer-Encoding header.
207: * Adds or creates new multipart.
208: * @param MailMimePart
209: * @return MailMimePart
211: public function addPart(MailMimePart $part =
NULL)
213: return $this->parts[] =
$part ===
NULL ?
new self :
$part;
219: * Sets textual body.
221: * @return MailMimePart provides a fluent interface
225: $this->body =
$body;
232: * Gets textual body.
242: /********************* building ****************d*g**/
247: * Returns encoded message.
255: foreach ($this->headers as $name =>
$value) {
257: if ($this->parts &&
$name ===
'Content-Type') {
258: $output .=
';' .
self::EOL .
"\tboundary=\"$boundary\"";
260: $output .=
self::EOL;
262: $output .=
self::EOL;
264: $body = (string)
$this->body;
267: case self::ENCODING_QUOTED_PRINTABLE:
268: $output .=
function_exists('quoted_printable_encode') ?
quoted_printable_encode($body) :
self::encodeQuotedPrintable($body);
271: case self::ENCODING_BASE64:
275: case self::ENCODING_7BIT:
277: // break intentionally omitted
279: case self::ENCODING_8BIT:
291: if (substr($output, -
strlen(self::EOL)) !==
self::EOL) $output .=
self::EOL;
292: foreach ($this->parts as $part) {
293: $output .=
'--' .
$boundary .
self::EOL .
$part->generateMessage() .
self::EOL;
295: $output .=
'--' .
$boundary.
'--';
303: /********************* QuotedPrintable helpers ****************d*g**/
308: * Converts a 8 bit header to a quoted-printable string.
314: private static function encodeQuotedPrintableHeader($s, $charset =
'UTF-8', & $len =
0)
316: $range =
'!"#$%&\'()*+,-./0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^`abcdefghijklmnopqrstuvwxyz{|}'; // \x21-\x7E without \x3D \x3F \x5F
322: $prefix =
"=?$charset?Q?";
327: while ($pos <
$size) {
329: while ($len +
$l >
self::LINE_LENGTH -
2) { // 2 = length of suffix ?=
330: $lx =
self::LINE_LENGTH -
$len -
2;
331: $o .=
substr($s, $pos, $lx) .
'?=' .
self::EOL .
"\t" .
$prefix;
342: // \xC0 tests UTF-8 character boudnary; 9 is reserved space for 4bytes UTF-8 character
343: if (($s[$pos] & "\xC0") !==
"\x80" &&
$len >
self::LINE_LENGTH -
2 -
9) {
344: $o .=
'?=' .
self::EOL .
"\t" .
$prefix;
357: * Converts a 8 bit string to a quoted-printable string.
363: $range =
'!"#$%&\'()*+,-./0123456789:;<>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}'; // \x21-\x7E without \x3D
368: while ($pos <
$size) {
370: while ($len +
$l >
self::LINE_LENGTH -
1) { // 1 = length of suffix =
371: $lx =
self::LINE_LENGTH -
$len -
1;
372: $o .=
substr($s, $pos, $lx) .
'=' .
self::EOL;
383: if ($len >
self::LINE_LENGTH -
1) {
384: $o .=
'=' .
self::EOL;