00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00039
00040 class CMailFile
00041 {
00042 var $subject;
00043 var $addr_to;
00044 var $addr_cc;
00045 var $text_body;
00046 var $text_encoded;
00047 var $mime_headers;
00048 var $mime_boundary =
"--==================_846811060==_";
00049 var $smtp_headers;
00050
00063
00064 function
CMailFile($subject,$to,$from,$msg,$filename_list,$mimetype_list,$mimefilename_list,$addr_cc =
"")
00065 {
00066 $this->subject = $subject;
00067 $this->addr_to = $to;
00068 $this->smtp_headers = $this->
write_smtpheaders($from,$addr_cc);
00069 $this->text_body = $this->
write_body($msg, $filename_list);
00070
if (count($filename_list)) {
00071 $this->mime_headers = $this->
write_mimeheaders($filename_list, $mimefilename_list);
00072 $this->text_encoded = $this->
attach_file($filename_list,$mimetype_list,$mimefilename_list);
00073 }
00074 }
00075
00083 function
attach_file($filename_list,$mimetype_list,$mimefilename_list)
00084 {
00085
for ($i = 0; $i < count($filename_list); $i++) {
00086 $encoded = $this->
encode_file($filename_list[$i]);
00087
if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
00088 $out = $out .
"--" . $this->mime_boundary .
"\n";
00089
if (! $mimetype_list[$i]) { $mimetype_list[$i] =
"application/octet-stream"; }
00090 $out = $out .
"Content-type: " . $mimetype_list[$i] .
"; name=\"$filename_list[$i]\";\n";
00091 $out = $out .
"Content-Transfer-Encoding: base64\n";
00092 $out = $out .
"Content-disposition: attachment; filename=\"$filename_list[$i]\"\n\n";
00093 $out = $out . $encoded .
"\n";
00094 }
00095 $out = $out .
"--" . $this->mime_boundary .
"--" .
"\n";
00096
return $out;
00097
00098 }
00099
00105 function
encode_file($sourcefile)
00106 {
00107
00108
if (is_readable($sourcefile))
00109 {
00110 $fd = fopen($sourcefile,
"r");
00111 $contents = fread($fd, filesize($sourcefile));
00112 $encoded =
my_chunk_split(base64_encode($contents));
00113 fclose($fd);
00114 }
00115
return $encoded;
00116 }
00117
00122 function
sendfile()
00123 {
00124 $headers .= $this->smtp_headers . $this->mime_headers;
00125 $message = $this->text_body . $this->text_encoded;
00126
return mail($this->addr_to,$this->subject,stripslashes($message),$headers);
00127 }
00128
00135 function
write_body($msgtext, $filename_list)
00136 {
00137
if (count($filename_list))
00138 {
00139 $out =
"--" . $this->mime_boundary .
"\n";
00140 $out = $out .
"Content-Type: text/plain; charset=\"iso8859-15\"\n\n";
00141
00142 }
00143 $out = $out . $msgtext .
"\n";
00144
return $out;
00145 }
00146
00153 function
write_mimeheaders($filename_list, $mimefilename_list) {
00154 $out =
"MIME-version: 1.0\n";
00155 $out = $out .
"Content-type: multipart/mixed; ";
00156 $out = $out .
"boundary=\"$this->mime_boundary\"\n";
00157 $out = $out .
"Content-transfer-encoding: 7BIT\n";
00158
for($i = 0; $i < count($filename_list); $i++) {
00159
if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i];
00160 $out = $out .
"X-attachments: $filename_list[$i];\n\n";
00161 }
00162
return $out;
00163 }
00164
00171 function
write_smtpheaders($addr_from,$addr_cc)
00172 {
00173 $out =
"From: $addr_from\n";
00174
if($addr_cc !=
"")
00175 $out = $out .
"Cc: $addr_cc\n";
00176 $out = $out .
"Reply-To: $addr_from\n";
00177 $out = $out .
"X-Mailer: Dolibarr version " . DOL_VERSION .
"\n";
00178 $out = $out .
"X-Sender: $addr_from\n";
00179
return $out;
00180 }
00181 }
00182
00190
00191
00192
00193
00194
00195
00196
00197 function
my_chunk_split($str)
00198 {
00199 $stmp = $str;
00200 $len = strlen($stmp);
00201 $out =
"";
00202
while ($len > 0) {
00203
if ($len >= 76) {
00204 $out = $out . substr($stmp, 0, 76) .
"\r\n";
00205 $stmp = substr($stmp, 76);
00206 $len = $len - 76;
00207 }
00208
else {
00209 $out = $out . $stmp .
"\r\n";
00210 $stmp =
""; $len = 0;
00211 }
00212 }
00213
return $out;
00214 }
00215
00216
00217 ?>