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