Give all the instance variables visibility tags
[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
52 * @var string
53 * @access public
54 */
55 var $to = '';
56
57 /**
58 * The subject of the message
59 * @var string
60 * @access public
61 */
62 var $subject = '';
63
64 /**
65 * Body of the message
66 * @var string
67 * @access public
68 */
69 var $body = '';
70
71 /**
72 * The message sender's email address
73 * @var string
74 * @access public
75 */
76 var $from = '';
77
78 /**
79 * The message sender's display name
80 * @var string
81 * @access public
82 */
83 var $fromname = '';
84
85 /**
86 * Additional message headers
87 * @var string
88 * @access public
89 */
90 var $headers = '';
91
92 // ###################################################################
93 /**
94 * Constructor
95 */
96 function __construct(&$registry)
97 {
98 $this->registry =& $registry;
99 }
100
101 // ###################################################################
102 /**
103 * (PHP 4) Constructor
104 */
105 function Mail(&$registry)
106 {
107 $this->__construct($registry);
108 }
109
110 // ###################################################################
111 /**
112 * Sends an email to the specified address with the specified
113 * sender, subject, and body.
114 *
115 * @access public
116 *
117 * @return bool Status of the message
118 */
119 function send()
120 {
121 if (!@ini_get('sendmail_path'))
122 {
123 $this->registry->debug("email: no sendmail -> not sending");
124 return false;
125 }
126
127 $this->to = $this->_fetch_first_line($this->to);
128 $this->from = $this->_fetch_first_line($this->from);
129
130 if (!$this->from)
131 {
132 $this->registry->debug("email: no from -> not sending");
133 return false;
134 }
135 else
136 {
137 $this->from = trim($this->registry->unsanitize($this->from));
138 }
139
140 if (!$this->fromname)
141 {
142 $this->fromname = $this->from;
143 }
144 else
145 {
146 $this->fromname = trim($this->registry->unsanitize($this->fromname));
147 }
148
149 if (!$this->to)
150 {
151 $this->registry->debug("email: no recipient -> not sending");
152 return false;
153 }
154 else
155 {
156 $this->to = trim($this->registry->unsanitize($this->to));
157 }
158
159 if (!$this->subject)
160 {
161 $this->registry->debug("email: no subject -> not sending");
162 return false;
163 }
164 else
165 {
166 $this->subject = trim($this->registry->unsanitize($this->_fetch_first_line($this->subject)));
167 }
168
169 if (!$this->body)
170 {
171 $this->registry->debug("email: no body -> not sending");
172 return false;
173 }
174 else
175 {
176 $this->body = $this->_convert_line_breaks($this->body);
177 $this->body = trim($this->registry->unsanitize($this->body, true));
178 }
179
180 $this->headers = $this->_convert_line_breaks($this->headers);
181 $this->headers .= "From: \"{$this->fromname}\" <{$this->from}>\n";
182 $this->headers .= "Return-Path: {$this->from}\n";
183 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$\n";
184 $this->headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
185 $this->headers .= "Content-Transfer-Encoding: 7bit\n";
186
187 if (mail($this->to, $this->subject, $this->body, trim($this->headers), "-f {$this->from}"))
188 {
189 $this->registry->debug("email: sent -> good");
190 return true;
191 }
192 else
193 {
194 $this->registry->debug("email: sent -> error");
195 return false;
196 }
197 }
198
199 // ###################################################################
200 /**
201 * Fetches the first line of a string
202 *
203 * @access private
204 *
205 * @param string A string
206 *
207 * @return string The first line of the string
208 */
209 function _fetch_first_line($string)
210 {
211 $string = $this->_convert_line_breaks($string);
212 $broken = explode("\n", $string);
213 return $broken[0];
214 }
215
216 // ###################################################################
217 /**
218 * Changes line breaks into one format
219 *
220 * @access private
221 *
222 * @param string Text
223 * @param string New line break (default is UNIX format)
224 *
225 * @return string Text with one type of line break
226 */
227 function _convert_line_breaks($text, $convert_to = "\n")
228 {
229 $text = trim($text);
230 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
231 $text = str_replace("\n", $convert_to, $text);
232 return $text;
233 }
234 }
235
236 /*=====================================================================*\
237 || ###################################################################
238 || # $HeadURL$
239 || # $Id$
240 || ###################################################################
241 \*=====================================================================*/
242 ?>