We can now send multipart/html emails :)
[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 $this->registry->do_set($name, $value, 'mail');
151 }
152
153 // ###################################################################
154 /**
155 * Gets an ISSO field
156 *
157 * @access public
158 *
159 * @param string Field name
160 *
161 * @return mixed Value of the field
162 */
163 function get($fieldname)
164 {
165 return $this->registry->do_get($fieldname, 'mail');
166 }
167
168 // ###################################################################
169 /**
170 * Adds a to address
171 *
172 * @access public
173 *
174 * @param string Name to send to
175 * @param string Email address
176 */
177 function to_add($name, $address)
178 {
179 if (isset($this->to["$address"]) AND $name !== null)
180 {
181 return;
182 }
183
184 if ($this->registry->modules['functions']->is_valid_email($address))
185 {
186 $this->to["$address"] = $name;
187 }
188 }
189
190 // ###################################################################
191 /**
192 * Removes a to address by email; if FALSE, it will clear the array
193 *
194 * @access public
195 *
196 * @param string Email address to remove, or FALSE to clear array
197 */
198 function to_remove($address)
199 {
200 if ($address === false)
201 {
202 $this->to = array();
203 }
204 else
205 {
206 unset($this->to["$address"]);
207 }
208 }
209
210 // ###################################################################
211 /**
212 * Returns the list of "to" addresses
213 *
214 * @access public
215 *
216 * @return array List of to-addresses
217 */
218 function to_fetch()
219 {
220 return $this->to;
221 }
222
223 // ###################################################################
224 /**
225 * Sends an email to the specified address with the specified
226 * sender, subject, and body.
227 *
228 * @access public
229 *
230 * @return bool Status of the message
231 */
232 function send()
233 {
234 // check the required stuff
235 $this->registry->check_isso_fields(get_class($this));
236 if (sizeof($this->to) < 1)
237 {
238 trigger_error('You need at least one email address to send to', E_USER_ERROR);
239 return false;
240 }
241
242 // make sure we have a mailer
243 // #*# add support for SMTP
244 if (!@ini_get('sendmail_path'))
245 {
246 $this->registry->debug("email: no sendmail -> not sending");
247 return false;
248 }
249
250 // sort out the to addresses
251 $tolist = array();
252 foreach ($this->to AS $address => $name)
253 {
254 $address = $this->_fetch_first_line($address);
255 $address = trim($this->registry->unsanitize($address));
256 $name = $this->_fetch_first_line($name);
257 $name = trim($this->registry->unsanitize($name));
258
259 if ($name == null)
260 {
261 $tolist[] = $address;
262 }
263 else
264 {
265 $tolist[] = "\"$name\" <$address>";
266 }
267 }
268
269 // sanitize the from field
270 $this->from = $this->_fetch_first_line($this->from);
271 if (!$this->from)
272 {
273 $this->registry->debug("email: no from -> not sending");
274 return false;
275 }
276 else
277 {
278 $this->from = trim($this->registry->unsanitize($this->from));
279 }
280
281 // sanitize the from name
282 if (!$this->fromname)
283 {
284 $this->fromname = $this->from;
285 }
286 else
287 {
288 $this->fromname = trim($this->registry->unsanitize($this->fromname));
289 }
290
291 // sanitize the subject
292 if (!$this->subject)
293 {
294 $this->registry->debug("email: no subject -> not sending");
295 return false;
296 }
297 else
298 {
299 $this->subject = trim($this->registry->unsanitize($this->_fetch_first_line($this->subject)));
300 }
301
302 $delim = "\n";
303
304 // sanitize the body
305 if (!$this->bodytext)
306 {
307 $this->registry->debug("email: no body -> not sending");
308 return false;
309 }
310 else
311 {
312 $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $delim);
313 $this->bodytext = trim($this->registry->unsanitize($this->bodytext, true));
314 }
315
316 // attach additional headers
317 $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers);
318 $this->headers .= "From: \"{$this->fromname}\" <{$this->from}>" . $delim;
319 $this->headers .= "Return-Path: {$this->from}" . $delim;
320 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $delim;
321 $this->headers .= "MIME-Version: 1.0" . $delim;
322
323 // see if we need to use mime/multipart
324 if ($this->sendhtml AND $this->fields['bodyhtml'][2] == true)
325 {
326 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
327 $this->headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $delim;
328
329 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $delim);
330
331 // first part of the message (plaintext)
332 $body = "--$boundary" . $delim;
333 $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $delim;
334 $body .= "Content-Transfer-Encoding: 8bit" . $delim . $delim;
335 $body .= $this->bodytext . $delim;
336
337 // add some space between the parts
338 $body .= $delim . $delim . $delim;
339
340 // second part (html)
341 $body .= "--$boundary" . $delim;
342 $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $delim;
343 $body .= "Content-Transfer-Encoding: 8bit" . $delim;
344 $body .= "Content-Disposition: inline" . $delim . $delim;
345 $body .= $this->bodyhtml . $delim;
346 $body .= "--$boundary--";
347 }
348 else
349 {
350 $this->headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $delim;
351 $body = $this->bodytext;
352 }
353 $this->headers .= "Content-Transfer-Encoding: 8bit" . $delim;
354
355 $this->headers = trim($this->headers);
356
357 #echo $body; exit;
358
359 // attempt to send the mail!
360 foreach ($tolist AS $address)
361 {
362 if (mail($address, $this->subject, $body, $this->headers, "-f {$this->from}"))
363 {
364 $this->registry->debug("email: sent -> good");
365 }
366 else
367 {
368 $this->registry->debug("email: sent -> error");
369 }
370 }
371 }
372
373 // ###################################################################
374 /**
375 * Fetches the first line of a string
376 *
377 * @access private
378 *
379 * @param string A string
380 *
381 * @return string The first line of the string
382 */
383 function _fetch_first_line($string)
384 {
385 $string = $this->registry->modules['functions']->convert_line_breaks($string);
386 $broken = explode("\n", $string);
387 return $broken[0];
388 }
389 }
390
391 /*=====================================================================*\
392 || ###################################################################
393 || # $HeadURL$
394 || # $Id$
395 || ###################################################################
396 \*=====================================================================*/
397 ?>