]>
src.bluestatic.org Git - isso.git/blob - mail.php
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 * The new line delimiter used in the message
114 * Character set used to send messages with
118 var $charset = 'utf-8'; // should we be using iso-8859-1 ?
121 * Fields array that is used in this module
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)
137 // ###################################################################
141 function __construct(&$registry)
143 $this->registry
=& $registry;
146 // ###################################################################
148 * (PHP 4) Constructor
150 function Mail(&$registry)
152 $this->__construct($registry);
155 // ###################################################################
161 * @param string Field name
162 * @param mixed Value of the field
164 function set($name, $value)
166 if (empty($value) AND $this->fields
["$name"][0] == REQ_YES)
168 trigger_error('Field ' . $name . ' cannot be empty', E_USER_ERROR);
171 $this->registry->do_set($name, $value, 'mail');
174 // ###################################################################
180 * @param string Field name
182 * @return mixed Value of the field
184 function get($fieldname)
186 return $this->registry->do_get($fieldname, 'mail');
189 // ###################################################################
195 * @param string Name to send to
196 * @param string Email address
198 function to_add($name, $address)
200 if (isset($this->to["$address"]) AND $name !== null)
205 if ($this->registry
->modules
['functions']->is_valid_email($address))
207 $this->to
["$address"] = $name;
211 // ###################################################################
213 * Removes a to address by email; if FALSE, it will clear the array
217 * @param string Email address to remove, or FALSE to clear array
219 function to_remove($address)
221 if ($address === false)
227 unset($this->to["$address"]);
231 // ###################################################################
233 * Returns the list of "to" addresses
237 * @return array List of to-addresses
244 // ###################################################################
246 * Sends an email to the specified address with the specified
247 * sender, subject, and body.
251 * @return bool Status of the message
255 // check the required stuff
256 $this->registry
->check_isso_fields(get_class($this));
257 if (sizeof($this->to
) < 1)
259 trigger_error('You need at least one email address to send to', E_USER_ERROR
);
263 // make sure we have a mailer
264 // #*# add support for SMTP
265 if (!@ini_get('sendmail_path'))
267 $this->registry
->debug("email: no sendmail -> not sending");
271 // sort out the to addresses
273 foreach ($this->to
AS $address => $name)
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));
282 $tolist[] = $address;
286 $tolist[] = "\"$name\" <$address>";
290 // sanitize the from field
291 $this->from
= $this->_fetch_first_line($this->from
);
292 $this->from
= trim($this->registry
->unsanitize($this->from
));
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
)));
298 // sanitize the subject
299 $this->subject
= $this->_fetch_first_line($this->subject
);
300 $this->subject
= trim($this->registry
->unsanitize($this->subject
));
303 $this->bodytext
= $this->registry
->modules
['functions']->convert_line_breaks($this->bodytext
, $this->delim
);
304 $this->bodytext
= trim($this->registry
->unsanitize($this->bodytext
, true));
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;
313 // see if we need to use mime/multipart
314 if ($this->sendhtml AND $this->fields['bodyhtml'][2] == true)
316 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
317 $this->headers .= "Content
-Type
: multipart
/alternative
; boundary
=\"$boundary\"" . $this->delim;
319 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
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
;
327 // add some space between the parts
328 $body .= $this->delim
. $this->delim
. $this->delim
;
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--";
340 $this->headers
.= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
341 $body = $this->bodytext
;
343 $this->headers
.= "Content-Transfer-Encoding: 8bit" . $this->delim
;
345 $this->headers
= trim($this->headers
);
347 // attempt to send the mail!
348 foreach ($tolist AS $address)
350 if (mail($address, $this->subject
, $body, $this->headers
, "-f {$this->from}"))
352 $this->registry
->debug("email: sent to $address");
356 $this->registry->debug("email
: error sending to
$address");
361 // ###################################################################
363 * Fetches the first line of a string
367 * @param string A string
369 * @return string The first line of the string
371 function _fetch_first_line($string)
373 $string = $this->registry
->modules
['functions']->convert_line_breaks($string);
374 $broken = explode("\n", $string);
379 /*=====================================================================*\
380 || ###################################################################
383 || ###################################################################
384 \*=====================================================================*/