In mail.php, we don't want to double encode fields so in send() create local variable...
[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
24 * mail.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Mail Sender
31 *
32 * This framework is a wrapper for the PHP mail function that properly
33 * sends mail with full email headers.
34 *
35 * @author Blue Static
36 * @copyright Copyright ©2002 - [#]year[#], Blue Static
37 * @version $Revision$
38 * @package ISSO
39 *
40 */
41 class Mail
42 {
43 /**
44 * Framework registry object
45 * @var object
46 * @access private
47 */
48 var $registry = null;
49
50 /**
51 * The subject of the message
52 * @var string
53 * @access private
54 */
55 var $subject = '';
56
57 /**
58 * Body plain-text of the message
59 * @var string
60 * @access private
61 */
62 var $bodytext = '';
63
64 /**
65 * HTML multi-part body of the message
66 * @var string
67 * @access private
68 */
69 var $bodyhtml = '';
70
71 /**
72 * The message sender's email address
73 * @var string
74 * @access private
75 */
76 var $from = '';
77
78 /**
79 * The message sender's display name
80 * @var string
81 * @access private
82 */
83 var $fromname = '';
84
85 /**
86 * Additional message headers
87 * @var string
88 * @access private
89 */
90 var $headers = '';
91
92 /**
93 * Whether to send the message as HTML or plain-text
94 * @var bool
95 * @access private
96 */
97 var $sendhtml = false;
98
99 /**
100 * The new line delimiter used in the message
101 * @var string
102 * @access private
103 */
104 var $delim = "\n";
105
106 /**
107 * Character set used to send messages with
108 * @var string
109 * @access public
110 */
111 var $charset = 'utf-8'; // should we be using iso-8859-1 ?
112
113 // ###################################################################
114 /**
115 * Constructor
116 */
117 function __construct(&$registry)
118 {
119 $this->registry =& $registry;
120 }
121
122 // ###################################################################
123 /**
124 * (PHP 4) Constructor
125 */
126 function Mail(&$registry)
127 {
128 $this->__construct($registry);
129 }
130
131 // ###################################################################
132 /**
133 * Sets the subject
134 *
135 * @access public
136 *
137 * @param string Subject text
138 */
139 function setSubject($subject)
140 {
141 $this->subject = $subject;
142 }
143
144 // ###################################################################
145 /**
146 * Sets the body text (required)
147 *
148 * @access public
149 *
150 * @param string Body text
151 */
152 function setBodyText($body)
153 {
154 $this->bodytext = $body;
155 }
156
157 // ###################################################################
158 /**
159 * Sets the HTML body (optional)
160 *
161 * @access public
162 *
163 * @param string Body HTML
164 */
165 function setBodyHtml($body)
166 {
167 $this->bodyhtml = $body;
168 }
169
170 // ###################################################################
171 /**
172 * Sets the from address
173 *
174 * @access public
175 *
176 * @param string Sending email address
177 */
178 function setFromAddress($address)
179 {
180 $this->from = $address;
181 }
182
183 // ###################################################################
184 /**
185 * Sets the from display name
186 *
187 * @access public
188 *
189 * @param string From name
190 */
191 function setFromName($name)
192 {
193 $this->fromname = $name;
194 }
195
196 // ###################################################################
197 /**
198 * Sets any additional headers
199 *
200 * @access public
201 *
202 * @param string Additional headers separated by a \n
203 */
204 function setHeaders($headers)
205 {
206 $this->headers = $headers;
207 }
208
209 // ###################################################################
210 /**
211 * Sets the character set to send the email in
212 *
213 * @access public
214 *
215 * @param string Charset
216 */
217 function setCharset($charset)
218 {
219 $this->charset = $charset;
220 }
221
222 // ###################################################################
223 /**
224 * Sends an email to the specified address with the specified
225 * sender, subject, and body.
226 *
227 * @access public
228 *
229 * @param string Email address to send to
230 * @param string Name of the recipient
231 * @param bool Send an HTML multipart (if HTML body specified)?
232 *
233 * @return bool Status of the message
234 */
235 function send($address, $name = null, $sendhtml = false)
236 {
237 if (empty($address))
238 {
239 trigger_error('You need to specify an email address', E_USER_ERROR);
240 return false;
241 }
242
243 // make sure we have a mailer
244 // TODO - add support for SMTP
245 if (!@ini_get('sendmail_path'))
246 {
247 $this->registry->debug("email: no sendmail -> not sending");
248 return false;
249 }
250
251 // sort out the to addresses
252 $address = $this->_fetch_first_line($address);
253 $address = trim($this->registry->unsanitize($address));
254 $name = $this->_fetch_first_line($name);
255 $name = trim($this->registry->unsanitize($name));
256 $name = $this->_encodeHeaderValue($name);
257 $tostring = ($name == null ? $address : "\"$name\" <$address>");
258
259 // sanitize the from field
260 $from = $this->_fetch_first_line($this->from);
261 $from = trim($this->registry->unsanitize($from));
262
263 // sanitize the from name
264 $fromname = $this->_fetch_first_line($this->fromname);
265 $fromname = ($fromname == '' ? $from : trim($this->registry->unsanitize($fromname)));
266 $fromname = $this->_encodeHeaderValue($this->fromname);
267
268 // sanitize the subject
269 $subject = $this->_fetch_first_line($this->subject);
270 $subject = trim($this->registry->unsanitize($subject));
271 $subject = $this->_encodeHeaderValue($subject);
272
273 // sanitize the body
274 $bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim);
275 $bodytext = trim($this->registry->unsanitize($bodytext, true));
276
277 // attach additional headers
278 $headers = $this->registry->modules['functions']->convert_line_breaks($this->headers, $this->delim);
279 $headers .= ((!preg_match("#{$this->delim}$#", $headers) AND $headers != '') ? "\n" : '') . "From: \"{$fromname}\" <{$from}>" . $this->delim;
280 $headers .= "Return-Path: {$from}" . $this->delim;
281 $headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
282 $headers .= "MIME-Version: 1.0" . $this->delim;
283
284 // see if we need to use mime/multipart
285 if ($sendhtml AND !empty($bodyhtml) == true)
286 {
287 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
288 $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
289
290 $bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
291
292 // first part of the message (plaintext)
293 $body = "--$boundary" . $this->delim;
294 $body .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
295 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim . $this->delim;
296 $body .= $bodytext . $this->delim;
297
298 // add some space between the parts
299 $body .= $this->delim . $this->delim . $this->delim;
300
301 // second part (html)
302 $body .= "--$boundary" . $this->delim;
303 $body .= "Content-Type: text/html; charset=\"" . $this->charset . "\"" . $this->delim;
304 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim;
305 $body .= "Content-Disposition: inline" . $this->delim . $this->delim;
306 $body .= $bodyhtml . $this->delim;
307 $body .= "--$boundary--";
308 }
309 else
310 {
311 $headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
312 $body = $bodytext;
313 }
314 $headers .= "Content-Transfer-Encoding: 8bit" . $this->delim;
315
316 $headers = trim($headers);
317
318 // attempt to send the mail!
319 if (mail($tostring, $subject, $body, $headers, "-f {$from}"))
320 {
321 $this->registry->debug("email: sent to $address");
322 }
323 else
324 {
325 $this->registry->debug("email: error sending to $address");
326 }
327 }
328
329 // ###################################################################
330 /**
331 * Fetches the first line of a string
332 *
333 * @access private
334 *
335 * @param string A string
336 *
337 * @return string The first line of the string
338 */
339 function _fetch_first_line($string)
340 {
341 $string = $this->registry->modules['functions']->convert_line_breaks($string);
342 $broken = explode("\n", $string);
343 return $broken[0];
344 }
345
346 // ###################################################################
347 /**
348 * Encodes a header value (to name, fron name, subject, etc.) according
349 * to RFC 2047
350 *
351 * @param string The text to encode
352 *
353 * @return string Encoded text
354 */
355 function _encodeHeaderValue($text)
356 {
357 if (preg_match('#[^a-zA-Z0-9\+\-\*!/]#', $text) == 0)
358 {
359 return $text;
360 }
361
362 // perform this on non-ASCII characters; excluding _ and = because we want them to be encoded as they have
363 // different meanings in mail messages
364 $text = preg_replace('#([^a-zA-Z0-9\+\-\*!/])#e', '"=" . strtoupper(dechex(ord("\\1")))', $text);
365 $text = str_replace('=20', '_' , $text);
366
367 return '=?' . $this->charset . '?q?' . $text . '?=';
368 }
369 }
370
371 /*=====================================================================*\
372 || ###################################################################
373 || # $HeadURL$
374 || # $Id$
375 || ###################################################################
376 \*=====================================================================*/
377 ?>