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