00001 <?php
00027 require_once 'Zend/Mime.php';
00028
00029
00040 abstract class Zend_Mail_Transport_Abstract
00041 {
00047 public $body = '';
00048
00054 public $boundary = '';
00055
00061 public $header = '';
00062
00068 protected $_headers = array();
00069
00075 protected $_isMultipart = false;
00076
00082 protected $_mail = false;
00083
00089 protected $_parts = array();
00090
00096 public $recipients = '';
00097
00103 public $EOL = "\r\n";
00104
00115 abstract protected function _sendMail();
00116
00127 protected function _getHeaders($boundary)
00128 {
00129 if (null !== $boundary) {
00130
00131 $type = $this->_mail->getType();
00132 if (!$type) {
00133 if ($this->_mail->hasAttachments) {
00134 $type = Zend_Mime::MULTIPART_MIXED;
00135 } elseif ($this->_mail->getBodyText() && $this->_mail->getBodyHtml()) {
00136 $type = Zend_Mime::MULTIPART_ALTERNATIVE;
00137 } else {
00138 $type = Zend_Mime::MULTIPART_MIXED;
00139 }
00140 }
00141
00142 $this->_headers['Content-Type'] = array(
00143 $type . ';'
00144 . $this->EOL
00145 . " " . 'boundary="' . $boundary . '"'
00146 );
00147 $this->boundary = $boundary;
00148 }
00149
00150 $this->_headers['MIME-Version'] = array('1.0');
00151
00152 return $this->_headers;
00153 }
00154
00165 protected static function _formatHeader(&$item, $key, $prefix)
00166 {
00167 $item = $prefix . ': ' . $item;
00168 }
00169
00181 protected function _prepareHeaders($headers)
00182 {
00183 if (!$this->_mail) {
00187 require_once 'Zend/Mail/Transport/Exception.php';
00188 throw new Zend_Mail_Transport_Exception('Missing Zend_Mail object in _mail property');
00189 }
00190
00191 $this->header = '';
00192
00193 foreach ($headers as $header => $content) {
00194 if (isset($content['append'])) {
00195 unset($content['append']);
00196 $value = implode(',' . $this->EOL . ' ', $content);
00197 $this->header .= $header . ': ' . $value . $this->EOL;
00198 } else {
00199 array_walk($content, array(get_class($this), '_formatHeader'), $header);
00200 $this->header .= implode($this->EOL, $content) . $this->EOL;
00201 }
00202 }
00203
00204
00205 $sane = true;
00206 foreach (explode($this->EOL, $this->header) as $line) {
00207 if (strlen(trim($line)) > 998) {
00208 $sane = false;
00209 break;
00210 }
00211 }
00212 if (!$sane) {
00216 require_once 'Zend/Mail/Transport/Exception.php';
00217 throw new Zend_Mail_Exception('At least one mail header line is too long');
00218 }
00219 }
00220
00233 protected function _buildBody()
00234 {
00235 if (($text = $this->_mail->getBodyText())
00236 && ($html = $this->_mail->getBodyHtml()))
00237 {
00238
00239 $mime = new Zend_Mime(null);
00240 $boundaryLine = $mime->boundaryLine($this->EOL);
00241 $boundaryEnd = $mime->mimeEnd($this->EOL);
00242
00243 $text->disposition = false;
00244 $html->disposition = false;
00245
00246 $body = $boundaryLine
00247 . $text->getHeaders($this->EOL)
00248 . $this->EOL
00249 . $text->getContent($this->EOL)
00250 . $this->EOL
00251 . $boundaryLine
00252 . $html->getHeaders($this->EOL)
00253 . $this->EOL
00254 . $html->getContent($this->EOL)
00255 . $this->EOL
00256 . $boundaryEnd;
00257
00258 $mp = new Zend_Mime_Part($body);
00259 $mp->type = Zend_Mime::MULTIPART_ALTERNATIVE;
00260 $mp->boundary = $mime->boundary();
00261
00262 $this->_isMultipart = true;
00263
00264
00265 array_unshift($this->_parts, $mp);
00266
00267
00268 $this->_headers = $this->_mail->getHeaders();
00269 return;
00270 }
00271
00272
00273 if (false !== ($body = $this->_mail->getBodyHtml())) {
00274 array_unshift($this->_parts, $body);
00275 } elseif (false !== ($body = $this->_mail->getBodyText())) {
00276 array_unshift($this->_parts, $body);
00277 }
00278
00279 if (!$body) {
00283 require_once 'Zend/Mail/Transport/Exception.php';
00284 throw new Zend_Mail_Transport_Exception('No body specified');
00285 }
00286
00287
00288 $this->_headers = $this->_mail->getHeaders();
00289 $headers = $body->getHeadersArray($this->EOL);
00290 foreach ($headers as $header) {
00291
00292
00293 $this->_headers[$header[0]] = array($header[1]);
00294 }
00295 }
00296
00305 public function send(Zend_Mail $mail)
00306 {
00307 $this->_isMultipart = false;
00308 $this->_mail = $mail;
00309 $this->_parts = $mail->getParts();
00310 $mime = $mail->getMime();
00311
00312
00313 $this->_buildBody();
00314
00315
00316 $count = count($this->_parts);
00317 $boundary = null;
00318 if ($count < 1) {
00322 require_once 'Zend/Mail/Transport/Exception.php';
00323 throw new Zend_Mail_Transport_Exception('Empty mail cannot be sent');
00324 }
00325
00326 if ($count > 1) {
00327
00328 $mime = new Zend_Mime($this->_mail->getMimeBoundary());
00329 $boundary = $mime->boundary();
00330 } elseif ($this->_isMultipart) {
00331
00332 $boundary = $this->_parts[0]->boundary;
00333 }
00334
00335
00336 $this->recipients = implode(',', $mail->getRecipients());
00337 $this->_prepareHeaders($this->_getHeaders($boundary));
00338
00339
00340
00341
00342 $message = new Zend_Mime_Message();
00343 $message->setParts($this->_parts);
00344 $message->setMime($mime);
00345 $this->body = $message->generateMessage($this->EOL);
00346
00347
00348 $this->_sendMail();
00349 }
00350 }