BSRegister::GetType() will be responsible for adding the BS prefix to class names...
[isso.git] / Mail.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Mail Sender (mail.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Functions.php');
29
30 /**
31 * Mail Sender
32 *
33 * This framework is a wrapper for the PHP mail function that properly
34 * sends mail with full email headers.
35 *
36 * @author Blue Static
37 * @copyright Copyright ©2002 - [#]year[#], Blue Static
38 * @version $Revision$
39 * @package ISSO
40 *
41 */
42 class BSMail
43 {
44 /**
45 * The subject of the message
46 * @var string
47 */
48 private $subject = '';
49
50 /**
51 * Body plain-text of the message
52 * @var string
53 */
54 private $bodyText = '';
55
56 /**
57 * HTML multi-part body of the message
58 * @var string
59 */
60 private $bodyHtml = '';
61
62 /**
63 * The message sender's email address
64 * @var string
65 */
66 private $from = '';
67
68 /**
69 * The message sender's display name
70 * @var string
71 */
72 private $fromName = '';
73
74 /**
75 * Additional message headers
76 * @var string
77 */
78 private $headers = '';
79
80 /**
81 * The new line delimiter used in the message
82 * @var string
83 * @access private
84 */
85 private $delim = "\n";
86
87 /**
88 * Character set used to send messages with
89 * @var string
90 * @access public
91 */
92 private $charset = 'utf-8'; // should we be using iso-8859-1 ?
93
94 // ###################################################################
95 /**
96 * Sets the subject
97 *
98 * @param string Subject text
99 */
100 public function setSubject($subject)
101 {
102 $this->subject = $subject;
103 }
104
105 // ###################################################################
106 /**
107 * Sets the body text (required)
108 *
109 * @param string Body text
110 */
111 public function setBodyText($body)
112 {
113 $this->bodyText = $body;
114 }
115
116 // ###################################################################
117 /**
118 * Sets the HTML body (optional)
119 *
120 * @param string Body HTML
121 */
122 public function setBodyHtml($body)
123 {
124 $this->bodyHtml = $body;
125 }
126
127 // ###################################################################
128 /**
129 * Sets the from address
130 *
131 * @param string Sending email address
132 */
133 public function setFromAddress($address)
134 {
135 $this->from = $address;
136 }
137
138 // ###################################################################
139 /**
140 * Sets the from display name
141 *
142 * @param string From name
143 */
144 public function setFromName($name)
145 {
146 $this->fromName = $name;
147 }
148
149 // ###################################################################
150 /**
151 * Sets any additional headers
152 *
153 * @param string Additional headers separated by a \n
154 */
155 public function setHeaders($headers)
156 {
157 $this->headers = $headers;
158 }
159
160 // ###################################################################
161 /**
162 * Sets the character set to send the email in
163 *
164 * @param string Charset
165 */
166 public function setCharset($charset)
167 {
168 $this->charset = $charset;
169 }
170
171 // ###################################################################
172 /**
173 * Sends an email to the specified address with the specified
174 * sender, subject, and body.
175 *
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)?
179 *
180 * @return bool Status of the message
181 */
182 public function send($address, $name = null, $sendHtml = false)
183 {
184 if (empty($address))
185 {
186 trigger_error('You need to specify an email address');
187 }
188
189 // load the input sanitizer
190 $input = BSRegister::GetType('Input');
191 if ($input == null)
192 {
193 BSRegister::Debug('ISSO/Input not loaded, so manually doing so');
194 $input = BSRegister::LoadModule('Input');
195 }
196
197 // make sure we have a mailer
198 // TODO - add support for SMTP
199 if (!@ini_get('sendmail_path'))
200 {
201 BSRegister::Debug("email: no sendmail -> not sending");
202 return false;
203 }
204
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>");
211
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))
216 {
217 trigger_error('You need to specify a from email address');
218 }
219
220 // sanitize the from name
221 $this->fromName = $this->_fetchFirstLine($this->fromName);
222 $this->fromName = ($this->fromName == '' ? $this->from : trim($input->unsanitize($this->fromName)));
223
224 // sanitize the subject
225 $this->subject = $this->_fetchFirstLine($this->subject);
226 $this->subject = trim($input->unsanitize($this->subject));
227 if (empty($this->subject))
228 {
229 trigger_error('You need to specify a subject for the message');
230 }
231
232 // sanitize the body
233 $this->bodyText = BSFunctions::ConvertLineBreaks($this->bodyText, $this->delim);
234 $this->bodyText = trim($input->unsanitize($this->bodyText, true));
235 if (empty($this->bodyText))
236 {
237 trigger_error('You need to specify body text before sending the message');
238 }
239
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;
246
247 // see if we need to use mime/multipart
248 if ($sendHtml AND !empty($this->bodyHtml) == true)
249 {
250 $boundary = 'ISSO-MULTIPART-' . BSFunctions::Rand(10);
251 $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
252
253 $this->bodyHtml = BSFunctions::ConvertLineBreaks($this->bodyHtml, $this->delim);
254
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;
260
261 // add some space between the parts
262 $body .= $this->delim . $this->delim . $this->delim;
263
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--";
271 }
272 else
273 {
274 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
275 $body = $this->bodyText;
276 }
277 $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim;
278
279 $headers = trim($headers);
280
281 // attempt to send the mail!
282 if (mail($tostring, $this->subject, $body, $headers, "-f {$this->from}"))
283 {
284 BSRegister::Debug("email: sent to $address");
285 }
286 else
287 {
288 BSRegister::Debug("email: error sending to $address");
289 }
290 }
291
292 // ###################################################################
293 /**
294 * Fetches the first line of a string
295 *
296 * @param string A string
297 *
298 * @return string The first line of the string
299 */
300 private function _fetchFirstLine($string)
301 {
302 $string = BSFunctions::ConvertLineBreaks($string);
303 $broken = explode("\n", $string);
304 return $broken[0];
305 }
306 }
307
308 /*=====================================================================*\
309 || ###################################################################
310 || # $HeadURL$
311 || # $Id$
312 || ###################################################################
313 \*=====================================================================*/
314 ?>