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