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