Deliver_SendMail.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Deliver_SendMail.class.php
  4. *
  5. * Delivery backend for the Deliver class.
  6. *
  7. * @author Marc Groot Koerkamp
  8. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /** @ignore */
  14. if (!defined('SM_PATH')) define('SM_PATH','../../');
  15. /** This of course depends upon Deliver */
  16. require_once(SM_PATH . 'class/deliver/Deliver.class.php');
  17. /**
  18. * Delivers messages using the sendmail binary
  19. * @package squirrelmail
  20. */
  21. class Deliver_SendMail extends Deliver {
  22. /**
  23. * Extra sendmail arguments
  24. *
  25. * Parameter can be set in class constructor function.
  26. *
  27. * WARNING: Introduction of this parameter broke backwards compatibility
  28. * with workarounds specific to qmail-inject.
  29. *
  30. * If parameter needs some security modifications, it should be set to
  31. * private in PHP 5+ in order to prevent uncontrolled access.
  32. * @var string
  33. * @since 1.5.1
  34. */
  35. var $sendmail_args = '-i -t';
  36. /**
  37. * Stores used sendmail command
  38. * Private variable that is used to inform about used sendmail command.
  39. * @var string
  40. * @since 1.5.1
  41. */
  42. var $sendmail_command = '';
  43. /**
  44. * Constructor function
  45. * @param array configuration options. array key = option name,
  46. * array value = option value.
  47. * @return void
  48. * @since 1.5.1
  49. */
  50. function Deliver_SendMail($params=array()) {
  51. if (!empty($params) && is_array($params)) {
  52. // set extra sendmail arguments
  53. if (isset($params['sendmail_args'])) {
  54. $this->sendmail_args = $params['sendmail_args'];
  55. }
  56. }
  57. }
  58. /**
  59. * function preWriteToStream
  60. *
  61. * Sendmail needs LF's as line endings instead of CRLF.
  62. * This function translates the line endings to LF and should be called
  63. * before each line is written to the stream.
  64. *
  65. * @param string $s Line to process
  66. * @return void
  67. * @access private
  68. */
  69. function preWriteToStream(&$s) {
  70. if ($s) {
  71. $s = str_replace("\r\n", "\n", $s);
  72. }
  73. }
  74. /**
  75. * function initStream
  76. *
  77. * Initialise the sendmail connection.
  78. *
  79. * @param Message $message Message object containing the from address
  80. * @param string $sendmail_path Location of sendmail binary
  81. * @return resource
  82. * @access public
  83. */
  84. function initStream($message, $sendmail_path) {
  85. $rfc822_header = $message->rfc822_header;
  86. $from = $rfc822_header->from[0];
  87. $envelopefrom = trim($from->mailbox.'@'.$from->host);
  88. $envelopefrom = str_replace(array("\0","\n"),array('',''),$envelopefrom);
  89. // save executed command for future reference
  90. $this->sendmail_command = "$sendmail_path $this->sendmail_args -f$envelopefrom";
  91. // open process handle for writing
  92. $stream = popen (escapeshellcmd($this->sendmail_command), "w");
  93. return $stream;
  94. }
  95. /**
  96. * Closes process handle.
  97. *
  98. * @param resource $stream
  99. * @return boolean
  100. * @access public
  101. */
  102. function finalizeStream($stream) {
  103. $ret = true;
  104. $status = pclose($stream);
  105. // check pclose() status.
  106. if ($status!=0) {
  107. $ret = false;
  108. $this->dlv_msg=_("Email delivery error");
  109. $this->dlv_ret_nr=$status;
  110. // we can get better error messsage only if we switch to php 4.3+ and proc_open().
  111. $this->dlv_server_msg=sprintf(_("Can't execute command '%s'."),$this->sendmail_command);
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * function getBcc
  117. *
  118. * In case of sendmail, the rfc822header must contain the bcc header.
  119. *
  120. * @return boolean true if rfc822header should include the bcc header.
  121. * @access private
  122. */
  123. function getBcc() {
  124. return true;
  125. }
  126. /**
  127. * function clean_crlf
  128. *
  129. * Cleans each line to only end in a LF
  130. * Returns the length of the line including a CR,
  131. * so that length is correct when the message is saved to imap
  132. * Implemented to fix sendmail->postfix rejection of messages with
  133. * attachments because of stray LF's
  134. *
  135. * @param string $s string to strip of CR's
  136. * @return integer length of string including a CR for each LF
  137. * @access private
  138. */
  139. function clean_crlf(&$s) {
  140. $s = str_replace("\r\n", "\n", $s);
  141. $s = str_replace("\r", "\n", $s);
  142. $s2 = str_replace("\n", "\r\n", $s);
  143. return strlen($s2);
  144. }
  145. }