MailTransport.php 2.2 KB

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