]> src.bluestatic.org Git - isso.git/blob - Mail.php
Fix a failing unit test to work around the file system being slow
[isso.git] / Mail.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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 2 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 (c)2002 - 2007, Blue Static
38 * @package ISSO
39 *
40 */
41 class BSMail
42 {
43 /**
44 * The subject of the message
45 * @var string
46 */
47 private $subject = '';
48
49 /**
50 * Body plain-text of the message
51 * @var string
52 */
53 private $bodyText = '';
54
55 /**
56 * HTML multi-part body of the message
57 * @var string
58 */
59 private $bodyHtml = '';
60
61 /**
62 * The message sender's email address
63 * @var string
64 */
65 private $from = '';
66
67 /**
68 * The message sender's display name
69 * @var string
70 */
71 private $fromName = '';
72
73 /**
74 * Additional message headers
75 * @var string
76 */
77 private $headers = '';
78
79 /**
80 * The new line delimiter used in the message
81 * @var string
82 * @access private
83 */
84 private $delim = "\n";
85
86 /**
87 * Character set used to send messages with
88 * @var string
89 * @access public
90 */
91 private $charset = 'utf-8'; // should we be using iso-8859-1 ?
92
93 // ###################################################################
94 /**
95 * Sets the subject
96 *
97 * @param string Subject text
98 */
99 public function setSubject($subject)
100 {
101 $this->subject = $subject;
102 }
103
104 // ###################################################################
105 /**
106 * Sets the body text (required)
107 *
108 * @param string Body text
109 */
110 public function setBodyText($body)
111 {
112 $this->bodyText = $body;
113 }
114
115 // ###################################################################
116 /**
117 * Sets the HTML body (optional)
118 *
119 * @param string Body HTML
120 */
121 public function setBodyHtml($body)
122 {
123 $this->bodyHtml = $body;
124 }
125
126 // ###################################################################
127 /**
128 * Sets the from address
129 *
130 * @param string Sending email address
131 */
132 public function setFromAddress($address)
133 {
134 $this->from = $address;
135 }
136
137 // ###################################################################
138 /**
139 * Sets the from display name
140 *
141 * @param string From name
142 */
143 public function setFromName($name)
144 {
145 $this->fromName = $name;
146 }
147
148 // ###################################################################
149 /**
150 * Sets any additional headers
151 *
152 * @param string Additional headers separated by a \n
153 */
154 public function setHeaders($headers)
155 {
156 $this->headers = $headers;
157 }
158
159 // ###################################################################
160 /**
161 * Sets the character set to send the email in
162 *
163 * @param string Charset
164 */
165 public function setCharset($charset)
166 {
167 $this->charset = $charset;
168 }
169
170 // ###################################################################
171 /**
172 * Sends an email to the specified address with the specified
173 * sender, subject, and body.
174 *
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)?
178 *
179 * @return bool Status of the message
180 */
181 public function send($address, $name = null, $sendHtml = false)
182 {
183 if (empty($address))
184 {
185 throw new Exception('You need to specify an email address');
186 }
187
188 // load the input sanitizer
189 $input = BSApp::Registry()->getType('Input');
190 if ($input == null)
191 {
192 BSApp::Debug('ISSO/Input not loaded, so manually doing so');
193 $input = BSApp::LoadModule('Input');
194 }
195
196 // make sure we have a mailer
197 // TODO - add support for SMTP
198 if (!@ini_get('sendmail_path'))
199 {
200 BSApp::Debug("email: no sendmail -> not sending");
201 return false;
202 }
203
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>");
210
211 // sanitize the from field
212 $from = $this->_fetchFirstLine($this->from);
213 $from = trim($input->unsanitize($from));
214 if (empty($from))
215 {
216 throw new Exception('You need to specify a from email address');
217 }
218
219 // sanitize the from name
220 $fromName = $this->_fetchFirstLine($this->fromName);
221 $fromName = ($fromName == '' ? $from : trim($input->unsanitize($fromName)));
222 $fromName = $this->_encodeHeaderValue($this->fromName);
223
224 // sanitize the subject
225 $subject = $this->_fetchFirstLine($this->subject);
226 $subject = trim($input->unsanitize($subject));
227 if (empty($subject))
228 {
229 throw new Exception('You need to specify a subject for the message');
230 }
231
232 // sanitize the body
233 $bodyText = BSFunctions::ConvertLineBreaks($this->bodyText, $this->delim);
234 $bodyText = trim($input->unsanitize($bodyText, true));
235 if (empty($bodyText))
236 {
237 throw new Exception('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: \"{$fromName}\" <{$from}>" . $this->delim;
243 $headers .= "Return-Path: {$from}" . $this->delim;
244 $headers .= "X-Mailer: ISSO Mail Framework" . $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($bodyhtml) == true)
249 {
250 $boundary = 'ISSO-MULTIPART-' . BSFunctions::Rand(10);
251 $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
252
253 $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 .= $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 .= $bodyHtml . $this->delim;
270 $body .= "--$boundary--";
271 }
272 else
273 {
274 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
275 $body = $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, $subject, $body, $headers, "-f {$from}"))
283 {
284 BSApp::Debug("email: sent to $address");
285 }
286 else
287 {
288 BSApp::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 * Encodes a header value (to name, fron name, subject, etc.) according
310 * to RFC 2047
311 *
312 * @param string The text to encode
313 *
314 * @return string Encoded text
315 */
316 function _encodeHeaderValue($text)
317 {
318 if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0)
319 {
320 return $text;
321 }
322
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);
327
328 return '=?' . $this->charset . '?q?' . $text . '?=';
329 }
330 }
331
332 ?>