]>
src.bluestatic.org Git - isso.git/blob - mail.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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.
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
44 * Framework registry object
47 private $registry = null;
50 * The message recipient's email address in the form of array(email => name)
53 private $to = array();
56 * The subject of the message
59 private $subject = '';
62 * Body plain-text of the message
65 private $bodytext = '';
68 * HTML multi-part body of the message
71 private $bodyhtml = '';
74 * The message sender's email address
80 * The message sender's display name
83 private $fromname = '';
86 * Additional message headers
89 private $headers = '';
92 * Whether to send the message as HTML or plain-text
95 private $sendhtml = false;
98 * The new line delimiter used in the message
101 private $delim = "\n";
104 * Character set used to send messages with
107 public $charset = 'utf-8'; // should we be using iso-8859-1 ?
110 * Fields array that is used in this module
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)
125 // ###################################################################
129 function __construct(&$registry)
131 $this->registry
=& $registry;
134 // ###################################################################
140 * @param string Field name
141 * @param mixed Value of the field
143 function set($name, $value)
145 if (empty($value) AND $this->fields
["$name"][0] == REQ_YES)
147 trigger_error('Field ' . $name . ' cannot be empty', E_USER_ERROR);
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 * @param bool Clear the "to" list after sending?
232 * @return bool Status of the message
234 function send($cleartos = false)
236 // check the required stuff
237 $this->registry
->check_isso_fields(get_class($this));
238 if (sizeof($this->to
) < 1)
240 trigger_error('You need at least one email address to send to', E_USER_ERROR
);
244 // make sure we have a mailer
245 // #*# add support for SMTP
246 if (!@ini_get('sendmail_path'))
248 $this->registry
->debug("email: no sendmail -> not sending");
252 // sort out the to addresses
254 foreach ($this->to
AS $address => $name)
256 $address = $this->_fetch_first_line($address);
257 $address = trim($this->registry
->unsanitize($address));
258 $name = $this->_fetch_first_line($name);
259 $name = trim($this->registry
->unsanitize($name));
263 $tolist[] = $address;
267 $tolist[] = "\"$name\" <$address>";
271 // sanitize the from field
272 $this->from
= $this->_fetch_first_line($this->from
);
273 $this->from
= trim($this->registry
->unsanitize($this->from
));
275 // sanitize the from name
276 $this->fromname
= $this->_fetch_first_line($this->fromname
);
277 $this->fromname
= ($this->fromname
== '' ? $this->from
: trim($this->registry
->unsanitize($this->fromname
)));
279 // sanitize the subject
280 $this->subject
= $this->_fetch_first_line($this->subject
);
281 $this->subject
= trim($this->registry
->unsanitize($this->subject
));
284 $this->bodytext
= $this->registry
->modules
['functions']->convert_line_breaks($this->bodytext
, $this->delim
);
285 $this->bodytext
= trim($this->registry
->unsanitize($this->bodytext
, true));
287 // attach additional headers
288 $this->headers
= $this->registry
->modules
['functions']->convert_line_breaks($this->headers
, $this->delim
);
289 $this->headers
.= ((!preg_match("#{$this->delim}$#", $this->headers
) AND $this->headers
!= '') ? "\n" : '') . "From: \"{$this->fromname}\" <{$this->from}>" . $this->delim
;
290 $this->headers
.= "Return-Path: {$this->from}" . $this->delim
;
291 $this->headers
.= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
292 $this->headers .= "MIME
-Version
: 1.0" . $this->delim;
294 // see if we need to use mime/multipart
295 if ($this->sendhtml AND $this->fields['bodyhtml'][2] == true)
297 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
298 $this->headers .= "Content
-Type
: multipart
/alternative
; boundary
=\"$boundary\"" . $this->delim;
300 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
302 // first part of the message (plaintext)
303 $body = "--$boundary" . $this->delim
;
304 $body .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
305 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim
. $this->delim
;
306 $body .= $this->bodytext
. $this->delim
;
308 // add some space between the parts
309 $body .= $this->delim
. $this->delim
. $this->delim
;
311 // second part (html)
312 $body .= "--$boundary" . $this->delim;
313 $body .= "Content
-Type
: text
/html
; charset
=\"" . $this->charset . "\"
" . $this->delim;
314 $body .= "Content
-Transfer
-Encoding
: 8bit
" . $this->delim;
315 $body .= "Content
-Disposition
: inline
" . $this->delim . $this->delim;
316 $body .= $this->bodyhtml . $this->delim;
317 $body .= "--$boundary--";
321 $this->headers
.= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
322 $body = $this->bodytext
;
324 $this->headers
.= "Content-Transfer-Encoding: 8bit" . $this->delim
;
326 $this->headers
= trim($this->headers
);
328 // attempt to send the mail!
329 foreach ($tolist AS $address)
331 if (mail($address, $this->subject
, $body, $this->headers
, "-f {$this->from}"))
333 $this->registry
->debug("email: sent to $address");
337 $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 \*=====================================================================*/