]>
src.bluestatic.org Git - isso.git/blob - Mail.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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 2 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 (c)2002 - 2007, Blue Static
44 * The subject of the message
47 private $subject = '';
50 * Body plain-text of the message
53 private $bodyText = '';
56 * HTML multi-part body of the message
59 private $bodyHtml = '';
62 * The message sender's email address
68 * The message sender's display name
71 private $fromName = '';
74 * Additional message headers
77 private $headers = '';
80 * The new line delimiter used in the message
84 private $delim = "\n";
87 * Character set used to send messages with
91 private $charset = 'utf-8'; // should we be using iso-8859-1 ?
93 // ###################################################################
97 * @param string Subject text
99 public function setSubject($subject)
101 $this->subject
= $subject;
104 // ###################################################################
106 * Sets the body text (required)
108 * @param string Body text
110 public function setBodyText($body)
112 $this->bodyText
= $body;
115 // ###################################################################
117 * Sets the HTML body (optional)
119 * @param string Body HTML
121 public function setBodyHtml($body)
123 $this->bodyHtml
= $body;
126 // ###################################################################
128 * Sets the from address
130 * @param string Sending email address
132 public function setFromAddress($address)
134 $this->from
= $address;
137 // ###################################################################
139 * Sets the from display name
141 * @param string From name
143 public function setFromName($name)
145 $this->fromName
= $name;
148 // ###################################################################
150 * Sets any additional headers
152 * @param string Additional headers separated by a \n
154 public function setHeaders($headers)
156 $this->headers
= $headers;
159 // ###################################################################
161 * Sets the character set to send the email in
163 * @param string Charset
165 public function setCharset($charset)
167 $this->charset
= $charset;
170 // ###################################################################
172 * Sends an email to the specified address with the specified
173 * sender, subject, and body.
175 * @param string Email address to send to
176 * @param string Name of the recipient
177 * @param bool Send an HTML multipart (if HTML body specified)?
179 * @return bool Status of the message
181 public function send($address, $name = null, $sendHtml = false)
185 throw new Exception('You need to specify an email address');
188 // load the input sanitizer
189 $input = BSApp
::Registry()->getType('Input');
192 BSApp
::Debug('ISSO/Input not loaded, so manually doing so');
193 $input = BSApp
::LoadModule('Input');
196 // make sure we have a mailer
197 // TODO - add support for SMTP
198 if (!@ini_get('sendmail_path'))
200 BSApp
::Debug("email: no sendmail -> not sending");
204 // sort out the to addresses
205 $address = $this->_fetchFirstLine($address);
206 $address = trim($input->unsanitize($address));
207 $name = $this->_fetchFirstLine($name);
208 $name = trim($input->unsanitize($name));
209 $tostring = ($name == null ? $address : "\"$name\" <$address>");
211 // sanitize the from field
212 $from = $this->_fetchFirstLine($this->from
);
213 $from = trim($input->unsanitize($from));
216 throw new Exception('You need to specify a from email address');
219 // sanitize the from name
220 $fromname = $this->_fetchFirstLine($this->fromname
);
221 $fromname = ($fromname == '' ? $from : trim($this->registry
->unsanitize($fromname)));
222 $fromname = $this->_encodeHeaderValue($this->fromname
);
224 // sanitize the subject
225 $subject = $this->_fetchFirstLine($this->subject
);
226 $subject = trim($unsanitize($subject));
229 throw new Exception('You need to specify a subject for the message');
233 $bodyText = BSFunctions
::ConvertLineBreaks($this->bodyText
, $this->delim
);
234 $bodyText = trim($input->unsanitize($bodyText, true));
235 if (empty($bodyText))
237 throw new Exception('You need to specify body text before sending the message');
240 // attach additional headers
241 $headers = BSFunctions
::ConvertLineBreaks($this->headers
, $this->delim
);
242 $headers .= ((!preg_match("#{$this->delim}$#", $headers) AND $headers != '') ? "\n" : '') . "From: \"{$fromName}\" <{$from}>" . $this->delim
;
243 $headers .= "Return-Path: {$from}" . $this->delim
;
244 $headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
245 $headers .= "MIME
-Version
: 1.0" . $this->delim;
247 // see if we need to use mime/multipart
248 if ($sendhtml AND !empty($bodyhtml) == true)
250 $boundary = 'ISSO-MULTIPART-' . BSFunctions::Rand(10);
251 $headers .= "Content
-Type
: multipart
/alternative
; boundary
=\"$boundary\"" . $this->delim;
253 $bodyHtml = BSFunctions::ConvertLineBreaks($this->bodyHtml, $this->delim);
255 // first part of the message (plaintext)
256 $body = "--$boundary" . $this->delim
;
257 $body .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
258 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim
. $this->delim
;
259 $body .= $bodytext . $this->delim
;
261 // add some space between the parts
262 $body .= $this->delim
. $this->delim
. $this->delim
;
264 // second part (html)
265 $body .= "--$boundary" . $this->delim;
266 $body .= "Content
-Type
: text
/html
; charset
=\"" . $this->charset . "\"
" . $this->delim;
267 $body .= "Content
-Transfer
-Encoding
: 8bit
" . $this->delim;
268 $body .= "Content
-Disposition
: inline
" . $this->delim . $this->delim;
269 $body .= $bodyhtml . $this->delim;
270 $body .= "--$boundary--";
274 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
277 $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim
;
279 $headers = trim($headers);
281 // attempt to send the mail!
282 if (mail($tostring, $subject, $body, $headers, "-f {$from}"))
284 BSApp
::Debug("email: sent to $address");
288 BSApp::Debug("email
: error sending to
$address");
292 // ###################################################################
294 * Fetches the first line of a string
296 * @param string A string
298 * @return string The first line of the string
300 private function _fetchFirstLine($string)
302 $string = BSFunctions
::ConvertLineBreaks($string);
303 $broken = explode("\n", $string);
307 // ###################################################################
309 * Encodes a header value (to name, fron name, subject, etc.) according
312 * @param string The text to encode
314 * @return string Encoded text
316 function _encodeHeaderValue($text)
318 if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0)
323 // perform this on non-ASCII characters; excluding _ and = because we want them to be encoded as they have
324 // different meanings in mail messages
325 $text = preg_replace('#([^a-zA-Z0-9\+\-\*!/])#e', '"=" . strtoupper(dechex(ord("\\1")))', $text);
326 $text = str_replace('=20', '_' , $text);
328 return '=?' . $this->charset
. '?q?' . $text . '?=';