2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
32 * This framework is a wrapper for the PHP mail function that properly
33 * sends mail with full email headers.
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
44 * Framework registry object
51 * The message recipient's email address in the form of array(email => name)
58 * The subject of the message
65 * Body plain-text of the message
72 * HTML multi-part body of the message
79 * The message sender's email address
86 * The message sender's display name
93 * Additional message headers
100 * Whether to send the message as HTML or plain-text
104 var $sendhtml = false;
107 * Fields array that is used in this module
112 'subject' => array(REQ_YES
, null, false),
113 'bodytext' => array(REQ_YES
, null, false),
114 'bodyhtml' => array(REQ_NO
, null, false),
115 'from' => array(REQ_YES
, null, false),
116 'fromname' => array(REQ_NO
, null, false),
117 'headers' => array(REQ_NO
, null, false),
118 'sendhtml' => array(REQ_NO
, null, false)
121 // ###################################################################
125 function __construct(&$registry)
127 $this->registry
=& $registry;
130 // ###################################################################
132 * (PHP 4) Constructor
134 function Mail(&$registry)
136 $this->__construct($registry);
139 // ###################################################################
145 * @param string Field name
146 * @param mixed Value of the field
148 function set($name, $value)
150 $this->registry
->do_set($name, $value, 'mail');
153 // ###################################################################
159 * @param string Field name
161 * @return mixed Value of the field
163 function get($fieldname)
165 return $this->registry
->do_get($fieldname, 'mail');
168 // ###################################################################
174 * @param string Name to send to
175 * @param string Email address
177 function to_add($name, $address)
179 if (isset($this->to
["$address"]) AND $name !== null)
184 if ($this->registry->modules['functions']->is_valid_email($address))
186 $this->to["$address"] = $name;
190 // ###################################################################
192 * Removes a to address by email; if FALSE, it will clear the array
196 * @param string Email address to remove, or FALSE to clear array
198 function to_remove($address)
200 if ($address === false)
206 unset($this->to
["$address"]);
210 // ###################################################################
212 * Returns the list of "to
" addresses
216 * @return array List of to-addresses
223 // ###################################################################
225 * Sends an email to the specified address with the specified
226 * sender, subject, and body.
230 * @return bool Status of the message
234 // check the required stuff
235 $this->registry->check_isso_fields(get_class($this));
236 if (sizeof($this->to) < 1)
238 trigger_error('You need at least one email address to send to', E_USER_ERROR);
242 // make sure we have a mailer
243 // #*# add support for SMTP
244 if (!@ini_get('sendmail_path'))
246 $this->registry->debug("email
: no sendmail
-> not sending
");
250 // sort out the to addresses
252 foreach ($this->to AS $address => $name)
254 $address = $this->_fetch_first_line($address);
255 $address = trim($this->registry->unsanitize($address));
256 $name = $this->_fetch_first_line($name);
257 $name = trim($this->registry->unsanitize($name));
261 $tolist[] = $address;
265 $tolist[] = "\"
$name\" <$address>";
269 // sanitize the from field
270 $this->from = $this->_fetch_first_line($this->from);
273 $this->registry->debug("email
: no from
-> not sending
");
278 $this->from = trim($this->registry->unsanitize($this->from));
281 // sanitize the from name
282 if (!$this->fromname)
284 $this->fromname = $this->from;
288 $this->fromname = trim($this->registry->unsanitize($this->fromname));
291 // sanitize the subject
294 $this->registry->debug("email
: no subject
-> not sending
");
299 $this->subject = trim($this->registry->unsanitize($this->_fetch_first_line($this->subject)));
305 if (!$this->bodytext)
307 $this->registry->debug("email
: no body
-> not sending
");
312 $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $delim);
313 $this->bodytext = trim($this->registry->unsanitize($this->bodytext, true));
316 // attach additional headers
317 $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers);
318 $this->headers .= "From
: \"{$this
->fromname
}\" <{$this
->from
}>" . $delim;
319 $this->headers .= "Return-Path
: {$this
->from
}" . $delim;
320 $this->headers .= "X
-Mailer
: ISSO Mail Framework \$Revision
$" . $delim;
321 $this->headers
.= "MIME-Version: 1.0" . $delim;
323 // see if we need to use mime/multipart
324 if ($this->sendhtml
AND $this->fields
['bodyhtml'][2] == true)
326 $boundary = 'ISSO-MULTIPART-' . $this->registry
->modules
['functions']->rand(10);
327 $this->headers
.= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $delim;
329 $this->bodyhtml
= $this->registry
->modules
['functions']->convert_line_breaks($this->bodyhtml
, $delim);
331 // first part of the message (plaintext)
332 $body = "--$boundary" . $delim;
333 $body .= "Content
-Type
: text
/plain
; charset
=\"iso
-8859-1\"" . $delim;
334 $body .= "Content
-Transfer
-Encoding
: 8bit
" . $delim . $delim;
335 $body .= $this->bodytext . $delim;
337 // add some space between the parts
338 $body .= $delim . $delim . $delim;
340 // second part (html)
341 $body .= "--$boundary" . $delim;
342 $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $delim;
343 $body .= "Content-Transfer-Encoding: 8bit" . $delim;
344 $body .= "Content-Disposition: inline" . $delim . $delim;
345 $body .= $this->bodyhtml
. $delim;
346 $body .= "--$boundary--";
350 $this->headers .= "Content
-Type
: text
/plain
; charset
=\"iso
-8859-1\"" . $delim;
351 $body = $this->bodytext;
353 $this->headers .= "Content
-Transfer
-Encoding
: 8bit
" . $delim;
355 $this->headers = trim($this->headers);
357 // attempt to send the mail!
358 foreach ($tolist AS $address)
360 if (mail($address, $this->subject, $body, $this->headers, "-f {$this
->from
}"))
362 $this->registry->debug("email
: sent
-> good
");
366 $this->registry->debug("email
: sent
-> error
");
371 // ###################################################################
373 * Fetches the first line of a string
377 * @param string A string
379 * @return string The first line of the string
381 function _fetch_first_line($string)
383 $string = $this->registry->modules['functions']->convert_line_breaks($string);
384 $broken = explode("\n
", $string);
389 /*=====================================================================*\
390 || ###################################################################
393 || ###################################################################
394 \*=====================================================================*/