PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Mail/Transport/Abstract.php

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