Positioning fixes so that it works in the most browsers possible
[isso.git] / mail.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
37 * @version $Revision$
38 * @package ISSO
39 *
40 */
41 class Mail
42 {
43 /**
44 * Framework registry object
45 * @var object
46 * @access private
47 */
48 var $registry = null;
49
50 /**
51 * The message recipient's email address in the form of array(email => name)
52 * @var array
53 * @access private
54 */
55 var $to = array();
56
57 /**
58 * The subject of the message
59 * @var string
60 * @access private
61 */
62 var $subject = '';
63
64 /**
65 * Body plain-text of the message
66 * @var string
67 * @access private
68 */
69 var $bodytext = '';
70
71 /**
72 * HTML multi-part body of the message
73 * @var string
74 * @access private
75 */
76 var $bodyhtml = '';
77
78 /**
79 * The message sender's email address
80 * @var string
81 * @access private
82 */
83 var $from = '';
84
85 /**
86 * The message sender's display name
87 * @var string
88 * @access private
89 */
90 var $fromname = '';
91
92 /**
93 * Additional message headers
94 * @var string
95 * @access private
96 */
97 var $headers = '';
98
99 /**
100 * Whether to send the message as HTML or plain-text
101 * @var bool
102 * @access private
103 */
104 var $sendhtml = false;
105
106 /**
107 * The new line delimiter used in the message
108 * @var string
109 * @access private
110 */
111 var $delim = "\n";
112
113 /**
114 * Character set used to send messages with
115 * @var string
116 * @access public
117 */
118 var $charset = 'utf-8'; // should we be using iso-8859-1 ?
119
120 /**
121 * Fields array that is used in this module
122 * @var array
123 * @access private
124 */
125 var $fields = array(
126 'subject' => array(REQ_YES, null, false),
127 'bodytext' => array(REQ_YES, null, false),
128 'bodyhtml' => array(REQ_NO, null, false),
129 'from' => array(REQ_YES, null, false),
130 'fromname' => array(REQ_NO, null, false),
131 'headers' => array(REQ_NO, null, false),
132 'sendhtml' => array(REQ_NO, null, false),
133 'delim' => array(REQ_YES, null, true),
134 'charset' => array(REQ_YES, null, true)
135 );
136
137 // ###################################################################
138 /**
139 * Constructor
140 */
141 function __construct(&$registry)
142 {
143 $this->registry =& $registry;
144 }
145
146 // ###################################################################
147 /**
148 * (PHP 4) Constructor
149 */
150 function Mail(&$registry)
151 {
152 $this->__construct($registry);
153 }
154
155 // ###################################################################
156 /**
157 * Sets an ISSO field
158 *
159 * @access public
160 *
161 * @param string Field name
162 * @param mixed Value of the field
163 */
164 function set($name, $value)
165 {
166 if (empty($value) AND $this->fields["$name"][0] == REQ_YES)
167 {
168 trigger_error('Field ' . $name . ' cannot be empty', E_USER_ERROR);
169 }
170
171 $this->registry->do_set($name, $value, 'mail');
172 }
173
174 // ###################################################################
175 /**
176 * Gets an ISSO field
177 *
178 * @access public
179 *
180 * @param string Field name
181 *
182 * @return mixed Value of the field
183 */
184 function get($fieldname)
185 {
186 return $this->registry->do_get($fieldname, 'mail');
187 }
188
189 // ###################################################################
190 /**
191 * Adds a to address
192 *
193 * @access public
194 *
195 * @param string Name to send to
196 * @param string Email address
197 */
198 function to_add($name, $address)
199 {
200 if (isset($this->to["$address"]) AND $name !== null)
201 {
202 return;
203 }
204
205 if ($this->registry->modules['functions']->is_valid_email($address))
206 {
207 $this->to["$address"] = $name;
208 }
209 }
210
211 // ###################################################################
212 /**
213 * Removes a to address by email; if FALSE, it will clear the array
214 *
215 * @access public
216 *
217 * @param string Email address to remove, or FALSE to clear array
218 */
219 function to_remove($address)
220 {
221 if ($address === false)
222 {
223 $this->to = array();
224 }
225 else
226 {
227 unset($this->to["$address"]);
228 }
229 }
230
231 // ###################################################################
232 /**
233 * Returns the list of "to" addresses
234 *
235 * @access public
236 *
237 * @return array List of to-addresses
238 */
239 function to_fetch()
240 {
241 return $this->to;
242 }
243
244 // ###################################################################
245 /**
246 * Sends an email to the specified address with the specified
247 * sender, subject, and body.
248 *
249 * @access public
250 *
251 * @return bool Status of the message
252 */
253 function send()
254 {
255 // check the required stuff
256 $this->registry->check_isso_fields(get_class($this));
257 if (sizeof($this->to) < 1)
258 {
259 trigger_error('You need at least one email address to send to', E_USER_ERROR);
260 return false;
261 }
262
263 // make sure we have a mailer
264 // #*# add support for SMTP
265 if (!@ini_get('sendmail_path'))
266 {
267 $this->registry->debug("email: no sendmail -> not sending");
268 return false;
269 }
270
271 // sort out the to addresses
272 $tolist = array();
273 foreach ($this->to AS $address => $name)
274 {
275 $address = $this->_fetch_first_line($address);
276 $address = trim($this->registry->unsanitize($address));
277 $name = $this->_fetch_first_line($name);
278 $name = trim($this->registry->unsanitize($name));
279
280 if ($name == null)
281 {
282 $tolist[] = $address;
283 }
284 else
285 {
286 $tolist[] = "\"$name\" <$address>";
287 }
288 }
289
290 // sanitize the from field
291 $this->from = $this->_fetch_first_line($this->from);
292 $this->from = trim($this->registry->unsanitize($this->from));
293
294 // sanitize the from name
295 $this->fromname = $this->_fetch_first_line($this->fromname);
296 $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname)));
297
298 // sanitize the subject
299 $this->subject = $this->_fetch_first_line($this->subject);
300 $this->subject = trim($this->registry->unsanitize($this->subject));
301
302 // sanitize the body
303 $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim);
304 $this->bodytext = trim($this->registry->unsanitize($this->bodytext, true));
305
306 // attach additional headers
307 $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers, $this->delim);
308 $this->headers .= "From: \"{$this->fromname}\" <{$this->from}>" . $this->delim;
309 $this->headers .= "Return-Path: {$this->from}" . $this->delim;
310 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
311 $this->headers .= "MIME-Version: 1.0" . $this->delim;
312
313 // see if we need to use mime/multipart
314 if ($this->sendhtml AND $this->fields['bodyhtml'][2] == true)
315 {
316 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
317 $this->headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
318
319 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
320
321 // first part of the message (plaintext)
322 $body = "--$boundary" . $this->delim;
323 $body .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
324 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim . $this->delim;
325 $body .= $this->bodytext . $this->delim;
326
327 // add some space between the parts
328 $body .= $this->delim . $this->delim . $this->delim;
329
330 // second part (html)
331 $body .= "--$boundary" . $this->delim;
332 $body .= "Content-Type: text/html; charset=\"" . $this->charset . "\"" . $this->delim;
333 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim;
334 $body .= "Content-Disposition: inline" . $this->delim . $this->delim;
335 $body .= $this->bodyhtml . $this->delim;
336 $body .= "--$boundary--";
337 }
338 else
339 {
340 $this->headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
341 $body = $this->bodytext;
342 }
343 $this->headers .= "Content-Transfer-Encoding: 8bit" . $this->delim;
344
345 $this->headers = trim($this->headers);
346
347 // attempt to send the mail!
348 foreach ($tolist AS $address)
349 {
350 if (mail($address, $this->subject, $body, $this->headers, "-f {$this->from}"))
351 {
352 $this->registry->debug("email: sent to $address");
353 }
354 else
355 {
356 $this->registry->debug("email: error sending to $address");
357 }
358 }
359 }
360
361 // ###################################################################
362 /**
363 * Fetches the first line of a string
364 *
365 * @access private
366 *
367 * @param string A string
368 *
369 * @return string The first line of the string
370 */
371 function _fetch_first_line($string)
372 {
373 $string = $this->registry->modules['functions']->convert_line_breaks($string);
374 $broken = explode("\n", $string);
375 return $broken[0];
376 }
377 }
378
379 /*=====================================================================*\
380 || ###################################################################
381 || # $HeadURL$
382 || # $Id$
383 || ###################################################################
384 \*=====================================================================*/
385 ?>