MailTransport.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Send mail using mail() function
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Network.Email
  16. * @since CakePHP(tm) v 2.0.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * Send mail using mail() function
  21. *
  22. * @package Cake.Network.Email
  23. */
  24. class MailTransport extends AbstractTransport {
  25. /**
  26. * Send mail
  27. *
  28. * @param CakeEmail $email CakeEmail
  29. * @return array
  30. * @throws SocketException When mail cannot be sent.
  31. */
  32. public function send(CakeEmail $email) {
  33. $eol = PHP_EOL;
  34. if (isset($this->_config['eol'])) {
  35. $eol = $this->_config['eol'];
  36. }
  37. $headers = $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'));
  38. $to = $headers['To'];
  39. unset($headers['To']);
  40. $headers = $this->_headersToString($headers, $eol);
  41. $message = implode($eol, $email->message());
  42. $params = isset($this->_config['additionalParameters']) ? $this->_config['additionalParameters'] : null;
  43. $this->_mail($to, $email->subject(), $message, $headers, $params);
  44. return array('headers' => $headers, 'message' => $message);
  45. }
  46. /**
  47. * Wraps internal function mail() and throws exception instead of errors if anything goes wrong
  48. *
  49. * @param string $to email's recipient
  50. * @param string $subject email's subject
  51. * @param string $message email's body
  52. * @param string $headers email's custom headers
  53. * @param string $params additional params for sending email, will be ignored when in safe_mode
  54. * @throws SocketException if mail could not be sent
  55. * @return void
  56. */
  57. protected function _mail($to, $subject, $message, $headers, $params = null) {
  58. if (ini_get('safe_mode')) {
  59. //@codingStandardsIgnoreStart
  60. if (!@mail($to, $subject, $message, $headers)) {
  61. throw new SocketException(__d('cake_dev', 'Could not send email.'));
  62. }
  63. } elseif (!@mail($to, $subject, $message, $headers, $params)) {
  64. //@codingStandardsIgnoreEnd
  65. throw new SocketException(__d('cake_dev', 'Could not send email.'));
  66. }
  67. }
  68. }