Adding check_isso_field() calls all over the place
[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 private
54 */
55 var $to = '';
56
57 /**
58 * The subject of the message
59 * @var string
60 * @access private
61 */
62 var $subject = '';
63
64 /**
65 * Body of the message
66 * @var string
67 * @access private
68 */
69 var $body = '';
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 * Fields array that is used in this module
94 * @var array
95 * @access private
96 */
97 var $fields = array(
98 'to' => array(REQ_YES, null, false),
99 'subject' => array(REQ_YES, null, false),
100 'body' => array(REQ_YES, null, false),
101 'from' => array(REQ_YES, null, false),
102 'fromname' => array(REQ_NO, null, false),
103 'headers' => array(REQ_NO, null, false)
104 );
105
106 // ###################################################################
107 /**
108 * Constructor
109 */
110 function __construct(&$registry)
111 {
112 $this->registry =& $registry;
113 }
114
115 // ###################################################################
116 /**
117 * (PHP 4) Constructor
118 */
119 function Mail(&$registry)
120 {
121 $this->__construct($registry);
122 }
123
124 // ###################################################################
125 /**
126 * Sets an ISSO field
127 *
128 * @access public
129 *
130 * @param string Field name
131 * @param mixed Value of the field
132 */
133 function set($name, $value)
134 {
135 $this->registry->do_set($name, $value, 'mail');
136 }
137
138 // ###################################################################
139 /**
140 * Sends an email to the specified address with the specified
141 * sender, subject, and body.
142 *
143 * @access public
144 *
145 * @return bool Status of the message
146 */
147 function send()
148 {
149 $this->registry->check_isso_fields(get_class($this));
150
151 if (!@ini_get('sendmail_path'))
152 {
153 $this->registry->debug("email: no sendmail -> not sending");
154 return false;
155 }
156
157 $this->to = $this->_fetch_first_line($this->to);
158 $this->from = $this->_fetch_first_line($this->from);
159
160 if (!$this->from)
161 {
162 $this->registry->debug("email: no from -> not sending");
163 return false;
164 }
165 else
166 {
167 $this->from = trim($this->registry->unsanitize($this->from));
168 }
169
170 if (!$this->fromname)
171 {
172 $this->fromname = $this->from;
173 }
174 else
175 {
176 $this->fromname = trim($this->registry->unsanitize($this->fromname));
177 }
178
179 if (!$this->to)
180 {
181 $this->registry->debug("email: no recipient -> not sending");
182 return false;
183 }
184 else
185 {
186 $this->to = trim($this->registry->unsanitize($this->to));
187 }
188
189 if (!$this->subject)
190 {
191 $this->registry->debug("email: no subject -> not sending");
192 return false;
193 }
194 else
195 {
196 $this->subject = trim($this->registry->unsanitize($this->_fetch_first_line($this->subject)));
197 }
198
199 if (!$this->body)
200 {
201 $this->registry->debug("email: no body -> not sending");
202 return false;
203 }
204 else
205 {
206 $this->body = $this->_convert_line_breaks($this->body);
207 $this->body = trim($this->registry->unsanitize($this->body, true));
208 }
209
210 $this->headers = $this->_convert_line_breaks($this->headers);
211 $this->headers .= "From: \"{$this->fromname}\" <{$this->from}>\n";
212 $this->headers .= "Return-Path: {$this->from}\n";
213 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$\n";
214 $this->headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
215 $this->headers .= "Content-Transfer-Encoding: 7bit\n";
216
217 if (mail($this->to, $this->subject, $this->body, trim($this->headers), "-f {$this->from}"))
218 {
219 $this->registry->debug("email: sent -> good");
220 return true;
221 }
222 else
223 {
224 $this->registry->debug("email: sent -> error");
225 return false;
226 }
227 }
228
229 // ###################################################################
230 /**
231 * Fetches the first line of a string
232 *
233 * @access private
234 *
235 * @param string A string
236 *
237 * @return string The first line of the string
238 */
239 function _fetch_first_line($string)
240 {
241 $string = $this->_convert_line_breaks($string);
242 $broken = explode("\n", $string);
243 return $broken[0];
244 }
245
246 // ###################################################################
247 /**
248 * Changes line breaks into one format
249 *
250 * @access private
251 *
252 * @param string Text
253 * @param string New line break (default is UNIX format)
254 *
255 * @return string Text with one type of line break
256 */
257 function _convert_line_breaks($text, $convert_to = "\n")
258 {
259 $text = trim($text);
260 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
261 $text = str_replace("\n", $convert_to, $text);
262 return $text;
263 }
264 }
265
266 /*=====================================================================*\
267 || ###################################################################
268 || # $HeadURL$
269 || # $Id$
270 || ###################################################################
271 \*=====================================================================*/
272 ?>