"Fossies" - the Fresh Open Source Software Archive

Member "icingaweb2-2.11.4/library/vendor/Zend/Mail/Transport/Abstract.php" (26 Jan 2023, 10069 Bytes) of package /linux/www/icingaweb2-2.11.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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  * @subpackage Transport
   18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   20  * @version    $Id$
   21  */
   22 
   23 
   24 /**
   25  * @see Zend_Mime
   26  */
   27 
   28 
   29 /**
   30  * Abstract for sending eMails through different
   31  * ways of transport
   32  *
   33  * @category   Zend
   34  * @package    Zend_Mail
   35  * @subpackage Transport
   36  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   38  */
   39 abstract class Zend_Mail_Transport_Abstract
   40 {
   41     /**
   42      * Mail body
   43      * @var string
   44      * @access public
   45      */
   46     public $body = '';
   47 
   48     /**
   49      * MIME boundary
   50      * @var string
   51      * @access public
   52      */
   53     public $boundary = '';
   54 
   55     /**
   56      * Mail header string
   57      * @var string
   58      * @access public
   59      */
   60     public $header = '';
   61 
   62     /**
   63      * Array of message headers
   64      * @var array
   65      * @access protected
   66      */
   67     protected $_headers = array();
   68 
   69     /**
   70      * Message is a multipart message
   71      * @var boolean
   72      * @access protected
   73      */
   74     protected $_isMultipart = false;
   75 
   76     /**
   77      * Zend_Mail object
   78      * @var false|Zend_Mail
   79      * @access protected
   80      */
   81     protected $_mail = false;
   82 
   83     /**
   84      * Array of message parts
   85      * @var array
   86      * @access protected
   87      */
   88     protected $_parts = array();
   89 
   90     /**
   91      * Recipients string
   92      * @var string
   93      * @access public
   94      */
   95     public $recipients = '';
   96 
   97     /**
   98      * EOL character string used by transport
   99      * @var string
  100      * @access public
  101      */
  102     public $EOL = "\r\n";
  103 
  104     /**
  105      * Send an email independent from the used transport
  106      *
  107      * The requisite information for the email will be found in the following
  108      * properties:
  109      *
  110      * - {@link $recipients} - list of recipients (string)
  111      * - {@link $header} - message header
  112      * - {@link $body} - message body
  113      */
  114     abstract protected function _sendMail();
  115 
  116     /**
  117      * Return all mail headers as an array
  118      *
  119      * If a boundary is given, a multipart header is generated with a
  120      * Content-Type of either multipart/alternative or multipart/mixed depending
  121      * on the mail parts present in the {@link $_mail Zend_Mail object} present.
  122      *
  123      * @param string $boundary
  124      * @return array
  125      */
  126     protected function _getHeaders($boundary)
  127     {
  128         if (null !== $boundary) {
  129             // Build multipart mail
  130             $type = $this->_mail->getType();
  131             if (!$type) {
  132                 if ($this->_mail->hasAttachments) {
  133                     $type = Zend_Mime::MULTIPART_MIXED;
  134                 } elseif ($this->_mail->getBodyText() && $this->_mail->getBodyHtml()) {
  135                     $type = Zend_Mime::MULTIPART_ALTERNATIVE;
  136                 } else {
  137                     $type = Zend_Mime::MULTIPART_MIXED;
  138                 }
  139             }
  140 
  141             $this->_headers['Content-Type'] = array(
  142                 $type . ';'
  143                 . $this->EOL
  144                 . " " . 'boundary="' . $boundary . '"'
  145             );
  146             $this->boundary = $boundary;
  147         }
  148 
  149         $this->_headers['MIME-Version'] = array('1.0');
  150 
  151         return $this->_headers;
  152     }
  153 
  154     /**
  155      * Prepend header name to header value
  156      *
  157      * @param string $item
  158      * @param string $key
  159      * @param string $prefix
  160      * @static
  161      * @access protected
  162      * @return void
  163      */
  164     protected static function _formatHeader(&$item, $key, $prefix)
  165     {
  166         $item = $prefix . ': ' . $item;
  167     }
  168 
  169     /**
  170      * Prepare header string for use in transport
  171      *
  172      * Prepares and generates {@link $header} based on the headers provided.
  173      *
  174      * @param mixed $headers
  175      * @access protected
  176      * @return void
  177      * @throws Zend_Mail_Transport_Exception if any header lines exceed 998
  178      * characters
  179      */
  180     protected function _prepareHeaders($headers)
  181     {
  182         if (!$this->_mail) {
  183             /**
  184              * @see Zend_Mail_Transport_Exception
  185              */
  186             throw new Zend_Mail_Transport_Exception('Missing Zend_Mail object in _mail property');
  187         }
  188 
  189         $this->header = '';
  190 
  191         foreach ($headers as $header => $content) {
  192             if (isset($content['append'])) {
  193                 unset($content['append']);
  194                 $value = implode(',' . $this->EOL . ' ', $content);
  195                 $this->header .= $header . ': ' . $value . $this->EOL;
  196             } else {
  197                 array_walk($content, array(get_class($this), '_formatHeader'), $header);
  198                 $this->header .= implode($this->EOL, $content) . $this->EOL;
  199             }
  200         }
  201 
  202         // Sanity check on headers -- should not be > 998 characters
  203         $sane = true;
  204         foreach (explode($this->EOL, $this->header) as $line) {
  205             if (strlen(trim($line)) > 998) {
  206                 $sane = false;
  207                 break;
  208             }
  209         }
  210         if (!$sane) {
  211             /**
  212              * @see Zend_Mail_Transport_Exception
  213              */
  214             throw new Zend_Mail_Exception('At least one mail header line is too long');
  215         }
  216     }
  217 
  218     /**
  219      * Generate MIME compliant message from the current configuration
  220      *
  221      * If both a text and HTML body are present, generates a
  222      * multipart/alternative Zend_Mime_Part containing the headers and contents
  223      * of each. Otherwise, uses whichever of the text or HTML parts present.
  224      *
  225      * The content part is then prepended to the list of Zend_Mime_Parts for
  226      * this message.
  227      *
  228      * @return void
  229      */
  230     protected function _buildBody()
  231     {
  232         if (($text = $this->_mail->getBodyText())
  233             && ($html = $this->_mail->getBodyHtml()))
  234         {
  235             // Generate unique boundary for multipart/alternative
  236             $mime = new Zend_Mime(null);
  237             $boundaryLine = $mime->boundaryLine($this->EOL);
  238             $boundaryEnd  = $mime->mimeEnd($this->EOL);
  239 
  240             $text->disposition = false;
  241             $html->disposition = false;
  242 
  243             $body = $boundaryLine
  244                   . $text->getHeaders($this->EOL)
  245                   . $this->EOL
  246                   . $text->getContent($this->EOL)
  247                   . $this->EOL
  248                   . $boundaryLine
  249                   . $html->getHeaders($this->EOL)
  250                   . $this->EOL
  251                   . $html->getContent($this->EOL)
  252                   . $this->EOL
  253                   . $boundaryEnd;
  254 
  255             $mp           = new Zend_Mime_Part($body);
  256             $mp->type     = Zend_Mime::MULTIPART_ALTERNATIVE;
  257             $mp->boundary = $mime->boundary();
  258 
  259             $this->_isMultipart = true;
  260 
  261             // Ensure first part contains text alternatives
  262             array_unshift($this->_parts, $mp);
  263 
  264             // Get headers
  265             $this->_headers = $this->_mail->getHeaders();
  266             return;
  267         }
  268 
  269         // If not multipart, then get the body
  270         if (false !== ($body = $this->_mail->getBodyHtml())) {
  271             array_unshift($this->_parts, $body);
  272         } elseif (false !== ($body = $this->_mail->getBodyText())) {
  273             array_unshift($this->_parts, $body);
  274         }
  275 
  276         if (!$body) {
  277             /**
  278              * @see Zend_Mail_Transport_Exception
  279              */
  280             throw new Zend_Mail_Transport_Exception('No body specified');
  281         }
  282 
  283         // Get headers
  284         $this->_headers = $this->_mail->getHeaders();
  285         $headers = $body->getHeadersArray($this->EOL);
  286         foreach ($headers as $header) {
  287             // Headers in Zend_Mime_Part are kept as arrays with two elements, a
  288             // key and a value
  289             $this->_headers[$header[0]] = array($header[1]);
  290         }
  291     }
  292 
  293     /**
  294      * Send a mail using this transport
  295      *
  296      * @param  Zend_Mail $mail
  297      * @access public
  298      * @return void
  299      * @throws Zend_Mail_Transport_Exception if mail is empty
  300      */
  301     public function send(Zend_Mail $mail)
  302     {
  303         $this->_isMultipart = false;
  304         $this->_mail        = $mail;
  305         $this->_parts       = $mail->getParts();
  306         $mime               = $mail->getMime();
  307 
  308         // Build body content
  309         $this->_buildBody();
  310 
  311         // Determine number of parts and boundary
  312         $count    = count($this->_parts);
  313         $boundary = null;
  314         if ($count < 1) {
  315             /**
  316              * @see Zend_Mail_Transport_Exception
  317              */
  318             throw new Zend_Mail_Transport_Exception('Empty mail cannot be sent');
  319         }
  320 
  321         if ($count > 1) {
  322             // Multipart message; create new MIME object and boundary
  323             $mime     = new Zend_Mime($this->_mail->getMimeBoundary());
  324             $boundary = $mime->boundary();
  325         } elseif ($this->_isMultipart) {
  326             // multipart/alternative -- grab boundary
  327             $boundary = $this->_parts[0]->boundary;
  328         }
  329 
  330         // Determine recipients, and prepare headers
  331         $this->recipients = implode(',', $mail->getRecipients());
  332         $this->_prepareHeaders($this->_getHeaders($boundary));
  333 
  334         // Create message body
  335         // This is done so that the same Zend_Mail object can be used in
  336         // multiple transports
  337         $message = new Zend_Mime_Message();
  338         $message->setParts($this->_parts);
  339         $message->setMime($mime);
  340         $this->body = $message->generateMessage($this->EOL);
  341 
  342         // Send to transport!
  343         $this->_sendMail();
  344     }
  345 }