1 |
<?php |
2 |
/* --------------------------------------------------------- |
3 |
MIME Class: |
4 |
Allows creation of e-mail messages via the MIME Standard. |
5 |
The class supports multiple attachments and presenting |
6 |
an e-mail in HTML. |
7 |
--------------------------------------------------------- */ |
8 |
//include ("MIME.def"); |
9 |
|
10 |
define('BASE64', 'base64'); |
11 |
define('BIT7', '7bit'); |
12 |
define('QP', 'quoted_printable'); |
13 |
define('NOSUBJECT', '(No Subject)'); |
14 |
define('WARNING', 'This is a MIME encoded message'); |
15 |
define('OCTET', 'application/octet-stream'); |
16 |
define('TEXT', 'text/plain'); |
17 |
define('HTML', 'text/html'); |
18 |
define('JPEG', 'image/jpg'); |
19 |
define('GIF', 'image/gif'); |
20 |
define('CRLF', "\r\n"); |
21 |
define('CHARSET', 'us-ascii'); |
22 |
define('INLINE', 'inline'); |
23 |
define('ATTACH', 'attachment'); |
24 |
define('BODY', CRLF.'BODY'.CRLF); |
25 |
|
26 |
class MIME_mail { |
27 |
//public: |
28 |
var $to; |
29 |
var $from; |
30 |
var $subject; |
31 |
var $body; |
32 |
var $headers = ""; |
33 |
var $errstr=""; |
34 |
|
35 |
// these are the names of the encoding functions, user |
36 |
// provide the names of user-defined functions |
37 |
|
38 |
var $base64_func= ''; # if !specified use PHP's base64 |
39 |
var $qp_func = ''; # None at this time |
40 |
|
41 |
// If do not have a local mailer..use this array to pass info of an SMTP object |
42 |
// e.g. $mime_mail->mailer = array('name' => 'smtp', method => 'smtp_send()'); |
43 |
// 'name' is the name of the object less the $ and 'method' can have parameters |
44 |
// specific to itself. If you are using MIME_mail object's to, from, etc. |
45 |
// remember to send parameters a literal strings referring 'this' object!!!! |
46 |
// If in doubt, you are probably better off subclassing this class... |
47 |
var $mailer = ""; # Set this to the name of a valid mail object |
48 |
|
49 |
//private: |
50 |
var $mimeparts = array(); |
51 |
|
52 |
// Constructor. |
53 |
function MIME_mail($from="", $to="", $subject="", $body="", $headers = "") { |
54 |
$this->to = $to; |
55 |
$this->from = $from; |
56 |
$this->subject = $subject; |
57 |
$this->body = $body; |
58 |
if (is_array($headers)) { |
59 |
if (sizeof($headers)>1) |
60 |
$headers=join(CRLF, $headers); |
61 |
else |
62 |
$headers=$headers[0]; |
63 |
} |
64 |
if ($from) { |
65 |
$headers = preg_replace("!(from:\ ?.+?[\r\n]?\b)!i", '', $headers); |
66 |
} |
67 |
$this->headers = chop($headers); |
68 |
$this->mimeparts[] = "" ; //Bump up location 0; |
69 |
$this->errstr = ""; |
70 |
return; |
71 |
} |
72 |
|
73 |
/* --------------------------------------------------------- |
74 |
Attach a 'file' to e-mail message |
75 |
Pass a file name to attach. |
76 |
This function returns a success/failure code/key of current |
77 |
attachment in array (+1). Read attach() below. |
78 |
--------------------------------------------------------- */ |
79 |
function fattach($path, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') { |
80 |
$this->errstr = ""; |
81 |
if (!file_exists($path)) { |
82 |
$this->errstr = "File does not exist"; |
83 |
return 0; |
84 |
} |
85 |
// Read in file |
86 |
$fp = fopen($path, "rb"); # (b)inary for Win compatability |
87 |
if (!$fp) { |
88 |
$this->errstr = "fopen() failed"; |
89 |
return 0; //failed |
90 |
} |
91 |
$contenttype .= ";\r\n\tname=".basename($path); |
92 |
$data = fread($fp, filesize($path)); |
93 |
return $this->attach($data, |
94 |
$description, |
95 |
$contenttype, |
96 |
$encoding, |
97 |
$disp); |
98 |
} |
99 |
|
100 |
/* --------------------------------------------------------- |
101 |
Attach data provided by user (rather than a file) |
102 |
Useful when you want to MIME encode user input |
103 |
like HTML. NOTE: This function returns key at which the requested |
104 |
data is attached. IT IS CURRENT KEY VALUE + 1!! |
105 |
Construct the body with MIME parts |
106 |
--------------------------------------------------------- */ |
107 |
function attach($data, $description = "", $contenttype = OCTET, $encoding = BASE64, $disp = '') { |
108 |
$this->errstr = ""; |
109 |
if (empty($data)) { |
110 |
$this->errstr = "No data to be attached"; |
111 |
return 0; |
112 |
} |
113 |
if (trim($contenttype) == '') $contenttype = OCTET ; |
114 |
if (trim($encoding) == '') $encoding = BASE64; |
115 |
if ($encoding == BIT7) $emsg = $data; |
116 |
elseif ($encoding == QP) |
117 |
$emsg = $$this->qp_func($data); |
118 |
elseif ($encoding == BASE64) { |
119 |
if (!$this->base64_func) # Check if there is user-defined function |
120 |
$emsg = base64_encode($data); |
121 |
else |
122 |
$emsg = $$this->base64_func($data); |
123 |
} |
124 |
$emsg = chunk_split($emsg); |
125 |
//Check if content-type is text/plain and if charset is not specified append default CHARSET |
126 |
if (preg_match("!^".TEXT."!i", $contenttype) && !preg_match("!;charset=!i", $contenttype)) |
127 |
$contenttype .= ";\r\n\tcharset=".CHARSET ; |
128 |
$msg = sprintf("Content-Type: %sContent-Transfer-Encoding: %s%s%s%s", |
129 |
$contenttype.CRLF, |
130 |
$encoding.CRLF, |
131 |
((($description) && (BODY != $description))?"Content-Description: $description".CRLF:""), |
132 |
($disp?"Content-Disposition: $disp".CRLF:""), |
133 |
CRLF.$emsg.CRLF); |
134 |
BODY==$description? $this->mimeparts[0] = $msg: $this->mimeparts[] = $msg ; |
135 |
return sizeof($this->mimeparts); |
136 |
} |
137 |
|
138 |
/* --------------------------------------------------------- |
139 |
private: |
140 |
Construct mail message header from info already given. |
141 |
This is a very important function. It shows how exactly |
142 |
the MIME message is constructed. |
143 |
--------------------------------------------------------- */ |
144 |
function build_message() { |
145 |
|
146 |
|
147 |
$this->errstr = ""; |
148 |
$msg = ""; |
149 |
$boundary = 'PM'.chr(rand(65, 91)).'------'.md5(uniqid(rand())); # Boundary marker |
150 |
$nparts = sizeof($this->mimeparts); |
151 |
|
152 |
// Case 1: Attachment list is there. Therefore MIME Message header must have multipart/mixed |
153 |
if (is_array($this->mimeparts) && ($nparts > 1)): |
154 |
$c_ver = "MIME-Version: 1.0".CRLF; |
155 |
$c_type = 'Content-Type: multipart/mixed;'.CRLF."\tboundary=\"$boundary\"".CRLF; |
156 |
$c_enc = "Content-Transfer-Encoding: ".BIT7.CRLF; |
157 |
$c_desc = $c_desc?"Content-Description: $c_desc".CRLF:""; |
158 |
$warning = CRLF.WARNING.CRLF.CRLF ; |
159 |
|
160 |
// Since we are here, it means we do have attachments => body must become an attachment too. |
161 |
if (!empty($this->body)) { |
162 |
$this->attach($this->body, BODY, TEXT, BIT7); |
163 |
} |
164 |
|
165 |
// Now create the MIME parts of the email! |
166 |
for ($i=0 ; $i < $nparts; $i++) { |
167 |
if (!empty($this->mimeparts[$i])) |
168 |
$msg .= CRLF.'--'.$boundary.CRLF.$this->mimeparts[$i].CRLF; |
169 |
} |
170 |
$msg .= '--'.$boundary.'--'.CRLF; |
171 |
$msg = $c_ver.$c_type.$c_enc.$c_desc.$warning.$msg; |
172 |
else: |
173 |
if (!empty($this->body)) $msg .= $this->body.CRLF.CRLF; |
174 |
endif; |
175 |
return $msg; |
176 |
} |
177 |
|
178 |
|
179 |
/* --------------------------------------------------------- |
180 |
public: |
181 |
Now Generate the entire Mail Message, header and body et al. |
182 |
--------------------------------------------------------- */ |
183 |
function gen_email($force=false) { |
184 |
|
185 |
$this->errstr = ""; |
186 |
if (!empty($this->email) && !$force) return $this->email ; // saves processing |
187 |
$email = ""; |
188 |
if (empty($this->subject)) $this->subject = NOSUBJECT; |
189 |
if (!empty($this->from)) $email .= 'From: '.$this->from.CRLF; |
190 |
if (!empty($this->headers)) $email .= $this->headers.CRLF; |
191 |
$email .= $this->build_message(); |
192 |
$this->email = $email; |
193 |
return $this->email; |
194 |
} |
195 |
|
196 |
/* --------------------------------------------------------- |
197 |
public: |
198 |
Printable form |
199 |
--------------------------------------------------------- */ |
200 |
function print_mail($force=false) { |
201 |
$this->errstr = ""; |
202 |
$email = $this->gen_email($force); |
203 |
if (!empty($this->to)) $email = 'To: '.$this->to.CRLF.$email; |
204 |
if (!empty($this->subject)) $email = 'Subject: '.$this->subject.CRLF.$email; |
205 |
print $email; |
206 |
} |
207 |
|
208 |
/* --------------------------------------------------------- |
209 |
public: |
210 |
Send mail via local mailer |
211 |
--------------------------------------------------------- */ |
212 |
function send_mail($force=false) { |
213 |
$this->errstr = ""; |
214 |
$email = $this->gen_email($force); |
215 |
if (empty($this->to)) { |
216 |
$this->errstr = "To Address not specified"; |
217 |
return 0; |
218 |
} |
219 |
if (is_array($this->mailer) && (1 == sizeof($this->mailer)) ) { |
220 |
$mail_obj = $this->mailer['name']; |
221 |
$mail_method = $this->mailer['method']; |
222 |
if (empty($mail_obj)) { |
223 |
$this->errstr = "Invalid object name passed to send_mail()"; |
224 |
return 0; |
225 |
} |
226 |
global $mail_obj; |
227 |
eval("$ret = \$$mail_obj".'->'."$mail_method;"); |
228 |
return $ret; |
229 |
} |
230 |
return mail($this->to, $this->subject, "", $email); |
231 |
} |
232 |
} // Class End |