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