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