Removing calls to check_isso_fields()
[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 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($email))
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 $tostring = ($name == null ? $address : "\"$name\" <$address>");
257
258 // sanitize the from field
259 $this->from = $this->_fetch_first_line($this->from);
260 $this->from = trim($this->registry->unsanitize($this->from));
261
262 // sanitize the from name
263 $this->fromname = $this->_fetch_first_line($this->fromname);
264 $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname)));
265
266 // sanitize the subject
267 $this->subject = $this->_fetch_first_line($this->subject);
268 $this->subject = trim($this->registry->unsanitize($this->subject));
269
270 // sanitize the body
271 $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim);
272 $this->bodytext = trim($this->registry->unsanitize($this->bodytext, true));
273
274 // attach additional headers
275 $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers, $this->delim);
276 $this->headers .= ((!preg_match("#{$this->delim}$#", $this->headers) AND $this->headers != '') ? "\n" : '') . "From: \"{$this->fromname}\" <{$this->from}>" . $this->delim;
277 $this->headers .= "Return-Path: {$this->from}" . $this->delim;
278 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$" . $this->delim;
279 $this->headers .= "MIME-Version: 1.0" . $this->delim;
280
281 // see if we need to use mime/multipart
282 if ($sendhtml AND !empty($this->bodyhtml) == true)
283 {
284 $boundary = 'ISSO-MULTIPART-' . $this->registry->modules['functions']->rand(10);
285 $this->headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"" . $this->delim;
286
287 $this->bodyhtml = $this->registry->modules['functions']->convert_line_breaks($this->bodyhtml, $this->delim);
288
289 // first part of the message (plaintext)
290 $body = "--$boundary" . $this->delim;
291 $body .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
292 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim . $this->delim;
293 $body .= $this->bodytext . $this->delim;
294
295 // add some space between the parts
296 $body .= $this->delim . $this->delim . $this->delim;
297
298 // second part (html)
299 $body .= "--$boundary" . $this->delim;
300 $body .= "Content-Type: text/html; charset=\"" . $this->charset . "\"" . $this->delim;
301 $body .= "Content-Transfer-Encoding: 8bit" . $this->delim;
302 $body .= "Content-Disposition: inline" . $this->delim . $this->delim;
303 $body .= $this->bodyhtml . $this->delim;
304 $body .= "--$boundary--";
305 }
306 else
307 {
308 $this->headers .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->delim;
309 $body = $this->bodytext;
310 }
311 $this->headers .= "Content-Transfer-Encoding: 8bit" . $this->delim;
312
313 $this->headers = trim($this->headers);
314
315 // attempt to send the mail!
316 if (mail($tostring, $this->subject, $body, $this->headers, "-f {$this->from}"))
317 {
318 $this->registry->debug("email: sent to $address");
319 }
320 else
321 {
322 $this->registry->debug("email: error sending to $address");
323 }
324 }
325
326 // ###################################################################
327 /**
328 * Fetches the first line of a string
329 *
330 * @access private
331 *
332 * @param string A string
333 *
334 * @return string The first line of the string
335 */
336 function _fetch_first_line($string)
337 {
338 $string = $this->registry->modules['functions']->convert_line_breaks($string);
339 $broken = explode("\n", $string);
340 return $broken[0];
341 }
342 }
343
344 /*=====================================================================*\
345 || ###################################################################
346 || # $HeadURL$
347 || # $Id$
348 || ###################################################################
349 \*=====================================================================*/
350 ?>