]> src.bluestatic.org Git - isso.git/blob - mail.php
Forgot to change this instance of a method name that was changed
[isso.git] / mail.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Mail Sender
24 * mail.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Mail Sender
31 *
32 * This framework is a wrapper for the PHP mail function that properly
33 * sends mail with full email headers.
34 *
35 * @author Blue Static
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
37 * @version $Revision$
38 * @package ISSO
39 *
40 */
41 class Mail
42 {
43 /**
44 * Framework registry object
45 * @var object
46 */
47 private $registry = null;
48
49 /**
50 * The message recipient's email address in the form of array(email => name)
51 * @var array
52 */
53 private $to = array();
54
55 /**
56 * The subject of the message
57 * @var string
58 */
59 private $subject = '';
60
61 /**
62 * Body plain-text of the message
63 * @var string
64 */
65 private $bodytext = '';
66
67 /**
68 * HTML multi-part body of the message
69 * @var string
70 */
71 private $bodyhtml = '';
72
73 /**
74 * The message sender's email address
75 * @var string
76 */
77 private $from = '';
78
79 /**
80 * The message sender's display name
81 * @var string
82 */
83 private $fromname = '';
84
85 /**
86 * Additional message headers
87 * @var string
88 */
89 private $headers = '';
90
91 /**
92 * Whether to send the message as HTML or plain-text
93 * @var bool
94 */
95 private $sendhtml = false;
96
97 /**
98 * The new line delimiter used in the message
99 * @var string
100 */
101 private $delim = "\n";
102
103 /**
104 * Character set used to send messages with
105 * @var string
106 */
107 public $charset = 'utf-8'; // should we be using iso-8859-1 ?
108
109 /**
110 * Fields array that is used in this module
111 * @var array
112 */
113 private $fields = array(
114 'subject' => array(REQ_YES, null, false),
115 'bodytext' => array(REQ_YES, null, false),
116 'bodyhtml' => array(REQ_NO, null, false),
117 'from' => array(REQ_YES, null, false),
118 'fromname' => array(REQ_NO, null, false),
119 'headers' => array(REQ_NO, null, false),
120 'sendhtml' => array(REQ_NO, null, false),
121 'delim' => array(REQ_YES, null, true),
122 'charset' => array(REQ_YES, null, true)
123 );
124
125 // ###################################################################
126 /**
127 * Constructor
128 */
129 public function __construct(&$registry)
130 {
131 $this->registry =& $registry;
132 }
133
134 // ###################################################################
135 /**
136 * Sets an ISSO field
137 *
138 * @param string Field name
139 * @param mixed Value of the field
140 */
141 public function set($name, $value)
142 {
143 if (empty($value) AND $this->fields["$name"][0] == REQ_YES)
144 {
145 trigger_error('Field ' . $name . ' cannot be empty', E_USER_ERROR);
146 }
147
148 $this->registry->do_set($name, $value, 'mail');
149 }
150
151 // ###################################################################
152 /**
153 * Gets an ISSO field
154 *
155 * @param string Field name
156 *
157 * @return mixed Value of the field
158 */
159 public function get($fieldname)
160 {
161 return $this->registry->do_get($fieldname, 'mail');
162 }
163
164 // ###################################################################
165 /**
166 * Adds a to address
167 *
168 * @param string Name to send to
169 * @param string Email address
170 */
171 public function to_add($name, $address)
172 {
173 if (isset($this->to["$address"]) AND $name !== null)
174 {
175 return;
176 }
177
178 if ($this->registry->modules['functions']->is_valid_email($address))
179 {
180 $this->to["$address"] = $name;
181 }
182 }
183
184 // ###################################################################
185 /**
186 * Removes a to address by email; if FALSE, it will clear the array
187 *
188 * @param string Email address to remove, or FALSE to clear array
189 */
190 public function to_remove($address)
191 {
192 if ($address === false)
193 {
194 $this->to = array();
195 }
196 else
197 {
198 unset($this->to["$address"]);
199 }
200 }
201
202 // ###################################################################
203 /**
204 * Returns the list of "to" addresses
205 *
206 * @return array List of to-addresses
207 */
208 public function to_fetch()
209 {
210 return $this->to;
211 }
212
213 // ###################################################################
214 /**
215 * Sends an email to the specified address with the specified
216 * sender, subject, and body.
217 *
218 * @param bool Clear the "to" list after sending?
219 *
220 * @return bool Status of the message
221 */
222 public function send($cleartos = false)
223 {
224 // check the required stuff
225 $this->registry->check_isso_fields(get_class($this));
226 if (sizeof($this->to) < 1)
227 {
228 trigger_error('You need at least one email address to send to', E_USER_ERROR);
229 return false;
230 }
231
232 // make sure we have a mailer
233 // #*# add support for SMTP
234 if (!@ini_get('sendmail_path'))
235 {
236 $this->registry->debug("email: no sendmail -> not sending");
237 return false;
238 }
239
240 // sort out the to addresses
241 $tolist = array();
242 foreach ($this->to AS $address => $name)
243 {
244 $address = $this->_fetch_first_line($address);
245 $address = trim($this->registry->unsanitize($address));
246 $name = $this->_fetch_first_line($name);
247 $name = trim($this->registry->unsanitize($name));
248
249 if ($name == null)
250 {
251 $tolist[] = $address;
252 }
253 else
254 {
255 $tolist[] = "\"$name\" <$address>";
256 }
257 }
258
259 // sanitize the from field
260 $this->from = $this->_fetch_first_line($this->from);
261 $this->from = trim($this->registry->unsanitize($this->from));
262
263 // sanitize the from name
264 $this->fromname = $this->_fetch_first_line($this->fromname);
265 $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname)));
266
267 // sanitize the subject
268 $this->subject = $this->_fetch_first_line($this->subject);
269 $this->subject = trim($this->registry->unsanitize($this->subject));
270
271 // sanitize the body
272 $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim);
273 $this->bodytext = trim($this->registry->unsanitize($this->bodytext, true));
274
275 // attach additional headers
276 $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers, $this->delim);
277 $this->headers .= ((!preg_match("#{$this->delim}$#", $this->headers) AND $this->headers != '') ? "\n" : '') . "From: \"{$this->fromname}\" <{$this->from}>" . $this->delim;
278 $this->headers .= "Return-Path: {$this->from}" . $this->delim;
279 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
280 $this->headers .= "MIME-Version: 1.0" . $this->delim;
281
282 // see if we need to use mime/multipart
283 if ($this->sendhtml AND $this->fields['bodyhtml'][2] == true)
284 {
285 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
286 $this->headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
287
288 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
289
290 // first part of the message (plaintext)
291 $body = "--$boundary" . $this->delim;
292 $body .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
293 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim . $this->delim;
294 $body .= $this->bodytext . $this->delim;
295
296 // add some space between the parts
297 $body .= $this->delim . $this->delim . $this->delim;
298
299 // second part (html)
300 $body .= "--$boundary" . $this->delim;
301 $body .= "Content-Type: text/html; charset=\"" . $this->charset . "\"" . $this->delim;
302 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim;
303 $body .= "Content-Disposition: inline" . $this->delim . $this->delim;
304 $body .= $this->bodyhtml . $this->delim;
305 $body .= "--$boundary--";
306 }
307 else
308 {
309 $this->headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
310 $body = $this->bodytext;
311 }
312 $this->headers .= "Content-Transfer-Encoding: 8bit" . $this->delim;
313
314 $this->headers = trim($this->headers);
315
316 // attempt to send the mail!
317 foreach ($tolist AS $address)
318 {
319 if (mail($address, $this->subject, $body, $this->headers, "-f {$this->from}"))
320 {
321 $this->registry->debug("email: sent to $address");
322 }
323 else
324 {
325 $this->registry->debug("email: error sending to $address");
326 }
327 }
328
329 if ($cleartos)
330 {
331 $this->to = array();
332 }
333 }
334
335 // ###################################################################
336 /**
337 * Fetches the first line of a string
338 *
339 * @param string A string
340 *
341 * @return string The first line of the string
342 */
343 private function _fetch_first_line($string)
344 {
345 $string = $this->registry->modules['functions']->convert_line_breaks($string);
346 $broken = explode("\n", $string);
347 return $broken[0];
348 }
349 }
350
351 /*=====================================================================*\
352 || ###################################################################
353 || # $HeadURL$
354 || # $Id$
355 || ###################################################################
356 \*=====================================================================*/
357 ?>