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