]>
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 \*=====================================================================*/
23 * Mail Sender (mail.php)
28 require_once('ISSO/Functions.php');
33 * This framework is a wrapper for the PHP mail function that properly
34 * sends mail with full email headers.
37 * @copyright Copyright ©2002 - [#]year[#], Blue Static
45 * The subject of the message
48 private $subject = '';
51 * Body plain-text of the message
54 private $bodyText = '';
57 * HTML multi-part body of the message
60 private $bodyHtml = '';
63 * The message sender's email address
69 * The message sender's display name
72 private $fromName = '';
75 * Additional message headers
78 private $headers = '';
81 * The new line delimiter used in the message
85 private $delim = "\n";
88 * Character set used to send messages with
92 private $charset = 'utf-8'; // should we be using iso-8859-1 ?
94 // ###################################################################
98 * @param string Subject text
100 public function setSubject($subject)
102 $this->subject
= $subject;
105 // ###################################################################
107 * Sets the body text (required)
109 * @param string Body text
111 public function setBodyText($body)
113 $this->bodyText
= $body;
116 // ###################################################################
118 * Sets the HTML body (optional)
120 * @param string Body HTML
122 public function setBodyHtml($body)
124 $this->bodyHtml
= $body;
127 // ###################################################################
129 * Sets the from address
131 * @param string Sending email address
133 public function setFromAddress($address)
135 $this->from
= $address;
138 // ###################################################################
140 * Sets the from display name
142 * @param string From name
144 public function setFromName($name)
146 $this->fromName
= $name;
149 // ###################################################################
151 * Sets any additional headers
153 * @param string Additional headers separated by a \n
155 public function setHeaders($headers)
157 $this->headers
= $headers;
160 // ###################################################################
162 * Sets the character set to send the email in
164 * @param string Charset
166 public function setCharset($charset)
168 $this->charset
= $charset;
171 // ###################################################################
173 * Sends an email to the specified address with the specified
174 * sender, subject, and body.
176 * @param string Email address to send to
177 * @param string Name of the recipient
178 * @param bool Send an HTML multipart (if HTML body specified)?
180 * @return bool Status of the message
182 public function send($address, $name = null, $sendHtml = false)
186 throw new Exception('You need to specify an email address');
189 // load the input sanitizer
190 $input = BSApp
::GetType('Input');
193 BSApp
::Debug('ISSO/Input not loaded, so manually doing so');
194 $input = BSApp
::LoadModule('Input');
197 // make sure we have a mailer
198 // TODO - add support for SMTP
199 if (!@ini_get('sendmail_path'))
201 BSApp
::Debug("email: no sendmail -> not sending");
205 // sort out the to addresses
206 $address = $this->_fetchFirstLine($address);
207 $address = trim($input->unsanitize($address));
208 $name = $this->_fetchFirstLine($name);
209 $name = trim($input->unsanitize($name));
210 $tostring = ($name == null ? $address : "\"$name\" <$address>");
212 // sanitize the from field
213 $from = $this->_fetchFirstLine($this->from
);
214 $from = trim($input->unsanitize($from));
217 throw new Exception('You need to specify a from email address');
220 // sanitize the from name
221 $fromname = $this->_fetchFirstLine($this->fromname
);
222 $fromname = ($fromname == '' ? $from : trim($this->registry
->unsanitize($fromname)));
223 $fromname = $this->_encodeHeaderValue($this->fromname
);
225 // sanitize the subject
226 $subject = $this->_fetchFirstLine($this->subject
);
227 $subject = trim($unsanitize($subject));
230 throw new Exception('You need to specify a subject for the message');
234 $bodyText = BSFunctions
::ConvertLineBreaks($this->bodyText
, $this->delim
);
235 $bodyText = trim($input->unsanitize($bodyText, true));
236 if (empty($bodyText))
238 throw new Exception('You need to specify body text before sending the message');
241 // attach additional headers
242 $headers = BSFunctions
::ConvertLineBreaks($this->headers
, $this->delim
);
243 $headers .= ((!preg_match("#{$this->delim}$#", $headers) AND $headers != '') ? "\n" : '') . "From: \"{$fromName}\" <{$from}>" . $this->delim
;
244 $headers .= "Return-Path: {$from}" . $this->delim
;
245 $headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
246 $headers .= "MIME
-Version
: 1.0" . $this->delim;
248 // see if we need to use mime/multipart
249 if ($sendhtml AND !empty($bodyhtml) == true)
251 $boundary = 'ISSO-MULTIPART-' . BSFunctions::Rand(10);
252 $headers .= "Content
-Type
: multipart
/alternative
; boundary
=\"$boundary\"" . $this->delim;
254 $bodyHtml = BSFunctions::ConvertLineBreaks($this->bodyHtml, $this->delim);
256 // first part of the message (plaintext)
257 $body = "--$boundary" . $this->delim
;
258 $body .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
259 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim
. $this->delim
;
260 $body .= $bodytext . $this->delim
;
262 // add some space between the parts
263 $body .= $this->delim
. $this->delim
. $this->delim
;
265 // second part (html)
266 $body .= "--$boundary" . $this->delim;
267 $body .= "Content
-Type
: text
/html
; charset
=\"" . $this->charset . "\"
" . $this->delim;
268 $body .= "Content
-Transfer
-Encoding
: 8bit
" . $this->delim;
269 $body .= "Content
-Disposition
: inline
" . $this->delim . $this->delim;
270 $body .= $bodyhtml . $this->delim;
271 $body .= "--$boundary--";
275 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
278 $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim
;
280 $headers = trim($headers);
282 // attempt to send the mail!
283 if (mail($tostring, $subject, $body, $headers, "-f {$from}"))
285 BSApp
::Debug("email: sent to $address");
289 BSApp::Debug("email
: error sending to
$address");
293 // ###################################################################
295 * Fetches the first line of a string
297 * @param string A string
299 * @return string The first line of the string
301 private function _fetchFirstLine($string)
303 $string = BSFunctions
::ConvertLineBreaks($string);
304 $broken = explode("\n", $string);
308 // ###################################################################
310 * Encodes a header value (to name, fron name, subject, etc.) according
313 * @param string The text to encode
315 * @return string Encoded text
317 function _encodeHeaderValue($text)
319 if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0)
324 // perform this on non-ASCII characters; excluding _ and = because we want them to be encoded as they have
325 // different meanings in mail messages
326 $text = preg_replace('#([^a-zA-Z0-9\+\-\*!/])#e', '"=" . strtoupper(dechex(ord("\\1")))', $text);
327 $text = str_replace('=20', '_' , $text);
329 return '=?' . $this->charset
. '?q?' . $text . '?=';
333 /*=====================================================================*\
334 || ###################################################################
337 || ###################################################################
338 \*=====================================================================*/