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