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