Source for file Mail.php
Documentation is available at Mail.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
16: * Mail provides functionality to compose and send both text and MIME-compliant multipart e-mail messages.
18: * @copyright Copyright (c) 2004, 2010 David Grudl
19: * @package Nette\Mail
21: * @property string $from
22: * @property string $subject
23: * @property string $returnPath
24: * @property int $priority
25: * @property string $htmlBody
36: public static $defaultMailer =
'Nette\Mail\SendmailMailer';
39: public static $defaultHeaders =
array(
40: 'MIME-Version' =>
'1.0',
41: 'X-Mailer' =>
'Nette Framework',
48: private $charset =
'UTF-8';
51: private $attachments =
array();
54: private $inlines =
array();
66: foreach (self::$defaultHeaders as $name =>
$value) {
75: * Sets the sender of the message.
76: * @param string e-mail or format "John Doe" <doe@example.com>
78: * @return Mail provides a fluent interface
82: $this->setHeader('From', $this->formatEmail($email, $name));
89: * Returns the sender of the message.
100: * Adds the reply-to address.
101: * @param string e-mail or format "John Doe" <doe@example.com>
103: * @return Mail provides a fluent interface
107: $this->setHeader('Reply-To', $this->formatEmail($email, $name), TRUE);
114: * Sets the subject of the message.
116: * @return Mail provides a fluent interface
127: * Returns the subject of the message.
138: * Adds email recipient.
139: * @param string e-mail or format "John Doe" <doe@example.com>
141: * @return Mail provides a fluent interface
143: public function addTo($email, $name =
NULL) // addRecipient()
145: $this->setHeader('To', $this->formatEmail($email, $name), TRUE);
152: * Adds carbon copy email recipient.
153: * @param string e-mail or format "John Doe" <doe@example.com>
155: * @return Mail provides a fluent interface
157: public function addCc($email, $name =
NULL)
159: $this->setHeader('Cc', $this->formatEmail($email, $name), TRUE);
166: * Adds blind carbon copy email recipient.
167: * @param string e-mail or format "John Doe" <doe@example.com>
169: * @return Mail provides a fluent interface
173: $this->setHeader('Bcc', $this->formatEmail($email, $name), TRUE);
180: * Formats recipient e-mail.
185: private function formatEmail($email, $name)
187: if (!$name &&
preg_match('#^(.+) +<(.*)>$#', $email, $matches)) {
188: return array($matches[2] =>
$matches[1]);
190: return array($email =>
$name);
197: * Sets the Return-Path header of the message.
198: * @param string e-mail
199: * @return Mail provides a fluent interface
210: * Returns the Return-Path header.
221: * Sets email priority.
223: * @return Mail provides a fluent interface
234: * Returns email priority.
246: * @param string|Nette\Templates\ITemplate
247: * @param mixed base-path or FALSE to disable parsing
248: * @return Mail provides a fluent interface
252: $this->html =
$html;
253: $this->basePath =
$basePath;
271: * Adds embedded file.
275: * @return 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="' .
basename($file) .
'"');
284: $part->setHeader('Content-ID', '<' .
md5(uniqid('', TRUE)) .
'>');
285: return $this->inlines[$file] =
$part;
295: * @return 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="' .
basename($file) .
'"');
304: return $this->attachments[] =
$part;
310: * Creates file MIME part.
315: private function readFile($file, & $contentType)
321: $contentType =
$info['mime'];
328: /********************* building and sending ****************d*g**/
346: * @return Mail provides a fluent interface
350: $this->mailer =
$mailer;
362: if ($this->mailer ===
NULL) {
364: $this->mailer =
is_object(self::$defaultMailer) ?
self::$defaultMailer :
new self::$defaultMailer;
366: return $this->mailer;
377: $mail =
clone $this;
378: $hostname =
isset($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] :
isset($_SERVER['SERVER_NAME']) ?
$_SERVER['SERVER_NAME'] :
'localhost';
379: $mail->setHeader('Message-ID', '<' .
md5(uniqid('', TRUE)) .
"@$hostname>");
385: if ($mail->attachments) {
386: $tmp =
$cursor->setContentType('multipart/mixed');
387: $cursor =
$cursor->addPart();
388: foreach ($mail->attachments as $value) {
389: $tmp->addPart($value);
393: if ($mail->html !=
NULL) { // intentionally ==
394: $tmp =
$cursor->setContentType('multipart/alternative');
395: $cursor =
$cursor->addPart();
396: $alt =
$tmp->addPart();
397: if ($mail->inlines) {
398: $tmp =
$alt->setContentType('multipart/related');
399: $alt =
$alt->addPart();
400: foreach ($mail->inlines as $name =>
$value) {
401: $tmp->addPart($value);
404: $alt->setContentType('text/html', $mail->charset)
405: ->setEncoding(preg_match('#[\x80-\xFF]#', $mail->html) ?
self::ENCODING_8BIT :
self::ENCODING_7BIT)
406: ->setBody($mail->html);
409: $text =
$mail->getBody();
410: $mail->setBody(NULL);
411: $cursor->setContentType('text/plain', $mail->charset)
412: ->setEncoding(preg_match('#[\x80-\xFF]#', $text) ?
self::ENCODING_8BIT :
self::ENCODING_7BIT)
421: * Builds HTML content.
427: $this->html->mail =
$this;
429: $this->basePath =
dirname($this->html->getFile());
431: $this->html =
$this->html->__toString(TRUE);
434: if ($this->basePath !==
FALSE) {
436: preg_match_all('#(src\s*=\s*|background\s*=\s*|url\()(["\'])(?![a-z]+:|[/\\#])(.+?)\\2#i', $this->html, $matches, PREG_SET_ORDER |
PREG_OFFSET_CAPTURE);
438: $file =
rtrim($this->basePath, '/\\') .
'/' .
$m[3][0];
439: $cid =
isset($cids[$file]) ?
$cids[$file] :
$cids[$file] =
substr($this->addEmbeddedFile($file)->getHeader("Content-ID"), 1, -
1);
452: * Builds text content.
459: $text->mail =
$this;
462: } elseif ($text ==
NULL &&
$this->html !=
NULL) { // intentionally ==
463: $text =
preg_replace('#<(style|script|head).*</\\1>#Uis', '', $this->html);