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