4 * Deliver_SendMail.class.php
6 * Delivery backend for the Deliver class.
8 * @author Marc Groot Koerkamp
9 * @copyright © 1999-2006 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
16 if (!defined('SM_PATH')) define('SM_PATH','../../');
18 /** This of course depends upon Deliver */
19 require_once(SM_PATH
. 'class/deliver/Deliver.class.php');
22 * Delivers messages using the sendmail binary
23 * @package squirrelmail
25 class Deliver_SendMail
extends Deliver
{
27 * Extra sendmail arguments
29 * Parameter can be set in class constructor function.
31 * WARNING: Introduction of this parameter broke backwards compatibility
32 * with workarounds specific to qmail-inject.
34 * If parameter needs some security modifications, it should be set to
35 * private in PHP 5+ in order to prevent uncontrolled access.
39 var $sendmail_args = '-i -t';
42 * Stores used sendmail command
43 * Private variable that is used to inform about used sendmail command.
47 var $sendmail_command = '';
50 * Constructor function
51 * @param array configuration options. array key = option name,
52 * array value = option value.
56 function Deliver_SendMail($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'];
66 * function preWriteToStream
68 * Sendmail needs LF's as line endings instead of CRLF.
69 * This function translates the line endings to LF and should be called
70 * before each line is written to the stream.
72 * @param string $s Line to process
76 function preWriteToStream(&$s) {
78 $s = str_replace("\r\n", "\n", $s);
85 * Initialise the sendmail connection.
87 * @param Message $message Message object containing the from address
88 * @param string $sendmail_path Location of sendmail binary
92 function initStream($message, $sendmail_path) {
93 $rfc822_header = $message->rfc822_header
;
94 $from = $rfc822_header->from
[0];
95 $envelopefrom = trim($from->mailbox
.'@'.$from->host
);
96 $envelopefrom = str_replace(array("\0","\n"),array('',''),$envelopefrom);
97 // save executed command for future reference
98 $this->sendmail_command
= "$sendmail_path $this->sendmail_args -f$envelopefrom";
99 // open process handle for writing
100 $stream = popen (escapeshellcmd($this->sendmail_command
), "w");
105 * Closes process handle.
107 * @param resource $stream
111 function finalizeStream($stream) {
113 $status = pclose($stream);
114 // check pclose() status.
117 $this->dlv_msg
=_("Email delivery error");
118 $this->dlv_ret_nr
=$status;
119 // we can get better error messsage only if we switch to php 4.3+ and proc_open().
120 $this->dlv_server_msg
=sprintf(_("Can't execute command '%s'."),$this->sendmail_command
);
128 * In case of sendmail, the rfc822header must contain the bcc header.
130 * @return boolean true if rfc822header should include the bcc header.
138 * function clean_crlf
140 * Cleans each line to only end in a LF
141 * Returns the length of the line including a CR,
142 * so that length is correct when the message is saved to imap
143 * Implemented to fix sendmail->postfix rejection of messages with
144 * attachments because of stray LF's
146 * @param string $s string to strip of CR's
147 * @return integer length of string including a CR for each LF
150 function clean_crlf(&$s) {
151 $s = str_replace("\r\n", "\n", $s);
152 $s = str_replace("\r", "\n", $s);
153 $s2 = str_replace("\n", "\r\n", $s);