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 |
47ccfad4 |
9 | * @copyright © 1999-2006 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 */ |
16 | if (!defined('SM_PATH')) define('SM_PATH','../../'); |
e1ee60fe |
17 | |
e7be6ad7 |
18 | /** This of course depends upon Deliver */ |
19 | require_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 |
25 | class 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 | /** |
50 | * Constructor function |
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 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']; |
61 | } |
62 | } |
63 | } |
e1ee60fe |
64 | |
3aa9dc23 |
65 | /** |
66 | * function preWriteToStream |
67 | * |
68 | * Sendmail needs LF's as line endings instead of CRLF. |
69 | * This function translates the line endings to LF and should be called |
91e0dccc |
70 | * before each line is written to the stream. |
71 | * |
3aa9dc23 |
72 | * @param string $s Line to process |
73 | * @return void |
74 | * @access private |
91e0dccc |
75 | */ |
11a01a02 |
76 | function preWriteToStream(&$s) { |
77 | if ($s) { |
91e0dccc |
78 | $s = str_replace("\r\n", "\n", $s); |
11a01a02 |
79 | } |
80 | } |
3aa9dc23 |
81 | |
82 | /** |
83 | * function initStream |
84 | * |
85 | * Initialise the sendmail connection. |
91e0dccc |
86 | * |
3aa9dc23 |
87 | * @param Message $message Message object containing the from address |
88 | * @param string $sendmail_path Location of sendmail binary |
fd7ab795 |
89 | * @return resource |
91e0dccc |
90 | * @access public |
91 | */ |
11a01a02 |
92 | function initStream($message, $sendmail_path) { |
93 | $rfc822_header = $message->rfc822_header; |
91e0dccc |
94 | $from = $rfc822_header->from[0]; |
95 | $envelopefrom = trim($from->mailbox.'@'.$from->host); |
3aa9dc23 |
96 | $envelopefrom = str_replace(array("\0","\n"),array('',''),$envelopefrom); |
fd7ab795 |
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"); |
91e0dccc |
101 | return $stream; |
11a01a02 |
102 | } |
3aa9dc23 |
103 | |
104 | /** |
fd7ab795 |
105 | * Closes process handle. |
91e0dccc |
106 | * |
3aa9dc23 |
107 | * @param resource $stream |
108 | * @return boolean |
109 | * @access public |
91e0dccc |
110 | */ |
11a01a02 |
111 | function finalizeStream($stream) { |
fd7ab795 |
112 | $ret = true; |
113 | $status = pclose($stream); |
114 | // check pclose() status. |
115 | if ($status!=0) { |
116 | $ret = false; |
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); |
121 | } |
122 | return $ret; |
11a01a02 |
123 | } |
3aa9dc23 |
124 | |
125 | /** |
126 | * function getBcc |
127 | * |
128 | * In case of sendmail, the rfc822header must contain the bcc header. |
91e0dccc |
129 | * |
3aa9dc23 |
130 | * @return boolean true if rfc822header should include the bcc header. |
131 | * @access private |
132 | */ |
93bbf72b |
133 | function getBcc() { |
134 | return true; |
135 | } |
91e0dccc |
136 | |
88e471ad |
137 | /** |
138 | * function clean_crlf |
139 | * |
140 | * Cleans each line to only end in a LF |
91e0dccc |
141 | * Returns the length of the line including a CR, |
88e471ad |
142 | * so that length is correct when the message is saved to imap |
91e0dccc |
143 | * Implemented to fix sendmail->postfix rejection of messages with |
3aa9dc23 |
144 | * attachments because of stray LF's |
88e471ad |
145 | * |
91e0dccc |
146 | * @param string $s string to strip of CR's |
3aa9dc23 |
147 | * @return integer length of string including a CR for each LF |
91e0dccc |
148 | * @access private |
88e471ad |
149 | */ |
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); |
154 | return strlen($s2); |
155 | } |
91e0dccc |
156 | |
3aa9dc23 |
157 | |
e1ee60fe |
158 | } |