- Starting to convert from the fields system to a get-set system
[isso.git] / mail.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
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 message recipient's email address in the form of array(email => name)
52 * @var array
53 * @access private
54 */
55 var $to = array();
56
57 /**
58 * The subject of the message
59 * @var string
60 * @access private
61 */
62 var $subject = '';
63
64 /**
65 * Body plain-text of the message
66 * @var string
67 * @access private
68 */
69 var $bodytext = '';
70
71 /**
72 * HTML multi-part body of the message
73 * @var string
74 * @access private
75 */
76 var $bodyhtml = '';
77
78 /**
79 * The message sender's email address
80 * @var string
81 * @access private
82 */
83 var $from = '';
84
85 /**
86 * The message sender's display name
87 * @var string
88 * @access private
89 */
90 var $fromname = '';
91
92 /**
93 * Additional message headers
94 * @var string
95 * @access private
96 */
97 var $headers = '';
98
99 /**
100 * Whether to send the message as HTML or plain-text
101 * @var bool
102 * @access private
103 */
104 var $sendhtml = false;
105
106 /**
107 * The new line delimiter used in the message
108 * @var string
109 * @access private
110 */
111 var $delim = "\n";
112
113 /**
114 * Character set used to send messages with
115 * @var string
116 * @access public
117 */
118 var $charset = 'utf-8'; // should we be using iso-8859-1 ?
119
120 /**
121 * Fields array that is used in this module
122 * @var array
123 * @access private
124 */
125 var $fields = array(
126 'subject' => array(REQ_YES, null, false),
127 'bodytext' => array(REQ_YES, null, false),
128 'bodyhtml' => array(REQ_NO, null, false),
129 'from' => array(REQ_YES, null, false),
130 'fromname' => array(REQ_NO, null, false),
131 'headers' => array(REQ_NO, null, false),
132 'sendhtml' => array(REQ_NO, null, false),
133 'delim' => array(REQ_YES, null, true),
134 'charset' => array(REQ_YES, null, true)
135 );
136
137 // ###################################################################
138 /**
139 * Constructor
140 */
141 function __construct(&$registry)
142 {
143 $this->registry =& $registry;
144 }
145
146 // ###################################################################
147 /**
148 * (PHP 4) Constructor
149 */
150 function Mail(&$registry)
151 {
152 $this->__construct($registry);
153 }
154
155 // ###################################################################
156 /**
157 * Sets an ISSO field
158 *
159 * @access public
160 *
161 * @param string Field name
162 * @param mixed Value of the field
163 */
164 function set($name, $value)
165 {
166 if (empty($value) AND $this->fields["$name"][0] == REQ_YES)
167 {
168 trigger_error('Field ' . $name . ' cannot be empty', E_USER_ERROR);
169 }
170
171 $this->registry->do_set($name, $value, 'mail');
172 }
173
174 // ###################################################################
175 /**
176 * Gets an ISSO field
177 *
178 * @access public
179 *
180 * @param string Field name
181 *
182 * @return mixed Value of the field
183 */
184 function get($fieldname)
185 {
186 return $this->registry->do_get($fieldname, 'mail');
187 }
188
189 // ###################################################################
190 /**
191 * Adds a to address
192 *
193 * @access public
194 *
195 * @param string Name to send to
196 * @param string Email address
197 */
198 function to_add($name, $address)
199 {
200 if (isset($this->to["$address"]) AND $name !== null)
201 {
202 return;
203 }
204
205 if ($this->registry->modules['functions']->is_valid_email($address))
206 {
207 $this->to["$address"] = $name;
208 }
209 }
210
211 // ###################################################################
212 /**
213 * Removes a to address by email; if FALSE, it will clear the array
214 *
215 * @access public
216 *
217 * @param string Email address to remove, or FALSE to clear array
218 */
219 function to_remove($address)
220 {
221 if ($address === false)
222 {
223 $this->to = array();
224 }
225 else
226 {
227 unset($this->to["$address"]);
228 }
229 }
230
231 // ###################################################################
232 /**
233 * Returns the list of "to" addresses
234 *
235 * @access public
236 *
237 * @return array List of to-addresses
238 */
239 function to_fetch()
240 {
241 return $this->to;
242 }
243
244 // ###################################################################
245 /**
246 * Sends an email to the specified address with the specified
247 * sender, subject, and body.
248 *
249 * @access public
250 *
251 * @param bool Clear the "to" list after sending?
252 *
253 * @return bool Status of the message
254 */
255 function send($cleartos = false)
256 {
257 // check the required stuff
258 $this->registry->check_isso_fields(get_class($this));
259 if (sizeof($this->to) < 1)
260 {
261 trigger_error('You need at least one email address to send to', E_USER_ERROR);
262 return false;
263 }
264
265 // make sure we have a mailer
266 // TODO - add support for SMTP
267 if (!@ini_get('sendmail_path'))
268 {
269 $this->registry->debug("email: no sendmail -> not sending");
270 return false;
271 }
272
273 // sort out the to addresses
274 $tolist = array();
275 foreach ($this->to AS $address => $name)
276 {
277 $address = $this->_fetch_first_line($address);
278 $address = trim($this->registry->unsanitize($address));
279 $name = $this->_fetch_first_line($name);
280 $name = trim($this->registry->unsanitize($name));
281
282 if ($name == null)
283 {
284 $tolist[] = $address;
285 }
286 else
287 {
288 $tolist[] = "\"$name\" <$address>";
289 }
290 }
291
292 // sanitize the from field
293 $this->from = $this->_fetch_first_line($this->from);
294 $this->from = trim($this->registry->unsanitize($this->from));
295
296 // sanitize the from name
297 $this->fromname = $this->_fetch_first_line($this->fromname);
298 $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname)));
299
300 // sanitize the subject
301 $this->subject = $this->_fetch_first_line($this->subject);
302 $this->subject = trim($this->registry->unsanitize($this->subject));
303
304 // sanitize the body
305 $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim);
306 $this->bodytext = trim($this->registry->unsanitize($this->bodytext, true));
307
308 // attach additional headers
309 $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers, $this->delim);
310 $this->headers .= ((!preg_match("#{$this->delim}$#", $this->headers) AND $this->headers != '') ? "\n" : '') . "From: \"{$this->fromname}\" <{$this->from}>" . $this->delim;
311 $this->headers .= "Return-Path: {$this->from}" . $this->delim;
312 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
313 $this->headers .= "MIME-Version: 1.0" . $this->delim;
314
315 // see if we need to use mime/multipart
316 if ($this->sendhtml AND $this->fields['bodyhtml'][2] == true)
317 {
318 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
319 $this->headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
320
321 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
322
323 // first part of the message (plaintext)
324 $body = "--$boundary" . $this->delim;
325 $body .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
326 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim . $this->delim;
327 $body .= $this->bodytext . $this->delim;
328
329 // add some space between the parts
330 $body .= $this->delim . $this->delim . $this->delim;
331
332 // second part (html)
333 $body .= "--$boundary" . $this->delim;
334 $body .= "Content-Type: text/html; charset=\"" . $this->charset . "\"" . $this->delim;
335 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim;
336 $body .= "Content-Disposition: inline" . $this->delim . $this->delim;
337 $body .= $this->bodyhtml . $this->delim;
338 $body .= "--$boundary--";
339 }
340 else
341 {
342 $this->headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
343 $body = $this->bodytext;
344 }
345 $this->headers .= "Content-Transfer-Encoding: 8bit" . $this->delim;
346
347 $this->headers = trim($this->headers);
348
349 // attempt to send the mail!
350 foreach ($tolist AS $address)
351 {
352 if (mail($address, $this->subject, $body, $this->headers, "-f {$this->from}"))
353 {
354 $this->registry->debug("email: sent to $address");
355 }
356 else
357 {
358 $this->registry->debug("email: error sending to $address");
359 }
360 }
361
362 if ($cleartos)
363 {
364 $this->to = array();
365 }
366 }
367
368 // ###################################################################
369 /**
370 * Fetches the first line of a string
371 *
372 * @access private
373 *
374 * @param string A string
375 *
376 * @return string The first line of the string
377 */
378 function _fetch_first_line($string)
379 {
380 $string = $this->registry->modules['functions']->convert_line_breaks($string);
381 $broken = explode("\n", $string);
382 return $broken[0];
383 }
384 }
385
386 /*=====================================================================*\
387 || ###################################################################
388 || # $HeadURL$
389 || # $Id$
390 || ###################################################################
391 \*=====================================================================*/
392 ?>