Moving Mail::_convert_line_breaks() to Functions::convert_line_breaks()
[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 * Gets an ISSO field
141 *
142 * @access public
143 *
144 * @param string Field name
145 *
146 * @return mixed Value of the field
147 */
148 function get($fieldname)
149 {
150 return $this->registry->do_get($fieldname, 'mail');
151 }
152
153 // ###################################################################
154 /**
155 * Sends an email to the specified address with the specified
156 * sender, subject, and body.
157 *
158 * @access public
159 *
160 * @return bool Status of the message
161 */
162 function send()
163 {
164 $this->registry->check_isso_fields(get_class($this));
165
166 if (!@ini_get('sendmail_path'))
167 {
168 $this->registry->debug("email: no sendmail -> not sending");
169 return false;
170 }
171
172 $this->to = $this->_fetch_first_line($this->to);
173 $this->from = $this->_fetch_first_line($this->from);
174
175 if (!$this->from)
176 {
177 $this->registry->debug("email: no from -> not sending");
178 return false;
179 }
180 else
181 {
182 $this->from = trim($this->registry->unsanitize($this->from));
183 }
184
185 if (!$this->fromname)
186 {
187 $this->fromname = $this->from;
188 }
189 else
190 {
191 $this->fromname = trim($this->registry->unsanitize($this->fromname));
192 }
193
194 if (!$this->to)
195 {
196 $this->registry->debug("email: no recipient -> not sending");
197 return false;
198 }
199 else
200 {
201 $this->to = trim($this->registry->unsanitize($this->to));
202 }
203
204 if (!$this->subject)
205 {
206 $this->registry->debug("email: no subject -> not sending");
207 return false;
208 }
209 else
210 {
211 $this->subject = trim($this->registry->unsanitize($this->_fetch_first_line($this->subject)));
212 }
213
214 if (!$this->body)
215 {
216 $this->registry->debug("email: no body -> not sending");
217 return false;
218 }
219 else
220 {
221 $this->body = $this->registry->modules['functions']->convert_line_breaks($this->body);
222 $this->body = trim($this->registry->unsanitize($this->body, true));
223 }
224
225 $this->headers = $this->registry->modules['functions']->convert_line_breaks($this->headers);
226 $this->headers .= "From: \"{$this->fromname}\" <{$this->from}>\n";
227 $this->headers .= "Return-Path: {$this->from}\n";
228 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$\n";
229 $this->headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
230 $this->headers .= "Content-Transfer-Encoding: 7bit\n";
231
232 if (mail($this->to, $this->subject, $this->body, trim($this->headers), "-f {$this->from}"))
233 {
234 $this->registry->debug("email: sent -> good");
235 return true;
236 }
237 else
238 {
239 $this->registry->debug("email: sent -> error");
240 return false;
241 }
242 }
243
244 // ###################################################################
245 /**
246 * Fetches the first line of a string
247 *
248 * @access private
249 *
250 * @param string A string
251 *
252 * @return string The first line of the string
253 */
254 function _fetch_first_line($string)
255 {
256 $string = $this->registry->modules['functions']->convert_line_breaks($string);
257 $broken = explode("\n", $string);
258 return $broken[0];
259 }
260 }
261
262 /*=====================================================================*\
263 || ###################################################################
264 || # $HeadURL$
265 || # $Id$
266 || ###################################################################
267 \*=====================================================================*/
268 ?>