fsf changes, meant to be rebased on upstream
[squirrelmail.git] / class / deliver / Deliver_SendMail.class.php
CommitLineData
e1ee60fe 1<?php
4b4abf93 2
5b8fd093 3/**
4 * Deliver_SendMail.class.php
5 *
5b8fd093 6 * Delivery backend for the Deliver class.
7 *
3aa9dc23 8 * @author Marc Groot Koerkamp
77a1e3d1 9 * @copyright 1999-2022 The SquirrelMail Project Team
4b4abf93 10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
2b646597 12 * @package squirrelmail
5b8fd093 13 */
e1ee60fe 14
fd7ab795 15/** @ignore */
16if (!defined('SM_PATH')) define('SM_PATH','../../');
e1ee60fe 17
e7be6ad7 18/** This of course depends upon Deliver */
19require_once(SM_PATH . 'class/deliver/Deliver.class.php');
91e0dccc 20
e7be6ad7 21/**
80f3e5c0 22 * Delivers messages using the sendmail binary
23 * @package squirrelmail
e7be6ad7 24 */
e1ee60fe 25class Deliver_SendMail extends Deliver {
fd7ab795 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 /**
3b4c56e0 50 * Constructor (PHP5 style, required in some future version of PHP)
fd7ab795 51 * @param array configuration options. array key = option name,
52 * array value = option value.
53 * @return void
54 * @since 1.5.1
55 */
3b4c56e0 56 function __construct($params=array()) {
fd7ab795 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 }
e1ee60fe 64
3b4c56e0 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
3aa9dc23 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
91e0dccc 81 * before each line is written to the stream.
82 *
3aa9dc23 83 * @param string $s Line to process
84 * @return void
85 * @access private
91e0dccc 86 */
11a01a02 87 function preWriteToStream(&$s) {
88 if ($s) {
91e0dccc 89 $s = str_replace("\r\n", "\n", $s);
11a01a02 90 }
91 }
3aa9dc23 92
93 /**
94 * function initStream
95 *
96 * Initialise the sendmail connection.
91e0dccc 97 *
3aa9dc23 98 * @param Message $message Message object containing the from address
99 * @param string $sendmail_path Location of sendmail binary
c09fd064 100 * @param mixed $ignore_x Eight extra arguments that the parent class
101 * requires which are not used here
fd7ab795 102 * @return resource
91e0dccc 103 * @access public
104 */
c09fd064 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()) {
11a01a02 106 $rfc822_header = $message->rfc822_header;
91e0dccc 107 $from = $rfc822_header->from[0];
108 $envelopefrom = trim($from->mailbox.'@'.$from->host);
fd7ab795 109 // save executed command for future reference
c6c3ccc4 110 $this->sendmail_command = escapeshellcmd("$sendmail_path $this->sendmail_args -f") . escapeshellarg($envelopefrom);
fd7ab795 111 // open process handle for writing
c6c3ccc4 112 $stream = popen($this->sendmail_command, "w");
91e0dccc 113 return $stream;
11a01a02 114 }
3aa9dc23 115
116 /**
fd7ab795 117 * Closes process handle.
91e0dccc 118 *
3aa9dc23 119 * @param resource $stream
120 * @return boolean
121 * @access public
91e0dccc 122 */
11a01a02 123 function finalizeStream($stream) {
fd7ab795 124 $ret = true;
125 $status = pclose($stream);
126 // check pclose() status.
127 if ($status!=0) {
128 $ret = false;
129 $this->dlv_msg=_("Email delivery error");
130 $this->dlv_ret_nr=$status;
131 // we can get better error messsage only if we switch to php 4.3+ and proc_open().
132 $this->dlv_server_msg=sprintf(_("Can't execute command '%s'."),$this->sendmail_command);
133 }
134 return $ret;
11a01a02 135 }
3aa9dc23 136
137 /**
138 * function getBcc
139 *
140 * In case of sendmail, the rfc822header must contain the bcc header.
91e0dccc 141 *
3aa9dc23 142 * @return boolean true if rfc822header should include the bcc header.
143 * @access private
144 */
93bbf72b 145 function getBcc() {
146 return true;
147 }
91e0dccc 148
88e471ad 149 /**
150 * function clean_crlf
151 *
152 * Cleans each line to only end in a LF
91e0dccc 153 * Returns the length of the line including a CR,
88e471ad 154 * so that length is correct when the message is saved to imap
91e0dccc 155 * Implemented to fix sendmail->postfix rejection of messages with
3aa9dc23 156 * attachments because of stray LF's
88e471ad 157 *
91e0dccc 158 * @param string $s string to strip of CR's
3aa9dc23 159 * @return integer length of string including a CR for each LF
91e0dccc 160 * @access private
88e471ad 161 */
162 function clean_crlf(&$s) {
163 $s = str_replace("\r\n", "\n", $s);
164 $s = str_replace("\r", "\n", $s);
165 $s2 = str_replace("\n", "\r\n", $s);
166 return strlen($s2);
167 }
91e0dccc 168
3aa9dc23 169
e1ee60fe 170}