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 if (empty($value) AND $this->fields
["$name"][0] == REQ_YES
)
152 trigger_error('Field ' . $name . ' cannot be empty', E_USER_ERROR
);
155 $this->registry
->do_set($name, $value, 'mail');
158 // ###################################################################
164 * @param string Field name
166 * @return mixed Value of the field
168 function get($fieldname)
170 return $this->registry
->do_get($fieldname, 'mail');
173 // ###################################################################
179 * @param string Name to send to
180 * @param string Email address
182 function to_add($name, $address)
184 if (isset($this->to
["$address"]) AND $name !== null
)
189 if ($this->registry
->modules
['functions']->is_valid_email($address))
191 $this->to
["$address"] = $name;
195 // ###################################################################
197 * Removes a to address by email; if FALSE, it will clear the array
201 * @param string Email address to remove, or FALSE to clear array
203 function to_remove($address)
205 if ($address === false
)
211 unset($this->to
["$address"]);
215 // ###################################################################
217 * Returns the list of "to" addresses
221 * @return array List of to-addresses
228 // ###################################################################
230 * Sends an email to the specified address with the specified
231 * sender, subject, and body.
235 * @return bool Status of the message
239 // check the required stuff
240 $this->registry
->check_isso_fields(get_class($this));
241 if (sizeof($this->to
) < 1)
243 trigger_error('You need at least one email address to send to', E_USER_ERROR
);
247 // make sure we have a mailer
248 // #*# add support for SMTP
249 if (!@ini_get('sendmail_path'))
251 $this->registry
->debug("email: no sendmail -> not sending");
255 // sort out the to addresses
257 foreach ($this->to
AS $address => $name)
259 $address = $this->_fetch_first_line($address);
260 $address = trim($this->registry
->unsanitize($address));
261 $name = $this->_fetch_first_line($name);
262 $name = trim($this->registry
->unsanitize($name));
266 $tolist[] = $address;
270 $tolist[] = "\"$name\" <$address>";
274 // sanitize the from field
275 $this->from
= $this->_fetch_first_line($this->from
);
276 $this->from
= trim($this->registry
->unsanitize($this->from
));
278 // sanitize the from name
279 $this->fromname
= $this->_fetch_first_line($this->fromname
);
280 $this->fromname
= ($this->fromname
== '' ?
$this->from
: trim($this->registry
->unsanitize($this->fromname
)));
282 // sanitize the subject
283 $this->subject
= $this->_fetch_first_line($this->subject
);
284 $this->subject
= trim($this->registry
->unsanitize($this->subject
));
289 $this->bodytext
= $this->registry
->modules
['functions']->convert_line_breaks($this->bodytext
, $delim);
290 $this->bodytext
= trim($this->registry
->unsanitize($this->bodytext
, true
));
292 // attach additional headers
293 $this->headers
= $this->registry
->modules
['functions']->convert_line_breaks($this->headers
, $delim);
294 $this->headers
.= "From: \"{$this->fromname}\" <{$this->from}>" . $delim;
295 $this->headers
.= "Return-Path: {$this->from}" . $delim;
296 $this->headers
.= "X-Mailer: ISSO Mail Framework \$Revision$" . $delim;
297 $this->headers
.= "MIME-Version: 1.0" . $delim;
299 // see if we need to use mime/multipart
300 if ($this->sendhtml
AND $this->fields
['bodyhtml'][2] == true
)
302 $boundary = 'ISSO-MULTIPART-' . $this->registry
->modules
['functions']->rand(10);
303 $this->headers
.= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $delim;
305 $this->bodyhtml
= $this->registry
->modules
['functions']->convert_line_breaks($this->bodyhtml
, $delim);
307 // first part of the message (plaintext)
308 $body = "--$boundary" . $delim;
309 $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $delim;
310 $body .= "Content-Transfer-Encoding: 8bit" . $delim . $delim;
311 $body .= $this->bodytext
. $delim;
313 // add some space between the parts
314 $body .= $delim . $delim . $delim;
316 // second part (html)
317 $body .= "--$boundary" . $delim;
318 $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $delim;
319 $body .= "Content-Transfer-Encoding: 8bit" . $delim;
320 $body .= "Content-Disposition: inline" . $delim . $delim;
321 $body .= $this->bodyhtml
. $delim;
322 $body .= "--$boundary--";
326 $this->headers
.= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $delim;
327 $body = $this->bodytext
;
329 $this->headers
.= "Content-Transfer-Encoding: 8bit" . $delim;
331 $this->headers
= trim($this->headers
);
333 // attempt to send the mail!
334 foreach ($tolist AS $address)
336 if (mail($address, $this->subject
, $body, $this->headers
, "-f {$this->from}"))
338 $this->registry
->debug("email: sent to $address");
342 $this->registry
->debug("email: error sending to $address");
347 // ###################################################################
349 * Fetches the first line of a string
353 * @param string A string
355 * @return string The first line of the string
357 function _fetch_first_line($string)
359 $string = $this->registry
->modules
['functions']->convert_line_breaks($string);
360 $broken = explode("\n", $string);
365 /*=====================================================================*\
366 || ###################################################################
369 || ###################################################################
370 \*=====================================================================*/