Adding fields and set() methods
[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 if (!@ini_get('sendmail_path'))
150 {
151 $this->registry->debug("email: no sendmail -> not sending");
152 return false;
153 }
154
155 $this->to = $this->_fetch_first_line($this->to);
156 $this->from = $this->_fetch_first_line($this->from);
157
158 if (!$this->from)
159 {
160 $this->registry->debug("email: no from -> not sending");
161 return false;
162 }
163 else
164 {
165 $this->from = trim($this->registry->unsanitize($this->from));
166 }
167
168 if (!$this->fromname)
169 {
170 $this->fromname = $this->from;
171 }
172 else
173 {
174 $this->fromname = trim($this->registry->unsanitize($this->fromname));
175 }
176
177 if (!$this->to)
178 {
179 $this->registry->debug("email: no recipient -> not sending");
180 return false;
181 }
182 else
183 {
184 $this->to = trim($this->registry->unsanitize($this->to));
185 }
186
187 if (!$this->subject)
188 {
189 $this->registry->debug("email: no subject -> not sending");
190 return false;
191 }
192 else
193 {
194 $this->subject = trim($this->registry->unsanitize($this->_fetch_first_line($this->subject)));
195 }
196
197 if (!$this->body)
198 {
199 $this->registry->debug("email: no body -> not sending");
200 return false;
201 }
202 else
203 {
204 $this->body = $this->_convert_line_breaks($this->body);
205 $this->body = trim($this->registry->unsanitize($this->body, true));
206 }
207
208 $this->headers = $this->_convert_line_breaks($this->headers);
209 $this->headers .= "From: \"{$this->fromname}\" <{$this->from}>\n";
210 $this->headers .= "Return-Path: {$this->from}\n";
211 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$\n";
212 $this->headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
213 $this->headers .= "Content-Transfer-Encoding: 7bit\n";
214
215 if (mail($this->to, $this->subject, $this->body, trim($this->headers), "-f {$this->from}"))
216 {
217 $this->registry->debug("email: sent -> good");
218 return true;
219 }
220 else
221 {
222 $this->registry->debug("email: sent -> error");
223 return false;
224 }
225 }
226
227 // ###################################################################
228 /**
229 * Fetches the first line of a string
230 *
231 * @access private
232 *
233 * @param string A string
234 *
235 * @return string The first line of the string
236 */
237 function _fetch_first_line($string)
238 {
239 $string = $this->_convert_line_breaks($string);
240 $broken = explode("\n", $string);
241 return $broken[0];
242 }
243
244 // ###################################################################
245 /**
246 * Changes line breaks into one format
247 *
248 * @access private
249 *
250 * @param string Text
251 * @param string New line break (default is UNIX format)
252 *
253 * @return string Text with one type of line break
254 */
255 function _convert_line_breaks($text, $convert_to = "\n")
256 {
257 $text = trim($text);
258 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
259 $text = str_replace("\n", $convert_to, $text);
260 return $text;
261 }
262 }
263
264 /*=====================================================================*\
265 || ###################################################################
266 || # $HeadURL$
267 || # $Id$
268 || ###################################################################
269 \*=====================================================================*/
270 ?>