00001 <?php
00026 require_once 'Zend/Mail/Transport/Abstract.php';
00027
00031 require_once 'Zend/Mime.php';
00032
00036 require_once 'Zend/Mime/Message.php';
00037
00041 require_once 'Zend/Mime/Part.php';
00042
00043
00052 class Zend_Mail extends Zend_Mime_Message
00053 {
00062 protected static $_defaultTransport = null;
00063
00068 protected $_charset = null;
00069
00074 protected $_headers = array();
00075
00080 protected $_headerEncoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
00081
00086 protected $_from = null;
00087
00092 protected $_to = array();
00093
00098 protected $_recipients = array();
00099
00104 protected $_returnPath = null;
00105
00110 protected $_subject = null;
00111
00116 protected $_date = null;
00117
00122 protected $_messageId = null;
00123
00128 protected $_bodyText = false;
00129
00134 protected $_bodyHtml = false;
00135
00140 protected $_mimeBoundary = null;
00141
00146 protected $_type = null;
00147
00154 public $hasAttachments = false;
00155
00156
00165 public static function setDefaultTransport(Zend_Mail_Transport_Abstract $transport)
00166 {
00167 self::$_defaultTransport = $transport;
00168 }
00169
00175 public function __construct($charset = 'iso-8859-1')
00176 {
00177 $this->_charset = $charset;
00178 }
00179
00185 public function getCharset()
00186 {
00187 return $this->_charset;
00188 }
00189
00199 public function setType($type)
00200 {
00201 $allowed = array(
00202 Zend_Mime::MULTIPART_ALTERNATIVE,
00203 Zend_Mime::MULTIPART_MIXED,
00204 Zend_Mime::MULTIPART_RELATED,
00205 );
00206 if (!in_array($type, $allowed)) {
00210 require_once 'Zend/Mail/Exception.php';
00211 throw new Zend_Mail_Exception('Invalid content type "' . $type . '"');
00212 }
00213
00214 $this->_type = $type;
00215 return $this;
00216 }
00217
00223 public function getType()
00224 {
00225 return $this->_type;
00226 }
00227
00236 public function setMimeBoundary($boundary)
00237 {
00238 $this->_mimeBoundary = $boundary;
00239
00240 return $this;
00241 }
00242
00248 public function getMimeBoundary()
00249 {
00250 return $this->_mimeBoundary;
00251 }
00252
00259 public function getEncodingOfHeaders()
00260 {
00261 return $this->getHeaderEncoding();
00262 }
00263
00271 public function getHeaderEncoding()
00272 {
00273 return $this->_headerEncoding;
00274 }
00275
00283 public function setEncodingOfHeaders($encoding)
00284 {
00285 return $this->setHeaderEncoding($encoding);
00286 }
00287
00294 public function setHeaderEncoding($encoding)
00295 {
00296 $allowed = array(
00297 Zend_Mime::ENCODING_BASE64,
00298 Zend_Mime::ENCODING_QUOTEDPRINTABLE
00299 );
00300 if (!in_array($encoding, $allowed)) {
00304 require_once 'Zend/Mail/Exception.php';
00305 throw new Zend_Mail_Exception('Invalid encoding "' . $encoding . '"');
00306 }
00307 $this->_headerEncoding = $encoding;
00308
00309 return $this;
00310 }
00311
00320 public function setBodyText($txt, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
00321 {
00322 if ($charset === null) {
00323 $charset = $this->_charset;
00324 }
00325
00326 $mp = new Zend_Mime_Part($txt);
00327 $mp->encoding = $encoding;
00328 $mp->type = Zend_Mime::TYPE_TEXT;
00329 $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
00330 $mp->charset = $charset;
00331
00332 $this->_bodyText = $mp;
00333
00334 return $this;
00335 }
00336
00343 public function getBodyText($textOnly = false)
00344 {
00345 if ($textOnly && $this->_bodyText) {
00346 $body = $this->_bodyText;
00347 return $body->getContent();
00348 }
00349
00350 return $this->_bodyText;
00351 }
00352
00361 public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
00362 {
00363 if ($charset === null) {
00364 $charset = $this->_charset;
00365 }
00366
00367 $mp = new Zend_Mime_Part($html);
00368 $mp->encoding = $encoding;
00369 $mp->type = Zend_Mime::TYPE_HTML;
00370 $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
00371 $mp->charset = $charset;
00372
00373 $this->_bodyHtml = $mp;
00374
00375 return $this;
00376 }
00377
00384 public function getBodyHtml($htmlOnly = false)
00385 {
00386 if ($htmlOnly && $this->_bodyHtml) {
00387 $body = $this->_bodyHtml;
00388 return $body->getContent();
00389 }
00390
00391 return $this->_bodyHtml;
00392 }
00393
00400 public function addAttachment(Zend_Mime_Part $attachment)
00401 {
00402 $this->addPart($attachment);
00403 $this->hasAttachments = true;
00404
00405 return $this;
00406 }
00407
00422 public function createAttachment($body,
00423 $mimeType = Zend_Mime::TYPE_OCTETSTREAM,
00424 $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
00425 $encoding = Zend_Mime::ENCODING_BASE64,
00426 $filename = null)
00427 {
00428
00429 $mp = new Zend_Mime_Part($body);
00430 $mp->encoding = $encoding;
00431 $mp->type = $mimeType;
00432 $mp->disposition = $disposition;
00433 $mp->filename = $filename;
00434
00435 $this->addAttachment($mp);
00436
00437 return $mp;
00438 }
00439
00445 public function getPartCount()
00446 {
00447 return count($this->_parts);
00448 }
00449
00459 protected function _encodeHeader($value)
00460 {
00461 if (Zend_Mime::isPrintable($value) === false) {
00462 if ($this->getHeaderEncoding() === Zend_Mime::ENCODING_QUOTEDPRINTABLE) {
00463 $value = Zend_Mime::encodeQuotedPrintableHeader($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
00464 } else {
00465 $value = Zend_Mime::encodeBase64Header($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
00466 }
00467 }
00468
00469 return $value;
00470 }
00471
00482 protected function _storeHeader($headerName, $value, $append = false)
00483 {
00484 if (isset($this->_headers[$headerName])) {
00485 $this->_headers[$headerName][] = $value;
00486 } else {
00487 $this->_headers[$headerName] = array($value);
00488 }
00489
00490 if ($append) {
00491 $this->_headers[$headerName]['append'] = true;
00492 }
00493
00494 }
00495
00501 protected function _clearHeader($headerName)
00502 {
00503 if (isset($this->_headers[$headerName])){
00504 unset($this->_headers[$headerName]);
00505 }
00506 }
00507
00515 protected function _addRecipientAndHeader($headerName, $email, $name)
00516 {
00517 $email = $this->_filterEmail($email);
00518 $name = $this->_filterName($name);
00519
00520 $this->_recipients[$email] = 1;
00521 $this->_storeHeader($headerName, $this->_formatAddress($email, $name), true);
00522 }
00523
00531 public function addTo($email, $name='')
00532 {
00533 $this->_addRecipientAndHeader('To', $email, $name);
00534 $this->_to[] = $email;
00535 return $this;
00536 }
00537
00545 public function addCc($email, $name='')
00546 {
00547 $this->_addRecipientAndHeader('Cc', $email, $name);
00548 return $this;
00549 }
00550
00557 public function addBcc($email)
00558 {
00559 $this->_addRecipientAndHeader('Bcc', $email, '');
00560 return $this;
00561 }
00562
00568 public function getRecipients()
00569 {
00570 return array_keys($this->_recipients);
00571 }
00572
00578 public function clearRecipients()
00579 {
00580 $this->_recipients = array();
00581 $this->_to = array();
00582
00583 $this->_clearHeader('To');
00584 $this->_clearHeader('Cc');
00585 $this->_clearHeader('Bcc');
00586
00587 return $this;
00588 }
00589
00598 public function setFrom($email, $name = null)
00599 {
00600 if ($this->_from === null) {
00601 $email = $this->_filterEmail($email);
00602 $name = $this->_filterName($name);
00603 $this->_from = $email;
00604 $this->_storeHeader('From', $this->_formatAddress($email, $name), true);
00605 } else {
00609 require_once 'Zend/Mail/Exception.php';
00610 throw new Zend_Mail_Exception('From Header set twice');
00611 }
00612 return $this;
00613 }
00614
00622 public function setReplyTo($email, $name=null)
00623 {
00624 $this->_addRecipientAndHeader('Reply-To', $email, $name);
00625 return $this;
00626 }
00627
00633 public function getFrom()
00634 {
00635 return $this->_from;
00636 }
00637
00643 public function clearFrom()
00644 {
00645 $this->_from = null;
00646 $this->_clearHeader('From');
00647
00648 return $this;
00649 }
00650
00658 public function setReturnPath($email)
00659 {
00660 if ($this->_returnPath === null) {
00661 $email = $this->_filterEmail($email);
00662 $this->_returnPath = $email;
00663 $this->_storeHeader('Return-Path', $email, false);
00664 } else {
00668 require_once 'Zend/Mail/Exception.php';
00669 throw new Zend_Mail_Exception('Return-Path Header set twice');
00670 }
00671 return $this;
00672 }
00673
00681 public function getReturnPath()
00682 {
00683 if (null !== $this->_returnPath) {
00684 return $this->_returnPath;
00685 }
00686
00687 return $this->_from;
00688 }
00689
00695 public function clearReturnPath()
00696 {
00697 $this->_returnPath = null;
00698 $this->_clearHeader('Return-Path');
00699
00700 return $this;
00701 }
00702
00710 public function setSubject($subject)
00711 {
00712 if ($this->_subject === null) {
00713 $subject = $this->_filterOther($subject);
00714 $this->_subject = $this->_encodeHeader($subject);
00715 $this->_storeHeader('Subject', $this->_subject);
00716 } else {
00720 require_once 'Zend/Mail/Exception.php';
00721 throw new Zend_Mail_Exception('Subject set twice');
00722 }
00723 return $this;
00724 }
00725
00731 public function getSubject()
00732 {
00733 return $this->_subject;
00734 }
00735
00741 public function clearSubject()
00742 {
00743 $this->_subject = null;
00744 $this->_clearHeader('Subject');
00745
00746 return $this;
00747 }
00748
00756 public function setDate($date = null)
00757 {
00758 if ($this->_date === null) {
00759 if ($date === null) {
00760 $date = date('r');
00761 } else if (is_int($date)) {
00762 $date = date('r', $date);
00763 } else if (is_string($date)) {
00764 $date = strtotime($date);
00765 if ($date === false || $date < 0) {
00769 require_once 'Zend/Mail/Exception.php';
00770 throw new Zend_Mail_Exception('String representations of Date Header must be ' .
00771 'strtotime()-compatible');
00772 }
00773 $date = date('r', $date);
00774 } else if ($date instanceof Zend_Date) {
00775 $date = $date->get(Zend_Date::RFC_2822);
00776 } else {
00780 require_once 'Zend/Mail/Exception.php';
00781 throw new Zend_Mail_Exception(__METHOD__ . ' only accepts UNIX timestamps, Zend_Date objects, ' .
00782 ' and strtotime()-compatible strings');
00783 }
00784 $this->_date = $date;
00785 $this->_storeHeader('Date', $date);
00786 } else {
00790 require_once 'Zend/Mail/Exception.php';
00791 throw new Zend_Mail_Exception('Date Header set twice');
00792 }
00793 return $this;
00794 }
00795
00801 public function getDate()
00802 {
00803 return $this->_date;
00804 }
00805
00811 public function clearDate()
00812 {
00813 $this->_date = null;
00814 $this->_clearHeader('Date');
00815
00816 return $this;
00817 }
00818
00830 public function setMessageId($id = true)
00831 {
00832 if ($id === null || $id === false) {
00833 return $this;
00834 } elseif ($id === true) {
00835 $id = $this->createMessageId();
00836 }
00837
00838 if ($this->_messageId === null) {
00839 $id = $this->_filterOther($id);
00840 $this->_messageId = $id;
00841 $this->_storeHeader('Message-Id', $this->_messageId);
00842 } else {
00846 require_once 'Zend/Mail/Exception.php';
00847 throw new Zend_Mail_Exception('Message-ID set twice');
00848 }
00849
00850 return $this;
00851 }
00852
00858 public function getMessageId()
00859 {
00860 return $this->_messageId;
00861 }
00862
00863
00869 public function clearMessageId()
00870 {
00871 $this->_messageId = null;
00872 $this->_clearHeader('Message-Id');
00873
00874 return $this;
00875 }
00876
00882 public function createMessageId() {
00883
00884 $time = time();
00885
00886 if ($this->_from !== null) {
00887 $user = $this->_from;
00888 } elseif (isset($_SERVER['REMOTE_ADDR'])) {
00889 $user = $_SERVER['REMOTE_ADDR'];
00890 } else {
00891 $user = getmypid();
00892 }
00893
00894 $rand = mt_rand();
00895
00896 if ($this->_recipients !== array()) {
00897 $recipient = array_rand($this->_recipients);
00898 } else {
00899 $recipient = 'unknown';
00900 }
00901
00902 if (isset($_SERVER["SERVER_NAME"])) {
00903 $hostName = $_SERVER["SERVER_NAME"];
00904 } else {
00905 $hostName = php_uname('n');
00906 }
00907
00908 return sha1($time . $user . $rand . $recipient) . '@' . $hostName;
00909 }
00910
00920 public function addHeader($name, $value, $append = false)
00921 {
00922 $prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
00923 'return-path', 'date', 'message-id',
00924 );
00925 if (in_array(strtolower($name), $prohibit)) {
00929 require_once 'Zend/Mail/Exception.php';
00930 throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
00931 }
00932
00933 $value = $this->_filterOther($value);
00934 $value = $this->_encodeHeader($value);
00935 $this->_storeHeader($name, $value, $append);
00936
00937 return $this;
00938 }
00939
00945 public function getHeaders()
00946 {
00947 return $this->_headers;
00948 }
00949
00958 public function send($transport = null)
00959 {
00960 if ($transport === null) {
00961 if (! self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
00962 require_once 'Zend/Mail/Transport/Sendmail.php';
00963 $transport = new Zend_Mail_Transport_Sendmail();
00964 } else {
00965 $transport = self::$_defaultTransport;
00966 }
00967 }
00968
00969 if ($this->_date === null) {
00970 $this->setDate();
00971 }
00972
00973 $transport->send($this);
00974
00975 return $this;
00976 }
00977
00984 protected function _filterEmail($email)
00985 {
00986 $rule = array("\r" => '',
00987 "\n" => '',
00988 "\t" => '',
00989 '"' => '',
00990 ',' => '',
00991 '<' => '',
00992 '>' => '',
00993 );
00994
00995 return strtr($email, $rule);
00996 }
00997
01004 protected function _filterName($name)
01005 {
01006 $rule = array("\r" => '',
01007 "\n" => '',
01008 "\t" => '',
01009 '"' => "'",
01010 '<' => '[',
01011 '>' => ']',
01012 );
01013
01014 return trim(strtr($name, $rule));
01015 }
01016
01023 protected function _filterOther($data)
01024 {
01025 $rule = array("\r" => '',
01026 "\n" => '',
01027 "\t" => '',
01028 );
01029
01030 return strtr($data, $rule);
01031 }
01032
01040 protected function _formatAddress($email, $name)
01041 {
01042 if ($name === '' || $name === null || $name === $email) {
01043 return $email;
01044 } else {
01045 $encodedName = $this->_encodeHeader($name);
01046 if ($encodedName === $name && strpos($name, ',') !== false) {
01047 $format = '"%s" <%s>';
01048 } else {
01049 $format = '%s <%s>';
01050 }
01051 return sprintf($format, $encodedName, $email);
01052 }
01053 }
01054
01055 }