Merging trunk back onto the template parsing branch
[isso.git] / mail.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Mail Sender
15 * mail.php
16 *
17 * @package ISSO
18 */
19
20 $OBJECT = 'Mail Sender';
21 $CLASS = 'Mail';
22 $OBJ = 'mail';
23
24 /**
25 * Mail Sender
26 *
27 * This framework is a wrapper for the PHP mail function that properly
28 * sends mail with full email headers.
29 *
30 * @author Iris Studios, Inc.
31 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
32 * @version $Revision$
33 * @package ISSO
34 *
35 */
36 class Mail
37 {
38 /**
39 * The message recipient's email address
40 * @var str
41 * @see send()
42 */
43 var $to = '';
44
45 /**
46 * The subject of the message
47 * @var str
48 * @see send()
49 */
50 var $subject = '';
51
52 /**
53 * Body of the message
54 * @var str
55 * @see send()
56 */
57 var $body = '';
58
59 /**
60 * The message sender's email address
61 * @var str
62 * @see send()
63 */
64 var $from = '';
65
66 /**
67 * The message sender's display name
68 * @var str
69 * @see send()
70 */
71 var $fromname = '';
72
73 /**
74 * Additional message headers
75 * @var str
76 */
77 var $headers = '';
78
79 /**
80 * Sends an email to the specified address with the specified
81 * sender, subject, and body.
82 *
83 * @return bool Status of the message
84 */
85 function send()
86 {
87 global $_isso;
88
89 if (!@ini_get('sendmail_path'))
90 {
91 $_isso->debug("email: no sendmail -> not sending");
92 return false;
93 }
94
95 $this->to = $this->_fetch_first_line($this->to);
96 $this->from = $this->_fetch_first_line($this->from);
97
98 if (!$this->from)
99 {
100 $_isso->debug("email: no from -> not sending");
101 return false;
102 }
103 else
104 {
105 $this->from = trim($_isso->unsanitize($this->from, true));
106 }
107
108 if (!$this->fromname)
109 {
110 $this->fromname = $this->from;
111 }
112 else
113 {
114 $this->fromname = trim($_isso->unsanitize($this->fromname, true));
115 }
116
117 if (!$this->to)
118 {
119 $_isso->debug("email: no recipient -> not sending");
120 return false;
121 }
122 else
123 {
124 $this->to = trim($_isso->unsanitize($this->to));
125 }
126
127 if (!$this->subject)
128 {
129 $_isso->debug("email: no subject -> not sending");
130 return false;
131 }
132 else
133 {
134 $this->subject = trim($_isso->unsanitize($this->_fetch_first_line($this->subject), true));
135 }
136
137 if (!$this->body)
138 {
139 $_isso->debug("email: no body -> not sending");
140 return false;
141 }
142 else
143 {
144 $this->body = $this->_convert_line_breaks($this->body);
145 $this->body = trim($_isso->unsanitize($this->body, true));
146 }
147
148 $this->headers = $this->_convert_line_breaks($this->headers);
149 $this->headers .= "From: \"{$this->fromname}\" <{$this->from}>\n";
150 $this->headers .= "Return-Path: {$this->from}\n";
151 $this->headers .= "X-Mailer: ISSO Mail Framework \$Revision$\n";
152 $this->headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
153 $this->headers .= "Content-Transfer-Encoding: 7bit\n";
154
155 if (mail($this->to, $this->subject, $this->body, trim($this->headers), "-f {$this->from}"))
156 {
157 $_isso->debug("email: sent -> good");
158 return true;
159 }
160 else
161 {
162 $_isso->debug("email: sent -> error");
163 return false;
164 }
165 }
166
167 /**
168 * Fetches the first line of a string
169 *
170 * @param str A string
171 *
172 * @return str The first line of the string
173 */
174 function _fetch_first_line($string)
175 {
176 $string = $this->_convert_line_breaks($string);
177 $broken = explode("\n", $string);
178 return $broken[0];
179 }
180
181 /**
182 * Changes line breaks into one format
183 *
184 * @param str Text
185 * @param str New line break (default is UNIX format)
186 *
187 * @return str Text with one type of line break
188 */
189 function _convert_line_breaks($text, $convert_to = "\n")
190 {
191 $text = trim($text);
192 $text = str_replace(array("\r\n", "\r", "\n"), "\n", $text);
193 $text = str_replace("\n", $convert_to, $text);
194 return $text;
195 }
196 }
197
198 /*=====================================================================*\
199 || ###################################################################
200 || # $HeadURL$
201 || # $Id$
202 || ###################################################################
203 \*=====================================================================*/
204 ?>