]>
src.bluestatic.org Git - isso.git/blob - mail.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
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
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 $tostring = ($name == null ? $address : "\"$name\" <$address>");
258 // sanitize the from field
259 $this->from
= $this->_fetch_first_line($this->from
);
260 $this->from
= trim($this->registry
->unsanitize($this->from
));
262 // sanitize the from name
263 $this->fromname
= $this->_fetch_first_line($this->fromname
);
264 $this->fromname
= ($this->fromname
== '' ? $this->from
: trim($this->registry
->unsanitize($this->fromname
)));
266 // sanitize the subject
267 $this->subject
= $this->_fetch_first_line($this->subject
);
268 $this->subject
= trim($this->registry
->unsanitize($this->subject
));
271 $this->bodytext
= $this->registry
->modules
['functions']->convert_line_breaks($this->bodytext
, $this->delim
);
272 $this->bodytext
= trim($this->registry
->unsanitize($this->bodytext
, true));
274 // attach additional headers
275 $this->headers
= $this->registry
->modules
['functions']->convert_line_breaks($this->headers
, $this->delim
);
276 $this->headers
.= ((!preg_match("#{$this->delim}$#", $this->headers
) AND $this->headers
!= '') ? "\n" : '') . "From: \"{$this->fromname}\" <{$this->from}>" . $this->delim
;
277 $this->headers
.= "Return-Path: {$this->from}" . $this->delim
;
278 $this->headers
.= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
279 $this->headers .= "MIME
-Version
: 1.0" . $this->delim;
281 // see if we need to use mime/multipart
282 if ($sendhtml AND !empty($this->bodyhtml) == true)
284 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
285 $this->headers .= "Content
-Type
: multipart
/alternative
; boundary
=\"$boundary\"" . $this->delim;
287 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
289 // first part of the message (plaintext)
290 $body = "--$boundary" . $this->delim
;
291 $body .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
292 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim
. $this->delim
;
293 $body .= $this->bodytext
. $this->delim
;
295 // add some space between the parts
296 $body .= $this->delim
. $this->delim
. $this->delim
;
298 // second part (html)
299 $body .= "--$boundary" . $this->delim;
300 $body .= "Content
-Type
: text
/html
; charset
=\"" . $this->charset . "\"
" . $this->delim;
301 $body .= "Content
-Transfer
-Encoding
: 8bit
" . $this->delim;
302 $body .= "Content
-Disposition
: inline
" . $this->delim . $this->delim;
303 $body .= $this->bodyhtml . $this->delim;
304 $body .= "--$boundary--";
308 $this->headers
.= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
309 $body = $this->bodytext
;
311 $this->headers
.= "Content-Transfer-Encoding: 8bit" . $this->delim
;
313 $this->headers
= trim($this->headers
);
315 // attempt to send the mail!
316 if (mail($tostring, $this->subject
, $body, $this->headers
, "-f {$this->from}"))
318 $this->registry
->debug("email: sent to $address");
322 $this->registry->debug("email
: error sending to
$address");
326 // ###################################################################
328 * Fetches the first line of a string
332 * @param string A string
334 * @return string The first line of the string
336 function _fetch_first_line($string)
338 $string = $this->registry
->modules
['functions']->convert_line_breaks($string);
339 $broken = explode("\n", $string);
344 /*=====================================================================*\
345 || ###################################################################
348 || ###################################################################
349 \*=====================================================================*/