]> src.bluestatic.org Git - isso.git/blob - Mail.php
Our test suite is now done with PHPUnit, although it's rather broken
[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 $from = $this->_fetchFirstLine($this->from);
214 $from = trim($input->unsanitize($from));
215 if (empty($from))
216 {
217 trigger_error('You need to specify a from email address');
218 }
219
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);
224
225 // sanitize the subject
226 $subject = $this->_fetchFirstLine($this->subject);
227 $subject = trim($unsanitize($subject));
228 if (empty($subject))
229 {
230 trigger_error('You need to specify a subject for the message');
231 }
232
233 // sanitize the body
234 $bodyText = BSFunctions::ConvertLineBreaks($this->bodyText, $this->delim);
235 $bodyText = trim($input->unsanitize($bodyText, true));
236 if (empty($bodyText))
237 {
238 trigger_error('You need to specify body text before sending the message');
239 }
240
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;
247
248 // see if we need to use mime/multipart
249 if ($sendhtml AND !empty($bodyhtml) == true)
250 {
251 $boundary = 'ISSO-MULTIPART-' . BSFunctions::Rand(10);
252 $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
253
254 $bodyHtml = BSFunctions::ConvertLineBreaks($this->bodyHtml, $this->delim);
255
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;
261
262 // add some space between the parts
263 $body .= $this->delim . $this->delim . $this->delim;
264
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--";
272 }
273 else
274 {
275 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
276 $body = $bodytext;
277 }
278 $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim;
279
280 $headers = trim($headers);
281
282 // attempt to send the mail!
283 if (mail($tostring, $subject, $body, $headers, "-f {$from}"))
284 {
285 BSRegister::Debug("email: sent to $address");
286 }
287 else
288 {
289 BSRegister::Debug("email: error sending to $address");
290 }
291 }
292
293 // ###################################################################
294 /**
295 * Fetches the first line of a string
296 *
297 * @param string A string
298 *
299 * @return string The first line of the string
300 */
301 private function _fetchFirstLine($string)
302 {
303 $string = BSFunctions::ConvertLineBreaks($string);
304 $broken = explode("\n", $string);
305 return $broken[0];
306 }
307
308 // ###################################################################
309 /**
310 * Encodes a header value (to name, fron name, subject, etc.) according
311 * to RFC 2047
312 *
313 * @param string The text to encode
314 *
315 * @return string Encoded text
316 */
317 function _encodeHeaderValue($text)
318 {
319 if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0)
320 {
321 return $text;
322 }
323
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);
328
329 return '=?' . $this->charset . '?q?' . $text . '?=';
330 }
331 }
332
333 /*=====================================================================*\
334 || ###################################################################
335 || # $HeadURL$
336 || # $Id$
337 || ###################################################################
338 \*=====================================================================*/
339 ?>