]>
src.bluestatic.org Git - bugdar.git/blob - framework/mail.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)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 (c)2002 - [#]year[#], Blue Static
44 * Framework registry object
51 * The subject of the message
58 * Body plain-text of the message
65 * HTML multi-part body of the message
72 * The message sender's email address
79 * The message sender's display name
86 * Additional message headers
93 * Whether to send the message as HTML or plain-text
97 var $sendhtml = false;
100 * The new line delimiter used in the message
107 * Character set used to send messages with
111 var $charset = 'utf-8'; // should we be using iso-8859-1 ?
113 // ###################################################################
117 function __construct(&$registry)
119 $this->registry
=& $registry;
122 // ###################################################################
124 * (PHP 4) Constructor
126 function Mail(&$registry)
128 $this->__construct($registry);
131 // ###################################################################
137 * @param string Subject text
139 function setSubject($subject)
141 $this->subject
= $subject;
144 // ###################################################################
146 * Sets the body text (required)
150 * @param string Body text
152 function setBodyText($body)
154 $this->bodytext
= $body;
157 // ###################################################################
159 * Sets the HTML body (optional)
163 * @param string Body HTML
165 function setBodyHtml($body)
167 $this->bodyhtml
= $body;
170 // ###################################################################
172 * Sets the from address
176 * @param string Sending email address
178 function setFromAddress($address)
180 $this->from
= $address;
183 // ###################################################################
185 * Sets the from display name
189 * @param string From name
191 function setFromName($name)
193 $this->fromname
= $name;
196 // ###################################################################
198 * Sets any additional headers
202 * @param string Additional headers separated by a \n
204 function setHeaders($headers)
206 $this->headers
= $headers;
209 // ###################################################################
211 * Sets the character set to send the email in
215 * @param string Charset
217 function setCharset($charset)
219 $this->charset
= $charset;
222 // ###################################################################
224 * Sends an email to the specified address with the specified
225 * sender, subject, and body.
229 * @param string Email address to send to
230 * @param string Name of the recipient
231 * @param bool Send an HTML multipart (if HTML body specified)?
233 * @return bool Status of the message
235 function send($address, $name = null, $sendhtml = false)
239 trigger_error('You need to specify an email address', E_USER_ERROR
);
243 // make sure we have a mailer
244 // TODO - add support for SMTP
245 if (!@ini_get('sendmail_path'))
247 $this->registry
->debug("email: no sendmail -> not sending");
251 // sort out the to addresses
252 $address = $this->_fetch_first_line($address);
253 $address = trim($this->registry
->unsanitize($address));
254 $name = $this->_fetch_first_line($name);
255 $name = trim($this->registry
->unsanitize($name));
256 $name = $this->_encodeHeaderValue($name);
257 $tostring = ($name == null ? $address : "\"$name\" <$address>");
259 // sanitize the from field
260 $from = $this->_fetch_first_line($this->from
);
261 $from = trim($this->registry
->unsanitize($from));
263 // sanitize the from name
264 $fromname = $this->_fetch_first_line($this->fromname
);
265 $fromname = ($fromname == '' ? $from : trim($this->registry
->unsanitize($fromname)));
266 $fromname = $this->_encodeHeaderValue($this->fromname
);
268 // sanitize the subject
269 $subject = $this->_fetch_first_line($this->subject
);
270 $subject = trim($this->registry
->unsanitize($subject));
271 $subject = $this->_encodeHeaderValue($subject);
274 $bodytext = $this->registry
->modules
['functions']->convert_line_breaks($this->bodytext
, $this->delim
);
275 $bodytext = trim($this->registry
->unsanitize($bodytext, true));
277 // attach additional headers
278 $headers = $this->registry
->modules
['functions']->convert_line_breaks($this->headers
, $this->delim
);
279 $headers .= ((!preg_match("#{$this->delim}$#", $headers) AND $headers != '') ? "\n" : '') . "From: \"{$fromname}\" <{$from}>" . $this->delim
;
280 $headers .= "Return-Path: {$from}" . $this->delim
;
281 $headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
282 $headers .= "MIME
-Version
: 1.0" . $this->delim;
284 // see if we need to use mime/multipart
285 if ($sendhtml AND !empty($bodyhtml) == true)
287 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
288 $headers .= "Content
-Type
: multipart
/alternative
; boundary
=\"$boundary\"" . $this->delim;
290 $bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
292 // first part of the message (plaintext)
293 $body = "--$boundary" . $this->delim
;
294 $body .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
295 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim
. $this->delim
;
296 $body .= $bodytext . $this->delim
;
298 // add some space between the parts
299 $body .= $this->delim
. $this->delim
. $this->delim
;
301 // second part (html)
302 $body .= "--$boundary" . $this->delim;
303 $body .= "Content
-Type
: text
/html
; charset
=\"" . $this->charset . "\"
" . $this->delim;
304 $body .= "Content
-Transfer
-Encoding
: 8bit
" . $this->delim;
305 $body .= "Content
-Disposition
: inline
" . $this->delim . $this->delim;
306 $body .= $bodyhtml . $this->delim;
307 $body .= "--$boundary--";
311 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
314 $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim
;
316 $headers = trim($headers);
318 // attempt to send the mail!
319 if (mail($tostring, $subject, $body, $headers, "-f {$from}"))
321 $this->registry
->debug("email: sent to $address");
325 $this->registry->debug("email
: error sending to
$address");
329 // ###################################################################
331 * Fetches the first line of a string
335 * @param string A string
337 * @return string The first line of the string
339 function _fetch_first_line($string)
341 $string = $this->registry
->modules
['functions']->convert_line_breaks($string);
342 $broken = explode("\n", $string);
346 // ###################################################################
348 * Encodes a header value (to name, fron name, subject, etc.) according
351 * @param string The text to encode
353 * @return string Encoded text
355 function _encodeHeaderValue($text)
357 if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0)
362 // perform this on non-ASCII characters; excluding _ and = because we want them to be encoded as they have
363 // different meanings in mail messages
364 $text = preg_replace('#([^a-zA-Z0-9\+\-\*!/])#e', '"=" . strtoupper(dechex(ord("\\1")))', $text);
365 $text = str_replace('=20', '_' , $text);
367 return '=?' . $this->charset
. '?q?' . $text . '?=';