PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Mail/Transport/Abstract.php

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