Source for file Mail.php
Documentation is available at Mail.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
17: * @package Nette\Mail
22: require_once dirname(__FILE__) .
'/../Mail/MailMimePart.php';
27: * Mail provides functionality to compose and send both text and MIME-compliant multipart e-mail messages.
29: * @author David Grudl
30: * @copyright Copyright (c) 2004, 2009 David Grudl
31: * @package Nette\Mail
33: * @property string $from
34: * @property string $subject
35: * @property string $returnPath
36: * @property int $priority
37: * @property string $htmlBody
48: public static $defaultMailer =
'Nette\Mail\SendmailMailer';
51: public static $defaultHeaders =
array(
52: 'MIME-Version' =>
'1.0',
53: 'X-Mailer' =>
'Nette Framework',
60: private $charset =
'UTF-8';
63: private $attachments =
array();
66: private $inlines =
array();
78: foreach (self::$defaultHeaders as $name =>
$value) {
87: * Sets the sender of the message.
88: * @param string e-mail or format "John Doe" <doe@example.com>
90: * @return Mail provides a fluent interface
94: $this->setHeader('From', $this->formatEmail($email, $name));
101: * Returns the sender of the message.
112: * Adds the reply-to address.
113: * @param string e-mail or format "John Doe" <doe@example.com>
115: * @return Mail provides a fluent interface
119: $this->setHeader('Reply-To', $this->formatEmail($email, $name), TRUE);
126: * Sets the subject of the message.
128: * @return Mail provides a fluent interface
139: * Returns the subject of the message.
150: * Adds email recipient.
151: * @param string e-mail or format "John Doe" <doe@example.com>
153: * @return Mail provides a fluent interface
155: public function addTo($email, $name =
NULL) // addRecipient()
157: $this->setHeader('To', $this->formatEmail($email, $name), TRUE);
164: * Adds carbon copy email recipient.
165: * @param string e-mail or format "John Doe" <doe@example.com>
167: * @return Mail provides a fluent interface
169: public function addCc($email, $name =
NULL)
171: $this->setHeader('Cc', $this->formatEmail($email, $name), TRUE);
178: * Adds blind carbon copy email recipient.
179: * @param string e-mail or format "John Doe" <doe@example.com>
181: * @return Mail provides a fluent interface
185: $this->setHeader('Bcc', $this->formatEmail($email, $name), TRUE);
192: * Formats recipient e-mail.
197: private function formatEmail($email, $name)
199: if (!$name &&
preg_match('#^(.+) +<(.*)>$#', $email, $matches)) {
200: return array($matches[2] =>
$matches[1]);
202: return array($email =>
$name);
209: * Sets the Return-Path header of the message.
210: * @param string e-mail
211: * @return Mail provides a fluent interface
222: * Returns the Return-Path header.
233: * Sets email priority.
235: * @return Mail provides a fluent interface
246: * Returns email priority.
258: * @param string|Nette\Templates\ITemplate
259: * @param mixed base-path or FALSE to disable parsing
260: * @return Mail provides a fluent interface
264: $this->html =
$html;
265: $this->basePath =
$basePath;
283: * Adds embedded file.
287: * @return MailMimePart
292: $part->setBody($content ===
NULL ?
$this->readFile($file, $contentType) : (string)
$content);
293: $part->setContentType($contentType ?
$contentType :
'application/octet-stream');
294: $part->setEncoding(self::ENCODING_BASE64);
295: $part->setHeader('Content-Disposition', 'inline; filename="' .
basename($file) .
'"');
296: $part->setHeader('Content-ID', '<' .
md5(uniqid('', TRUE)) .
'>');
297: return $this->inlines[$file] =
$part;
307: * @return MailMimePart
312: $part->setBody($content ===
NULL ?
$this->readFile($file, $contentType) : (string)
$content);
313: $part->setContentType($contentType ?
$contentType :
'application/octet-stream');
314: $part->setEncoding(self::ENCODING_BASE64);
315: $part->setHeader('Content-Disposition', 'attachment; filename="' .
basename($file) .
'"');
316: return $this->attachments[] =
$part;
322: * Creates file MIME part.
327: private function readFile($file, & $contentType)
333: $contentType =
$info['mime'];
340: /********************* building and sending ****************d*g**/
358: * @return Mail provides a fluent interface
362: $this->mailer =
$mailer;
374: if ($this->mailer ===
NULL) {
376: $this->mailer =
is_object(self::$defaultMailer) ?
self::$defaultMailer :
new self::$defaultMailer;
378: return $this->mailer;
389: $mail =
clone $this;
390: $hostname =
isset($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] :
isset($_SERVER['SERVER_NAME']) ?
$_SERVER['SERVER_NAME'] :
'localhost';
391: $mail->setHeader('Message-ID', '<' .
md5(uniqid('', TRUE)) .
"@$hostname>");
397: if ($mail->attachments) {
398: $tmp =
$cursor->setContentType('multipart/mixed');
399: $cursor =
$cursor->addPart();
400: foreach ($mail->attachments as $value) {
401: $tmp->addPart($value);
405: if ($mail->html !=
NULL) { // intentionally ==
406: $tmp =
$cursor->setContentType('multipart/alternative');
407: $cursor =
$cursor->addPart();
408: $alt =
$tmp->addPart();
409: if ($mail->inlines) {
410: $tmp =
$alt->setContentType('multipart/related');
411: $alt =
$alt->addPart();
412: foreach ($mail->inlines as $name =>
$value) {
413: $tmp->addPart($value);
416: $alt->setContentType('text/html', $mail->charset)
417: ->setEncoding(preg_match('#[\x80-\xFF]#', $mail->html) ?
self::ENCODING_8BIT :
self::ENCODING_7BIT)
418: ->setBody($mail->html);
421: $text =
$mail->getBody();
422: $mail->setBody(NULL);
423: $cursor->setContentType('text/plain', $mail->charset)
424: ->setEncoding(preg_match('#[\x80-\xFF]#', $text) ?
self::ENCODING_8BIT :
self::ENCODING_7BIT)
433: * Builds HTML content.
439: $this->html->mail =
$this;
441: $this->basePath =
dirname($this->html->getFile());
443: $this->html =
$this->html->__toString(TRUE);
446: if ($this->basePath !==
FALSE) {
448: preg_match_all('#(src\s*=\s*|background\s*=\s*|url\()(["\'])(?![a-z]+:|[/\\#])(.+?)\\2#i', $this->html, $matches, PREG_SET_ORDER |
PREG_OFFSET_CAPTURE);
450: $file =
rtrim($this->basePath, '/\\') .
'/' .
$m[3][0];
451: $cid =
isset($cids[$file]) ?
$cids[$file] :
$cids[$file] =
substr($this->addEmbeddedFile($file)->getHeader("Content-ID"), 1, -
1);
464: * Builds text content.
471: $text->mail =
$this;
474: } elseif ($text ==
NULL &&
$this->html !=
NULL) { // intentionally ==
475: $text =
preg_replace('#<(style|script|head).*</\\1>#Uis', '', $this->html);