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