source: sandbox/2.2.0.2/library/Zend/Mail.php @ 4445

Revision 4445, 31.6 KB checked in by airton, 13 years ago (diff)

Ticket #1908 - Implementacao de melhorias na conta compartilhada - Adicao de arquivos e bibliotecas necessarias

  • Property svn:executable set to *
Line 
1<?php
2/**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category   Zend
16 * @package    Zend_Mail
17 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
19 * @version    $Id: Mail.php 23251 2010-10-26 12:47:55Z matthew $
20 */
21
22
23/**
24 * @see Zend_Mail_Transport_Abstract
25 */
26require_once 'Zend/Mail/Transport/Abstract.php';
27
28/**
29 * @see Zend_Mime
30 */
31require_once 'Zend/Mime.php';
32
33/**
34 * @see Zend_Mime_Message
35 */
36require_once 'Zend/Mime/Message.php';
37
38/**
39 * @see Zend_Mime_Part
40 */
41require_once 'Zend/Mime/Part.php';
42
43
44/**
45 * Class for sending an email.
46 *
47 * @category   Zend
48 * @package    Zend_Mail
49 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
50 * @license    http://framework.zend.com/license/new-bsd     New BSD License
51 */
52class Zend_Mail extends Zend_Mime_Message
53{
54    /**#@+
55     * @access protected
56     */
57
58    /**
59     * @var Zend_Mail_Transport_Abstract
60     * @static
61     */
62    protected static $_defaultTransport = null;
63
64    /**
65     * @var array
66     * @static
67     */
68    protected static $_defaultFrom;
69
70    /**
71     * @var array
72     * @static
73     */
74    protected static $_defaultReplyTo;
75
76    /**
77     * Mail character set
78     * @var string
79     */
80    protected $_charset = 'iso-8859-1';
81
82    /**
83     * Mail headers
84     * @var array
85     */
86    protected $_headers = array();
87
88    /**
89     * Encoding of Mail headers
90     * @var string
91     */
92    protected $_headerEncoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
93
94    /**
95     * From: address
96     * @var string
97     */
98    protected $_from = null;
99
100    /**
101     * To: addresses
102     * @var array
103     */
104    protected $_to = array();
105
106    /**
107     * Array of all recipients
108     * @var array
109     */
110    protected $_recipients = array();
111
112    /**
113     * Reply-To header
114     * @var string
115     */
116    protected $_replyTo = null;
117
118    /**
119     * Return-Path header
120     * @var string
121     */
122    protected $_returnPath = null;
123
124    /**
125     * Subject: header
126     * @var string
127     */
128    protected $_subject = null;
129
130    /**
131     * Date: header
132     * @var string
133     */
134    protected $_date = null;
135
136    /**
137     * Message-ID: header
138     * @var string
139     */
140    protected $_messageId = null;
141
142    /**
143     * text/plain MIME part
144     * @var false|Zend_Mime_Part
145     */
146    protected $_bodyText = false;
147
148    /**
149     * text/html MIME part
150     * @var false|Zend_Mime_Part
151     */
152    protected $_bodyHtml = false;
153
154    /**
155     * MIME boundary string
156     * @var string
157     */
158    protected $_mimeBoundary = null;
159
160    /**
161     * Content type of the message
162     * @var string
163     */
164    protected $_type = null;
165
166    /**#@-*/
167
168    /**
169     * Flag: whether or not email has attachments
170     * @var boolean
171     */
172    public $hasAttachments = false;
173
174
175    /**
176     * Sets the default mail transport for all following uses of
177     * Zend_Mail::send();
178     *
179     * @todo Allow passing a string to indicate the transport to load
180     * @todo Allow passing in optional options for the transport to load
181     * @param  Zend_Mail_Transport_Abstract $transport
182     */
183    public static function setDefaultTransport(Zend_Mail_Transport_Abstract $transport)
184    {
185        self::$_defaultTransport = $transport;
186    }
187
188    /**
189     * Gets the default mail transport for all following uses of
190     * unittests
191     *
192     * @todo Allow passing a string to indicate the transport to load
193     * @todo Allow passing in optional options for the transport to load
194     */
195    public static function getDefaultTransport()
196    {
197        return self::$_defaultTransport;
198    }
199
200    /**
201     * Clear the default transport property
202     */
203    public static function clearDefaultTransport()
204    {
205        self::$_defaultTransport = null;
206    }
207
208    /**
209     * Public constructor
210     *
211     * @param  string $charset
212     * @return void
213     */
214    public function __construct($charset = null)
215    {
216        if ($charset != null) {
217            $this->_charset = $charset;
218        }
219    }
220
221    /**
222     * Return charset string
223     *
224     * @return string
225     */
226    public function getCharset()
227    {
228        return $this->_charset;
229    }
230
231    /**
232     * Set content type
233     *
234     * Should only be used for manually setting multipart content types.
235     *
236     * @param  string $type Content type
237     * @return Zend_Mail Implements fluent interface
238     * @throws Zend_Mail_Exception for types not supported by Zend_Mime
239     */
240    public function setType($type)
241    {
242        $allowed = array(
243            Zend_Mime::MULTIPART_ALTERNATIVE,
244            Zend_Mime::MULTIPART_MIXED,
245            Zend_Mime::MULTIPART_RELATED,
246        );
247        if (!in_array($type, $allowed)) {
248            /**
249             * @see Zend_Mail_Exception
250             */
251            require_once 'Zend/Mail/Exception.php';
252            throw new Zend_Mail_Exception('Invalid content type "' . $type . '"');
253        }
254
255        $this->_type = $type;
256        return $this;
257    }
258
259    /**
260     * Get content type of the message
261     *
262     * @return string
263     */
264    public function getType()
265    {
266        return $this->_type;
267    }
268
269    /**
270     * Set an arbitrary mime boundary for the message
271     *
272     * If not set, Zend_Mime will generate one.
273     *
274     * @param  string    $boundary
275     * @return Zend_Mail Provides fluent interface
276     */
277    public function setMimeBoundary($boundary)
278    {
279        $this->_mimeBoundary = $boundary;
280
281        return $this;
282    }
283
284    /**
285     * Return the boundary string used for the message
286     *
287     * @return string
288     */
289    public function getMimeBoundary()
290    {
291        return $this->_mimeBoundary;
292    }
293
294    /**
295     * Return encoding of mail headers
296     *
297     * @deprecated use {@link getHeaderEncoding()} instead
298     * @return string
299     */
300    public function getEncodingOfHeaders()
301    {
302        return $this->getHeaderEncoding();
303    }
304
305    /**
306     * Return the encoding of mail headers
307     *
308     * Either Zend_Mime::ENCODING_QUOTEDPRINTABLE or Zend_Mime::ENCODING_BASE64
309     *
310     * @return string
311     */
312    public function getHeaderEncoding()
313    {
314        return $this->_headerEncoding;
315    }
316
317    /**
318     * Set the encoding of mail headers
319     *
320     * @deprecated Use {@link setHeaderEncoding()} instead.
321     * @param  string $encoding
322     * @return Zend_Mail
323     */
324    public function setEncodingOfHeaders($encoding)
325    {
326        return $this->setHeaderEncoding($encoding);
327    }
328
329    /**
330     * Set the encoding of mail headers
331     *
332     * @param  string $encoding Zend_Mime::ENCODING_QUOTEDPRINTABLE or Zend_Mime::ENCODING_BASE64
333     * @return Zend_Mail Provides fluent interface
334     */
335    public function setHeaderEncoding($encoding)
336    {
337        $allowed = array(
338            Zend_Mime::ENCODING_BASE64,
339            Zend_Mime::ENCODING_QUOTEDPRINTABLE
340        );
341        if (!in_array($encoding, $allowed)) {
342            /**
343             * @see Zend_Mail_Exception
344             */
345            require_once 'Zend/Mail/Exception.php';
346            throw new Zend_Mail_Exception('Invalid encoding "' . $encoding . '"');
347        }
348        $this->_headerEncoding = $encoding;
349
350        return $this;
351    }
352
353    /**
354     * Sets the text body for the message.
355     *
356     * @param  string $txt
357     * @param  string $charset
358     * @param  string $encoding
359     * @return Zend_Mail Provides fluent interface
360    */
361    public function setBodyText($txt, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
362    {
363        if ($charset === null) {
364            $charset = $this->_charset;
365        }
366
367        $mp = new Zend_Mime_Part($txt);
368        $mp->encoding = $encoding;
369        $mp->type = Zend_Mime::TYPE_TEXT;
370        $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
371        $mp->charset = $charset;
372
373        $this->_bodyText = $mp;
374
375        return $this;
376    }
377
378    /**
379     * Return text body Zend_Mime_Part or string
380     *
381     * @param  bool textOnly Whether to return just the body text content or the MIME part; defaults to false, the MIME part
382     * @return false|Zend_Mime_Part|string
383     */
384    public function getBodyText($textOnly = false)
385    {
386        if ($textOnly && $this->_bodyText) {
387            $body = $this->_bodyText;
388            return $body->getContent();
389        }
390
391        return $this->_bodyText;
392    }
393
394    /**
395     * Sets the HTML body for the message
396     *
397     * @param  string    $html
398     * @param  string    $charset
399     * @param  string    $encoding
400     * @return Zend_Mail Provides fluent interface
401     */
402    public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
403    {
404        if ($charset === null) {
405            $charset = $this->_charset;
406        }
407
408        $mp = new Zend_Mime_Part($html);
409        $mp->encoding = $encoding;
410        $mp->type = Zend_Mime::TYPE_HTML;
411        $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
412        $mp->charset = $charset;
413
414        $this->_bodyHtml = $mp;
415
416        return $this;
417    }
418
419    /**
420     * Return Zend_Mime_Part representing body HTML
421     *
422     * @param  bool $htmlOnly Whether to return the body HTML only, or the MIME part; defaults to false, the MIME part
423     * @return false|Zend_Mime_Part|string
424     */
425    public function getBodyHtml($htmlOnly = false)
426    {
427        if ($htmlOnly && $this->_bodyHtml) {
428            $body = $this->_bodyHtml;
429            return $body->getContent();
430        }
431
432        return $this->_bodyHtml;
433    }
434
435    /**
436     * Adds an existing attachment to the mail message
437     *
438     * @param  Zend_Mime_Part $attachment
439     * @return Zend_Mail Provides fluent interface
440     */
441    public function addAttachment(Zend_Mime_Part $attachment)
442    {
443        $this->addPart($attachment);
444        $this->hasAttachments = true;
445
446        return $this;
447    }
448
449    /**
450     * Creates a Zend_Mime_Part attachment
451     *
452     * Attachment is automatically added to the mail object after creation. The
453     * attachment object is returned to allow for further manipulation.
454     *
455     * @param  string         $body
456     * @param  string         $mimeType
457     * @param  string         $disposition
458     * @param  string         $encoding
459     * @param  string         $filename OPTIONAL A filename for the attachment
460     * @return Zend_Mime_Part Newly created Zend_Mime_Part object (to allow
461     * advanced settings)
462     */
463    public function createAttachment($body,
464                                     $mimeType    = Zend_Mime::TYPE_OCTETSTREAM,
465                                     $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
466                                     $encoding    = Zend_Mime::ENCODING_BASE64,
467                                     $filename    = null)
468    {
469
470        $mp = new Zend_Mime_Part($body);
471        $mp->encoding = $encoding;
472        $mp->type = $mimeType;
473        $mp->disposition = $disposition;
474        $mp->filename = $filename;
475
476        $this->addAttachment($mp);
477
478        return $mp;
479    }
480
481    /**
482     * Return a count of message parts
483     *
484     * @return integer
485     */
486    public function getPartCount()
487    {
488        return count($this->_parts);
489    }
490
491    /**
492     * Encode header fields
493     *
494     * Encodes header content according to RFC1522 if it contains non-printable
495     * characters.
496     *
497     * @param  string $value
498     * @return string
499     */
500    protected function _encodeHeader($value)
501    {
502        if (Zend_Mime::isPrintable($value) === false) {
503            if ($this->getHeaderEncoding() === Zend_Mime::ENCODING_QUOTEDPRINTABLE) {
504                $value = Zend_Mime::encodeQuotedPrintableHeader($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
505            } else {
506                $value = Zend_Mime::encodeBase64Header($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
507            }
508        }
509
510        return $value;
511    }
512
513    /**
514     * Add a header to the message
515     *
516     * Adds a header to this message. If append is true and the header already
517     * exists, raises a flag indicating that the header should be appended.
518     *
519     * @param string  $headerName
520     * @param string  $value
521     * @param bool $append
522     */
523    protected function _storeHeader($headerName, $value, $append = false)
524    {
525        if (isset($this->_headers[$headerName])) {
526            $this->_headers[$headerName][] = $value;
527        } else {
528            $this->_headers[$headerName] = array($value);
529        }
530
531        if ($append) {
532            $this->_headers[$headerName]['append'] = true;
533        }
534
535    }
536
537    /**
538     * Clear header from the message
539     *
540     * @param string $headerName
541     * @deprecated use public method directly
542     */
543    protected function _clearHeader($headerName)
544    {
545        $this->clearHeader($headerName);
546    }
547
548    /**
549     * Helper function for adding a recipient and the corresponding header
550     *
551     * @param string $headerName
552     * @param string $email
553     * @param string $name
554     */
555    protected function _addRecipientAndHeader($headerName, $email, $name)
556    {
557        $email = $this->_filterEmail($email);
558        $name  = $this->_filterName($name);
559        // prevent duplicates
560        $this->_recipients[$email] = 1;
561        $this->_storeHeader($headerName, $this->_formatAddress($email, $name), true);
562    }
563
564    /**
565     * Adds To-header and recipient, $email can be an array, or a single string address
566     *
567     * @param  string|array $email
568     * @param  string $name
569     * @return Zend_Mail Provides fluent interface
570     */
571    public function addTo($email, $name='')
572    {
573        if (!is_array($email)) {
574            $email = array($name => $email);
575        }
576
577        foreach ($email as $n => $recipient) {
578            $this->_addRecipientAndHeader('To', $recipient, is_int($n) ? '' : $n);
579            $this->_to[] = $recipient;
580        }
581
582        return $this;
583    }
584
585    /**
586     * Adds Cc-header and recipient, $email can be an array, or a single string address
587     *
588     * @param  string|array    $email
589     * @param  string    $name
590     * @return Zend_Mail Provides fluent interface
591     */
592    public function addCc($email, $name='')
593    {
594        if (!is_array($email)) {
595            $email = array($name => $email);
596        }
597
598        foreach ($email as $n => $recipient) {
599            $this->_addRecipientAndHeader('Cc', $recipient, is_int($n) ? '' : $n);
600        }
601
602        return $this;
603    }
604
605    /**
606     * Adds Bcc recipient, $email can be an array, or a single string address
607     *
608     * @param  string|array    $email
609     * @return Zend_Mail Provides fluent interface
610     */
611    public function addBcc($email)
612    {
613        if (!is_array($email)) {
614            $email = array($email);
615        }
616
617        foreach ($email as $recipient) {
618            $this->_addRecipientAndHeader('Bcc', $recipient, '');
619        }
620
621        return $this;
622    }
623
624    /**
625     * Return list of recipient email addresses
626     *
627     * @return array (of strings)
628     */
629    public function getRecipients()
630    {
631        return array_keys($this->_recipients);
632    }
633
634    /**
635     * Clear header from the message
636     *
637     * @param string $headerName
638     * @return Zend_Mail Provides fluent inter
639     */
640    public function clearHeader($headerName)
641    {
642        if (isset($this->_headers[$headerName])){
643            unset($this->_headers[$headerName]);
644        }
645        return $this;
646    }
647
648    /**
649     * Clears list of recipient email addresses
650     *
651     * @return Zend_Mail Provides fluent interface
652     */
653    public function clearRecipients()
654    {
655        $this->_recipients = array();
656        $this->_to = array();
657
658        $this->clearHeader('To');
659        $this->clearHeader('Cc');
660        $this->clearHeader('Bcc');
661
662        return $this;
663    }
664
665    /**
666     * Sets From-header and sender of the message
667     *
668     * @param  string    $email
669     * @param  string    $name
670     * @return Zend_Mail Provides fluent interface
671     * @throws Zend_Mail_Exception if called subsequent times
672     */
673    public function setFrom($email, $name = null)
674    {
675        if (null !== $this->_from) {
676            /**
677             * @see Zend_Mail_Exception
678             */
679            require_once 'Zend/Mail/Exception.php';
680            throw new Zend_Mail_Exception('From Header set twice');
681        }
682
683        $email = $this->_filterEmail($email);
684        $name  = $this->_filterName($name);
685        $this->_from = $email;
686        $this->_storeHeader('From', $this->_formatAddress($email, $name), true);
687
688        return $this;
689    }
690
691    /**
692     * Set Reply-To Header
693     *
694     * @param string $email
695     * @param string $name
696     * @return Zend_Mail
697     * @throws Zend_Mail_Exception if called more than one time
698     */
699    public function setReplyTo($email, $name = null)
700    {
701        if (null !== $this->_replyTo) {
702            /**
703             * @see Zend_Mail_Exception
704             */
705            require_once 'Zend/Mail/Exception.php';
706            throw new Zend_Mail_Exception('Reply-To Header set twice');
707        }
708
709        $email = $this->_filterEmail($email);
710        $name  = $this->_filterName($name);
711        $this->_replyTo = $email;
712        $this->_storeHeader('Reply-To', $this->_formatAddress($email, $name), true);
713
714        return $this;
715    }
716
717    /**
718     * Returns the sender of the mail
719     *
720     * @return string
721     */
722    public function getFrom()
723    {
724        return $this->_from;
725    }
726
727    /**
728     * Returns the current Reply-To address of the message
729     *
730     * @return string|null Reply-To address, null when not set
731     */
732    public function getReplyTo()
733    {
734        return $this->_replyTo;
735    }
736
737    /**
738     * Clears the sender from the mail
739     *
740     * @return Zend_Mail Provides fluent interface
741     */
742    public function clearFrom()
743    {
744        $this->_from = null;
745        $this->clearHeader('From');
746
747        return $this;
748    }
749
750     /**
751      * Clears the current Reply-To address from the message
752      *
753      * @return Zend_Mail Provides fluent interface
754      */
755    public function clearReplyTo()
756    {
757        $this->_replyTo = null;
758        $this->clearHeader('Reply-To');
759
760        return $this;
761    }
762
763    /**
764     * Sets Default From-email and name of the message
765     *
766     * @param  string               $email
767     * @param  string    Optional   $name
768     * @return void
769     */
770    public static function setDefaultFrom($email, $name = null)
771    {
772        self::$_defaultFrom = array('email' => $email, 'name' => $name);
773    }
774
775    /**
776     * Returns the default sender of the mail
777     *
778     * @return null|array   Null if none was set.
779     */
780    public static function getDefaultFrom()
781    {
782        return self::$_defaultFrom;
783    }
784
785    /**
786     * Clears the default sender from the mail
787     *
788     * @return void
789     */
790    public static function clearDefaultFrom()
791    {
792        self::$_defaultFrom = null;
793    }
794
795    /**
796     * Sets From-name and -email based on the defaults
797     *
798     * @return Zend_Mail Provides fluent interface
799     */
800    public function setFromToDefaultFrom() {
801        $from = self::getDefaultFrom();
802        if($from === null) {
803            require_once 'Zend/Mail/Exception.php';
804            throw new Zend_Mail_Exception(
805                'No default From Address set to use');
806        }
807
808        $this->setFrom($from['email'], $from['name']);
809
810        return $this;
811    }
812
813    /**
814     * Sets Default ReplyTo-address and -name of the message
815     *
816     * @param  string               $email
817     * @param  string    Optional   $name
818     * @return void
819     */
820    public static function setDefaultReplyTo($email, $name = null)
821    {
822        self::$_defaultReplyTo = array('email' => $email, 'name' => $name);
823    }
824
825    /**
826     * Returns the default Reply-To Address and Name of the mail
827     *
828     * @return null|array   Null if none was set.
829     */
830    public static function getDefaultReplyTo()
831    {
832        return self::$_defaultReplyTo;
833    }
834
835    /**
836     * Clears the default ReplyTo-address and -name from the mail
837     *
838     * @return void
839     */
840    public static function clearDefaultReplyTo()
841    {
842        self::$_defaultReplyTo = null;
843    }
844
845    /**
846     * Sets ReplyTo-name and -email based on the defaults
847     *
848     * @return Zend_Mail Provides fluent interface
849     */
850    public function setReplyToFromDefault() {
851        $replyTo = self::getDefaultReplyTo();
852        if($replyTo === null) {
853            require_once 'Zend/Mail/Exception.php';
854            throw new Zend_Mail_Exception(
855                'No default Reply-To Address set to use');
856        }
857
858        $this->setReplyTo($replyTo['email'], $replyTo['name']);
859
860        return $this;
861    }
862
863    /**
864     * Sets the Return-Path header of the message
865     *
866     * @param  string    $email
867     * @return Zend_Mail Provides fluent interface
868     * @throws Zend_Mail_Exception if set multiple times
869     */
870    public function setReturnPath($email)
871    {
872        if ($this->_returnPath === null) {
873            $email = $this->_filterEmail($email);
874            $this->_returnPath = $email;
875            $this->_storeHeader('Return-Path', $email, false);
876        } else {
877            /**
878             * @see Zend_Mail_Exception
879             */
880            require_once 'Zend/Mail/Exception.php';
881            throw new Zend_Mail_Exception('Return-Path Header set twice');
882        }
883        return $this;
884    }
885
886    /**
887     * Returns the current Return-Path address of the message
888     *
889     * If no Return-Path header is set, returns the value of {@link $_from}.
890     *
891     * @return string
892     */
893    public function getReturnPath()
894    {
895        if (null !== $this->_returnPath) {
896            return $this->_returnPath;
897        }
898
899        return $this->_from;
900    }
901
902    /**
903     * Clears the current Return-Path address from the message
904     *
905     * @return Zend_Mail Provides fluent interface
906     */
907    public function clearReturnPath()
908    {
909        $this->_returnPath = null;
910        $this->clearHeader('Return-Path');
911
912        return $this;
913    }
914
915    /**
916     * Sets the subject of the message
917     *
918     * @param   string    $subject
919     * @return  Zend_Mail Provides fluent interface
920     * @throws  Zend_Mail_Exception
921     */
922    public function setSubject($subject)
923    {
924        if ($this->_subject === null) {
925            $subject = $this->_filterOther($subject);
926            $this->_subject = $this->_encodeHeader($subject);
927            $this->_storeHeader('Subject', $this->_subject);
928        } else {
929            /**
930             * @see Zend_Mail_Exception
931             */
932            require_once 'Zend/Mail/Exception.php';
933            throw new Zend_Mail_Exception('Subject set twice');
934        }
935        return $this;
936    }
937
938    /**
939     * Returns the encoded subject of the message
940     *
941     * @return string
942     */
943    public function getSubject()
944    {
945        return $this->_subject;
946    }
947
948    /**
949     * Clears the encoded subject from the message
950     *
951     * @return  Zend_Mail Provides fluent interface
952     */
953    public function clearSubject()
954    {
955        $this->_subject = null;
956        $this->clearHeader('Subject');
957
958        return $this;
959    }
960
961    /**
962     * Sets Date-header
963     *
964     * @param  timestamp|string|Zend_Date $date
965     * @return Zend_Mail Provides fluent interface
966     * @throws Zend_Mail_Exception if called subsequent times or wrong date format.
967     */
968    public function setDate($date = null)
969    {
970        if ($this->_date === null) {
971            if ($date === null) {
972                $date = date('r');
973            } else if (is_int($date)) {
974                $date = date('r', $date);
975            } else if (is_string($date)) {
976                $date = strtotime($date);
977                if ($date === false || $date < 0) {
978                    /**
979                     * @see Zend_Mail_Exception
980                     */
981                    require_once 'Zend/Mail/Exception.php';
982                    throw new Zend_Mail_Exception('String representations of Date Header must be ' .
983                                                  'strtotime()-compatible');
984                }
985                $date = date('r', $date);
986            } else if ($date instanceof Zend_Date) {
987                $date = $date->get(Zend_Date::RFC_2822);
988            } else {
989                /**
990                 * @see Zend_Mail_Exception
991                 */
992                require_once 'Zend/Mail/Exception.php';
993                throw new Zend_Mail_Exception(__METHOD__ . ' only accepts UNIX timestamps, Zend_Date objects, ' .
994                                              ' and strtotime()-compatible strings');
995            }
996            $this->_date = $date;
997            $this->_storeHeader('Date', $date);
998        } else {
999            /**
1000             * @see Zend_Mail_Exception
1001             */
1002            require_once 'Zend/Mail/Exception.php';
1003            throw new Zend_Mail_Exception('Date Header set twice');
1004        }
1005        return $this;
1006    }
1007
1008    /**
1009     * Returns the formatted date of the message
1010     *
1011     * @return string
1012     */
1013    public function getDate()
1014    {
1015        return $this->_date;
1016    }
1017
1018    /**
1019     * Clears the formatted date from the message
1020     *
1021     * @return Zend_Mail Provides fluent interface
1022     */
1023    public function clearDate()
1024    {
1025        $this->_date = null;
1026        $this->clearHeader('Date');
1027
1028        return $this;
1029    }
1030
1031    /**
1032     * Sets the Message-ID of the message
1033     *
1034     * @param   boolean|string  $id
1035     * true  :Auto
1036     * false :No set
1037     * null  :No set
1038     * string:Sets given string (Angle brackets is not necessary)
1039     * @return  Zend_Mail Provides fluent interface
1040     * @throws  Zend_Mail_Exception
1041     */
1042    public function setMessageId($id = true)
1043    {
1044        if ($id === null || $id === false) {
1045            return $this;
1046        } elseif ($id === true) {
1047            $id = $this->createMessageId();
1048        }
1049
1050        if ($this->_messageId === null) {
1051            $id = $this->_filterOther($id);
1052            $this->_messageId = $id;
1053            $this->_storeHeader('Message-Id', '<' . $this->_messageId . '>');
1054        } else {
1055            /**
1056             * @see Zend_Mail_Exception
1057             */
1058            require_once 'Zend/Mail/Exception.php';
1059            throw new Zend_Mail_Exception('Message-ID set twice');
1060        }
1061
1062        return $this;
1063    }
1064
1065    /**
1066     * Returns the Message-ID of the message
1067     *
1068     * @return string
1069     */
1070    public function getMessageId()
1071    {
1072        return $this->_messageId;
1073    }
1074
1075
1076    /**
1077     * Clears the Message-ID from the message
1078     *
1079     * @return Zend_Mail Provides fluent interface
1080     */
1081    public function clearMessageId()
1082    {
1083        $this->_messageId = null;
1084        $this->clearHeader('Message-Id');
1085
1086        return $this;
1087    }
1088
1089    /**
1090     * Creates the Message-ID
1091     *
1092     * @return string
1093     */
1094    public function createMessageId() {
1095
1096        $time = time();
1097
1098        if ($this->_from !== null) {
1099            $user = $this->_from;
1100        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
1101            $user = $_SERVER['REMOTE_ADDR'];
1102        } else {
1103            $user = getmypid();
1104        }
1105
1106        $rand = mt_rand();
1107
1108        if ($this->_recipients !== array()) {
1109            $recipient = array_rand($this->_recipients);
1110        } else {
1111            $recipient = 'unknown';
1112        }
1113
1114        if (isset($_SERVER["SERVER_NAME"])) {
1115            $hostName = $_SERVER["SERVER_NAME"];
1116        } else {
1117            $hostName = php_uname('n');
1118        }
1119
1120        return sha1($time . $user . $rand . $recipient) . '@' . $hostName;
1121    }
1122
1123    /**
1124     * Add a custom header to the message
1125     *
1126     * @param  string              $name
1127     * @param  string              $value
1128     * @param  boolean             $append
1129     * @return Zend_Mail           Provides fluent interface
1130     * @throws Zend_Mail_Exception on attempts to create standard headers
1131     */
1132    public function addHeader($name, $value, $append = false)
1133    {
1134        $prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
1135                          'reply-to', 'return-path',
1136                          'date', 'message-id',
1137                         );
1138        if (in_array(strtolower($name), $prohibit)) {
1139            /**
1140             * @see Zend_Mail_Exception
1141             */
1142            require_once 'Zend/Mail/Exception.php';
1143            throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
1144        }
1145
1146        $value = $this->_filterOther($value);
1147        $value = $this->_encodeHeader($value);
1148        $this->_storeHeader($name, $value, $append);
1149
1150        return $this;
1151    }
1152
1153    /**
1154     * Return mail headers
1155     *
1156     * @return void
1157     */
1158    public function getHeaders()
1159    {
1160        return $this->_headers;
1161    }
1162
1163    /**
1164     * Sends this email using the given transport or a previously
1165     * set DefaultTransport or the internal mail function if no
1166     * default transport had been set.
1167     *
1168     * @param  Zend_Mail_Transport_Abstract $transport
1169     * @return Zend_Mail                    Provides fluent interface
1170     */
1171    public function send($transport = null)
1172    {
1173        if ($transport === null) {
1174            if (! self::$_defaultTransport instanceof Zend_Mail_Transport_Abstract) {
1175                require_once 'Zend/Mail/Transport/Sendmail.php';
1176                $transport = new Zend_Mail_Transport_Sendmail();
1177            } else {
1178                $transport = self::$_defaultTransport;
1179            }
1180        }
1181
1182        if ($this->_date === null) {
1183            $this->setDate();
1184        }
1185
1186        if(null === $this->_from && null !== self::getDefaultFrom()) {
1187            $this->setFromToDefaultFrom();
1188        }
1189
1190        if(null === $this->_replyTo && null !== self::getDefaultReplyTo()) {
1191            $this->setReplyToFromDefault();
1192        }
1193
1194        $transport->send($this);
1195
1196        return $this;
1197    }
1198
1199    /**
1200     * Filter of email data
1201     *
1202     * @param string $email
1203     * @return string
1204     */
1205    protected function _filterEmail($email)
1206    {
1207        $rule = array("\r" => '',
1208                      "\n" => '',
1209                      "\t" => '',
1210                      '"'  => '',
1211                      ','  => '',
1212                      '<'  => '',
1213                      '>'  => '',
1214        );
1215
1216        return strtr($email, $rule);
1217    }
1218
1219    /**
1220     * Filter of name data
1221     *
1222     * @param string $name
1223     * @return string
1224     */
1225    protected function _filterName($name)
1226    {
1227        $rule = array("\r" => '',
1228                      "\n" => '',
1229                      "\t" => '',
1230                      '"'  => "'",
1231                      '<'  => '[',
1232                      '>'  => ']',
1233        );
1234
1235        return trim(strtr($name, $rule));
1236    }
1237
1238    /**
1239     * Filter of other data
1240     *
1241     * @param string $data
1242     * @return string
1243     */
1244    protected function _filterOther($data)
1245    {
1246        $rule = array("\r" => '',
1247                      "\n" => '',
1248                      "\t" => '',
1249        );
1250
1251        return strtr($data, $rule);
1252    }
1253
1254    /**
1255     * Formats e-mail address
1256     *
1257     * @param string $email
1258     * @param string $name
1259     * @return string
1260     */
1261    protected function _formatAddress($email, $name)
1262    {
1263        if ($name === '' || $name === null || $name === $email) {
1264            return $email;
1265        } else {
1266            $encodedName = $this->_encodeHeader($name);
1267            if ($encodedName === $name &&
1268                    ((strpos($name, '@') !== false) || (strpos($name, ',') !== false))) {
1269                $format = '"%s" <%s>';
1270            } else {
1271                $format = '%s <%s>';
1272            }
1273            return sprintf($format, $encodedName, $email);
1274        }
1275    }
1276
1277}
Note: See TracBrowser for help on using the repository browser.