SendmailTransport.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * SendmailTransport for sending mail through a Sendmail/Postfix (etc..) binary.
  11. *
  12. * Supported modes are -bs and -t, with any additional flags desired.
  13. * It is advised to use -bs mode since error reporting with -t mode is not
  14. * possible.
  15. *
  16. * @author Chris Corbyn
  17. */
  18. class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTransport
  19. {
  20. /**
  21. * Connection buffer parameters.
  22. *
  23. * @var array
  24. */
  25. private $_params = array(
  26. 'timeout' => 30,
  27. 'blocking' => 1,
  28. 'command' => '/usr/sbin/sendmail -bs',
  29. 'type' => Swift_Transport_IoBuffer::TYPE_PROCESS
  30. );
  31. /**
  32. * Create a new SendmailTransport with $buf for I/O.
  33. *
  34. * @param Swift_Transport_IoBuffer $buf
  35. * @param Swift_Events_EventDispatcher $dispatcher
  36. */
  37. public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher)
  38. {
  39. parent::__construct($buf, $dispatcher);
  40. }
  41. /**
  42. * Start the standalone SMTP session if running in -bs mode.
  43. */
  44. public function start()
  45. {
  46. if (false !== strpos($this->getCommand(), ' -bs')) {
  47. parent::start();
  48. }
  49. }
  50. /**
  51. * Set the command to invoke.
  52. *
  53. * If using -t mode you are strongly advised to include -oi or -i in the flags.
  54. * For example: /usr/sbin/sendmail -oi -t
  55. * Swift will append a -f<sender> flag if one is not present.
  56. *
  57. * The recommended mode is "-bs" since it is interactive and failure notifications
  58. * are hence possible.
  59. *
  60. * @param string $command
  61. *
  62. * @return Swift_Transport_SendmailTransport
  63. */
  64. public function setCommand($command)
  65. {
  66. $this->_params['command'] = $command;
  67. return $this;
  68. }
  69. /**
  70. * Get the sendmail command which will be invoked.
  71. *
  72. * @return string
  73. */
  74. public function getCommand()
  75. {
  76. return $this->_params['command'];
  77. }
  78. /**
  79. * Send the given Message.
  80. *
  81. * Recipient/sender data will be retrieved from the Message API.
  82. *
  83. * The return value is the number of recipients who were accepted for delivery.
  84. * NOTE: If using 'sendmail -t' you will not be aware of any failures until
  85. * they bounce (i.e. send() will always return 100% success).
  86. *
  87. * @param Swift_Mime_Message $message
  88. * @param string[] $failedRecipients An array of failures by-reference
  89. *
  90. * @return int
  91. */
  92. public function send(Swift_Mime_Message $message, &$failedRecipients = null)
  93. {
  94. $failedRecipients = (array) $failedRecipients;
  95. $command = $this->getCommand();
  96. $buffer = $this->getBuffer();
  97. if (false !== strpos($command, ' -t')) {
  98. if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
  99. $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
  100. if ($evt->bubbleCancelled()) {
  101. return 0;
  102. }
  103. }
  104. if (false === strpos($command, ' -f')) {
  105. $command .= ' -f' . escapeshellarg($this->_getReversePath($message));
  106. }
  107. $buffer->initialize(array_merge($this->_params, array('command' => $command)));
  108. if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) {
  109. $buffer->setWriteTranslations(array("\r\n" => "\n", "\n." => "\n.."));
  110. } else {
  111. $buffer->setWriteTranslations(array("\r\n"=>"\n"));
  112. }
  113. $count = count((array) $message->getTo())
  114. + count((array) $message->getCc())
  115. + count((array) $message->getBcc())
  116. ;
  117. $message->toByteStream($buffer);
  118. $buffer->flushBuffers();
  119. $buffer->setWriteTranslations(array());
  120. $buffer->terminate();
  121. if ($evt) {
  122. $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
  123. $evt->setFailedRecipients($failedRecipients);
  124. $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
  125. }
  126. $message->generateId();
  127. } elseif (false !== strpos($command, ' -bs')) {
  128. $count = parent::send($message, $failedRecipients);
  129. } else {
  130. $this->_throwException(new Swift_TransportException(
  131. 'Unsupported sendmail command flags [' . $command . ']. ' .
  132. 'Must be one of "-bs" or "-t" but can include additional flags.'
  133. ));
  134. }
  135. return $count;
  136. }
  137. /** Get the params to initialize the buffer */
  138. protected function _getBufferParams()
  139. {
  140. return $this->_params;
  141. }
  142. }