]>
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 trigger_error('You need to specify an email address');
189 // load the input sanitizer
190 $input = BSRegister
::GetType('Input');
193 BSRegister
::Debug('ISSO/Input not loaded, so manually doing so');
194 $input = BSRegister
::LoadModule('Input');
197 // make sure we have a mailer
198 // TODO - add support for SMTP
199 if (!@ini_get('sendmail_path'))
201 BSRegister
::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 $this->from
= $this->_fetchFirstLine($this->from
);
214 $this->from
= trim($input->unsanitize($this->from
));
215 if (empty($this->from
))
217 trigger_error('You need to specify a from email address');
220 // sanitize the from name
221 $this->fromName
= $this->_fetchFirstLine($this->fromName
);
222 $this->fromName
= ($this->fromName
== '' ? $this->from
: trim($input->unsanitize($this->fromName
)));
224 // sanitize the subject
225 $this->subject
= $this->_fetchFirstLine($this->subject
);
226 $this->subject
= trim($input->unsanitize($this->subject
));
227 if (empty($this->subject
))
229 trigger_error('You need to specify a subject for the message');
233 $this->bodyText
= BSFunctions
::ConvertLineBreaks($this->bodyText
, $this->delim
);
234 $this->bodyText
= trim($input->unsanitize($this->bodyText
, true));
235 if (empty($this->bodyText
))
237 trigger_error('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: \"{$this->fromName}\" <{$this->from}>" . $this->delim
;
243 $headers .= "Return-Path: {$this->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($this->bodyHtml) == true)
250 $boundary = 'ISSO-MULTIPART-' . BSFunctions::Rand(10);
251 $headers .= "Content
-Type
: multipart
/alternative
; boundary
=\"$boundary\"" . $this->delim;
253 $this->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 .= $this->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 .= $this->bodyHtml . $this->delim;
270 $body .= "--$boundary--";
274 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset
. "\"" . $this->delim
;
275 $body = $this->bodyText
;
277 $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim
;
279 $headers = trim($headers);
281 // attempt to send the mail!
282 if (mail($tostring, $this->subject
, $body, $headers, "-f {$this->from}"))
284 BSRegister
::Debug("email: sent to $address");
288 BSRegister::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);
308 /*=====================================================================*\
309 || ###################################################################
312 || ###################################################################
313 \*=====================================================================*/