]>
src.bluestatic.org Git - isso.git/blob - Mail.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 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)2005 - 2008, 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 ?
96 public function __construct()
98 if (!BSApp
::$input instanceof BSInput
)
100 throw new Exception('BSApp::$input is not an instance of BSInput');
107 * @param string Subject text
109 public function setSubject($subject)
111 $this->subject
= $subject;
115 * Sets the body text (required)
117 * @param string Body text
119 public function setBodyText($body)
121 $this->bodyText
= $body;
125 * Sets the HTML body (optional)
127 * @param string Body HTML
129 public function setBodyHtml($body)
131 $this->bodyHtml
= $body;
135 * Sets the from address
137 * @param string Sending email address
139 public function setFromAddress($address)
141 $this->from
= $address;
145 * Sets the from display name
147 * @param string From name
149 public function setFromName($name)
151 $this->fromName
= $name;
155 * Sets any additional headers
157 * @param string Additional headers separated by a \n
159 public function setHeaders($headers)
161 $this->headers
= $headers;
165 * Sets the character set to send the email in
167 * @param string Charset
169 public function setCharset($charset)
171 $this->charset
= $charset;
175 * Sends an email to the specified address with the specified
176 * sender, subject, and body.
178 * @param string Email address to send to
179 * @param string Name of the recipient
180 * @param bool Send an HTML multipart (if HTML body specified)?
182 * @return bool Status of the message
184 public function send($address, $name = null, $sendHtml = false)
188 throw new Exception('You need to specify an email address');
191 // make sure we have a mailer
192 // TODO - add support for SMTP
193 if (!@ini_get('sendmail_path'))
195 BSApp
::debug("email: no sendmail -> not sending");
199 // sort out the to addresses
200 $address = $this->_fetchFirstLine($address);
201 $address = trim(BSApp
::$input->unsanitize($address));
202 $name = $this->_fetchFirstLine($name);
203 $name = trim(BSApp
::$input->unsanitize($name));
204 $tostring = ($name == null ? $address : "\"$name\" <$address>");
206 // sanitize the from field
207 $from = $this->_fetchFirstLine($this->from
);
208 $from = trim(BSApp
::$input->unsanitize($from));
211 throw new Exception('You need to specify a from email address');
214 // sanitize the from name
215 $fromName = $this->_fetchFirstLine($this->fromName
);
216 $fromName = ($fromName == '' ? $from : trim(BSApp
::$input->unsanitize($fromName)));
217 $fromName = $this->_encodeHeaderValue($this->fromName
);
219 // sanitize the subject
220 $subject = $this->_fetchFirstLine($this->subject
);
221 $subject = trim(BSApp
::$input->unsanitize($subject));
224 throw new Exception('You need to specify a subject for the message');
228 $bodyText = BSFunctions
::convert_line_breaks($this->bodyText
, $this->delim
);
229 $bodyText = trim(BSApp
::$input->unsanitize($bodyText, true));
230 if (empty($bodyText))
232 throw new Exception('You need to specify body text before sending the message');
235 // attach additional headers
236 $headers = BSFunctions
::convert_line_breaks($this->headers
, $this->delim
);
237 $headers .= ((!preg_match("#{$this->delim}$#", $headers) && $headers != '') ? "\n" : '') . "From: \"{$fromName}\" <{$from}>" . $this->delim
;
238 $headers .= "Return-Path: {$from}" . $this->delim
;
239 $headers .= "X-Mailer: ISSO Mail Framework" . $this->delim
;
240 $headers .= "MIME-Version: 1.0" . $this->delim
;
242 // see if we need to use mime/multipart
243 if ($sendhtml && !empty($bodyhtml) == true)
245 $boundary = 'ISSO-MULTIPART-' . BSFunctions
::Rand(10);
246 $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim
;
248 $bodyHtml = BSFunctions
::convert_line_breaks($this->bodyHtml
, $this->delim
);
250 // first part of the message (plaintext)
251 $body = "--$boundary" . $this->delim;
252 $body .= "Content
-Type
: text
/plain
; charset
=\"" . $this->charset . "\"
" . $this->delim;
253 $body .= "Content
-Transfer
-Encoding
: 8bit
" . $this->delim . $this->delim;
254 $body .= $bodyText . $this->delim;
256 // add some space between the parts
257 $body .= $this->delim . $this->delim . $this->delim;
259 // second part (html)
260 $body .= "--$boundary" . $this->delim
;
261 $body .= "Content-Type: text/html; charset=\"" . $this->charset
. "\"" . $this->delim
;
262 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim
;
263 $body .= "Content-Disposition: inline" . $this->delim
. $this->delim
;
264 $body .= $bodyHtml . $this->delim
;
265 $body .= "--$boundary--";
269 $headers .= "Content
-Type
: text
/plain
; charset
=\"" . $this->charset . "\"
" . $this->delim;
272 $headers .= "Content
-Transfer
-Encoding
: 8bit
" . $this->delim;
274 $headers = trim($headers);
276 // attempt to send the mail!
277 if (mail($tostring, $subject, $body, $headers, "-f {$from
}"))
279 BSApp::debug("email
: sent to
$address");
283 BSApp
::debug("email: error sending to $address");
288 * Fetches the first line of a string
290 * @param string A string
292 * @return string The first line of the string
294 private function _fetchFirstLine($string)
296 $string = BSFunctions::convert_line_breaks($string);
297 $broken = explode("\n
", $string);
302 * Encodes a header value (to name, fron name, subject, etc.) according
305 * @param string The text to encode
307 * @return string Encoded text
309 function _encodeHeaderValue($text)
311 if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0)
316 // perform this on non-ASCII characters; excluding _ and = because we want them to be encoded as they have
317 // different meanings in mail messages
318 $text = preg_replace('#([^a-zA-Z0-9\+\-\*!/])#e', '"=" . strtoupper(dechex(ord("\\
1")))', $text);
319 $text = str_replace('=20', '_' , $text);
321 return '=?' . $this->charset . '?q?' . $text . '?=';