Page principale | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de classe | Membres de fichier

htdocs/lib/CMailFile.class.php

00001 <?php 00002 /* notes from Dan Potter: 00003 Sure. I changed a few other things in here too though. One is that I let 00004 you specify what the destination filename is (i.e., what is shows up as in 00005 the attachment). This is useful since in a web submission you often can't 00006 tell what the filename was supposed to be from the submission itself. I 00007 also added my own version of chunk_split because our production version of 00008 PHP doesn't have it. You can change that back or whatever though =). 00009 Finally, I added an extra "\n" before the message text gets added into the 00010 MIME output because otherwise the message text wasn't showing up. 00011 /* 00012 note: someone mentioned a command-line utility called 'mutt' that 00013 can mail attachments. 00014 */ 00015 /* 00016 If chunk_split works on your system, change the call to my_chunk_split 00017 to chunk_split 00018 */ 00019 /* Note: if you don't have base64_encode on your sytem it will not work */ 00020 00021 /* 00022 Éric Seigne <eric.seigne@ryxeo.com> 2004.01.08 00023 - ajout de la gestion du Cc 00024 - ajout de l'expédition de plusieurs fichiers 00025 00026 Laurent Destailleur 2004.02.10 00027 - Correction d'un disfonctionnement suite à modif précédente sur la gestion 00028 des attachements multi-fichiers 00029 */ 00030 00031 // simple class that encapsulates mail() with addition of mime file attachment. 00032 class CMailFile 00033 { 00034 var $subject; 00035 var $addr_to; 00036 var $addr_cc; 00037 var $text_body; 00038 var $text_encoded; 00039 var $mime_headers; 00040 var $mime_boundary = "--==================_846811060==_"; 00041 var $smtp_headers; 00042 00043 // CMail("sujet","email_to","email_from","email_msg",tableau du path de fichiers,tableau de type mime,tableau de noms fichiers,"chaine cc") 00044 function CMailFile($subject,$to,$from,$msg,$filename_list,$mimetype_list,$mimefilename_list,$addr_cc = "") 00045 { 00046 $this->subject = $subject; 00047 $this->addr_to = $to; 00048 $this->smtp_headers = $this->write_smtpheaders($from,$addr_cc); 00049 $this->text_body = $this->write_body($msg, $filename_list); 00050 if (count($filename_list)) { 00051 $this->mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list); 00052 $this->text_encoded = $this->attach_file($filename_list,$mimetype_list,$mimefilename_list); 00053 } 00054 } 00055 00056 function attach_file($filename_list,$mimetype_list,$mimefilename_list) 00057 { 00058 for ($i = 0; $i < count($filename_list); $i++) { 00059 $encoded = $this->encode_file($filename_list[$i]); 00060 if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i]; 00061 $out = $out . "--" . $this->mime_boundary . "\n"; 00062 if (! $mimetype_list[$i]) { $mimetype_list[$i] = "application/octet-stream"; } 00063 $out = $out . "Content-type: " . $mimetype_list[$i] . "; name=\"$filename_list[$i]\";\n"; 00064 $out = $out . "Content-Transfer-Encoding: base64\n"; 00065 $out = $out . "Content-disposition: attachment; filename=\"$filename_list[$i]\"\n\n"; 00066 $out = $out . $encoded . "\n"; 00067 } 00068 $out = $out . "--" . $this->mime_boundary . "--" . "\n"; 00069 return $out; 00070 // added -- to notify email client attachment is done 00071 } 00072 00073 function encode_file($sourcefile) 00074 { 00075 // print "<pre> on encode $sourcefile </pre>\n"; 00076 if (is_readable($sourcefile)) 00077 { 00078 $fd = fopen($sourcefile, "r"); 00079 $contents = fread($fd, filesize($sourcefile)); 00080 $encoded = my_chunk_split(base64_encode($contents)); 00081 fclose($fd); 00082 } 00083 return $encoded; 00084 } 00085 00086 function sendfile() 00087 { 00088 $headers .= $this->smtp_headers . $this->mime_headers; 00089 $message = $this->text_body . $this->text_encoded; 00090 return mail($this->addr_to,$this->subject,stripslashes($message),$headers); 00091 } 00092 00093 function write_body($msgtext, $filename_list) 00094 { 00095 if (count($filename_list)) 00096 { 00097 $out = "--" . $this->mime_boundary . "\n"; 00098 $out = $out . "Content-Type: text/plain; charset=\"iso8859-15\"\n\n"; 00099 // $out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n"; 00100 } 00101 $out = $out . $msgtext . "\n"; 00102 return $out; 00103 } 00104 00105 function write_mimeheaders($filename_list, $mimefilename_list) { 00106 $out = "MIME-version: 1.0\n"; 00107 $out = $out . "Content-type: multipart/mixed; "; 00108 $out = $out . "boundary=\"$this->mime_boundary\"\n"; 00109 $out = $out . "Content-transfer-encoding: 7BIT\n"; 00110 for($i = 0; $i < count($filename_list); $i++) { 00111 if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i]; 00112 $out = $out . "X-attachments: $filename_list[$i];\n\n"; 00113 } 00114 return $out; 00115 } 00116 00117 function write_smtpheaders($addr_from,$addr_cc) 00118 { 00119 $out = "From: $addr_from\n"; 00120 if($addr_cc != "") 00121 $out = $out . "Cc: $addr_cc\n"; 00122 $out = $out . "Reply-To: $addr_from\n"; 00123 $out = $out . "X-Mailer: Dolibarr version " . DOL_VERSION ."\n"; 00124 $out = $out . "X-Sender: $addr_from\n"; 00125 return $out; 00126 } 00127 } 00128 00129 // usage - mimetype example "image/gif" 00130 // $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype); 00131 // $mailfile->sendfile(); 00132 00133 // Splits a string by RFC2045 semantics (76 chars per line, end with \r\n). 00134 // This is not in all PHP versions so I define one here manuall. 00135 function my_chunk_split($str) 00136 { 00137 $stmp = $str; 00138 $len = strlen($stmp); 00139 $out = ""; 00140 while ($len > 0) { 00141 if ($len >= 76) { 00142 $out = $out . substr($stmp, 0, 76) . "\r\n"; 00143 $stmp = substr($stmp, 76); 00144 $len = $len - 76; 00145 } 00146 else { 00147 $out = $out . $stmp . "\r\n"; 00148 $stmp = ""; $len = 0; 00149 } 00150 } 00151 return $out; 00152 } 00153 00154 // end script 00155 ?>

Généré le Thu Jul 15 20:50:37 2004 pour dolibarr par doxygen 1.3.7