[ Index ] |
|
Code source de Dolibarr 2.0.1 |
1 <?php 2 /* Copyright (C) 2000-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org> 3 * Copyright (C) 2003 Jean-Louis Bergamo <jlb@j1b.org> 4 * Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 * or see http://www.gnu.org/ 20 * 21 * $Id: CMailFile.class.php,v 1.19 2005/10/17 16:58:15 eldy Exp $ 22 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/lib/CMailFile.class.php,v $ 23 * 24 * Lots of code inspired from Dan Potter's CMailFile class 25 * 26 * If chunk_split does not works on your system, change the call to chunk_split 27 * to my_chunk_split 28 */ 29 30 /** 31 \file htdocs/lib/CMailFile.class.php 32 \brief Classe permettant d'envoyer des mail avec attachements 33 \author Dan Potter. 34 \author Eric Seigne 35 \author Laurent Destailleur. 36 \version $Revision: 1.19 $ 37 */ 38 39 /** 40 \class CMailFile 41 \brief Classe d'envoi de mails et pièces jointes. Encapsule mail() avec d'éventuel attachements. 42 \remarks Usage: 43 \remarks $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc); 44 \remarks $mailfile->sendfile(); 45 */ 46 47 class CMailFile 48 { 49 var $subject; 50 var $addr_from_email; 51 var $addr_from_name; 52 var $addr_to; 53 var $addr_cc; 54 var $addr_bcc; 55 var $text_body; 56 var $text_encoded; 57 var $mime_headers; 58 var $mime_boundary; 59 var $smtp_headers; 60 61 /** 62 \brief CMailFile 63 \param subject sujet 64 \param to email destinataire ("Nom <email>" ou "email" ou "<email>") 65 \param from email emetteur ("Nom <email>" ou "email" ou "<email>") 66 \param msg message 67 \param filename_list tableau de fichiers attachés 68 \param mimetype_list tableau des types des fichiers attachés 69 \param mimefilename_list tableau des noms des fichiers attachés 70 \param addr_cc email cc 71 \param addr_bcc email bcc 72 */ 73 function CMailFile($subject,$to,$from,$msg, 74 $filename_list=array(),$mimetype_list=array(),$mimefilename_list=array(), 75 $addr_cc="",$addr_bcc="") 76 { 77 dolibarr_syslog("CMailFile::CMailfile: from=$from, filename_list[0]=$filename_list[0], mimetype_list[0]=$mimetype_list[0] mimefilename_list[0]=$mimefilename_list[0]"); 78 79 $this->mime_boundary = md5( uniqid("dolibarr") ); 80 81 $this->subject = $subject; 82 if (eregi('(.*)<(.+)>',$from,$regs)) 83 { 84 $this->addr_from_name = trim($regs[1]); 85 $this->addr_from_email = trim($regs[2]); 86 } 87 else 88 { 89 $this->addr_from_name = $from; 90 $this->addr_from_email = $from; 91 } 92 $this->addr_to = $to; 93 $this->addr_cc = $addr_cc; 94 $this->addr_bcc = $addr_bcc; 95 $this->smtp_headers = $this->write_smtpheaders(); 96 $this->text_body = $this->write_body($msg, $filename_list); 97 if (count($filename_list)) 98 { 99 $this->mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list); 100 $this->text_encoded = $this->attach_file($filename_list,$mimetype_list,$mimefilename_list); 101 } 102 } 103 104 105 /** 106 \brief permet d'attacher un fichier 107 \param filename_list 108 \param mimetype_list 109 \param mimefilename_list 110 */ 111 function attach_file($filename_list,$mimetype_list,$mimefilename_list) 112 { 113 for ($i = 0; $i < count($filename_list); $i++) 114 { 115 dolibarr_syslog("CMailFile::attach_file: i=$i"); 116 $encoded = $this->encode_file($filename_list[$i]); 117 if ($encoded != -1) 118 { 119 if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i]; 120 $out = $out . "--" . $this->mime_boundary . "\n"; 121 if (! $mimetype_list[$i]) { $mimetype_list[$i] = "application/octet-stream"; } 122 $out .= "Content-type: " . $mimetype_list[$i] . "; name=\"$filename_list[$i]\";\n"; 123 $out .= "Content-Transfer-Encoding: base64\n"; 124 $out .= "Content-disposition: attachment; filename=\"$filename_list[$i]\"\n\n"; 125 $out .= $encoded . "\n"; 126 } 127 } 128 $out = $out . "--" . $this->mime_boundary . "--" . "\n"; 129 return $out; 130 } 131 132 133 /** 134 \brief Permet d'encoder un fichier 135 \param sourcefile 136 \return <0 si erreur, fichier encodé si ok 137 */ 138 function encode_file($sourcefile) 139 { 140 if (is_readable($sourcefile)) 141 { 142 $fd = fopen($sourcefile, "r"); 143 $contents = fread($fd, filesize($sourcefile)); 144 $encoded = chunk_split(base64_encode($contents)); 145 //$encoded = my_chunk_split(base64_encode($contents)); 146 fclose($fd); 147 return $encoded; 148 } 149 else 150 { 151 dolibarr_syslog("CMailFile::encode_file: Can't read file '$sourcefile'"); 152 return -1; 153 } 154 } 155 156 /** 157 \brief Envoi le mail 158 \return boolean vrai si mail envoyé, faux sinon 159 */ 160 function sendfile() 161 { 162 $headers = $this->smtp_headers . $this->mime_headers; 163 $message=$this->text_body . $this->text_encoded; 164 165 // Fix si windows, la fonction mail ne traduit pas les \n, il faut donc y mettre 166 // des champs \r\n (imposés par SMTP). 167 if (eregi('^win',PHP_OS)) 168 { 169 $message = eregi_replace("\r","", $this->text_body . $this->text_encoded); 170 $message = eregi_replace("\n","\r\n", $message); 171 } 172 173 dolibarr_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject); 174 //dolibarr_syslog("CMailFile::sendfile message=\n".$message); 175 //dolibarr_syslog("CMailFile::sendfile header=\n".$headers); 176 177 $errorlevel=error_reporting(); 178 //error_reporting($errorlevel ^ E_WARNING); // Désactive warnings 179 if ($this->errors_to) 180 { 181 dolibarr_syslog("CMailFile::sendfile with errorsto : ".$this->errors_to); 182 $res = mail($this->addr_to,$this->subject,stripslashes($message),$headers,"-f".$this->errors_to); 183 } 184 else 185 { 186 dolibarr_syslog("CMailFile::sendfile"); 187 $res = mail($this->addr_to,$this->subject,stripslashes($message),$headers); 188 } 189 //if (! $res) $this->error= 190 error_reporting($errorlevel); // Réactive niveau erreur origine 191 192 //$this->write_to_file(); 193 194 return $res; 195 } 196 197 /** 198 * \brief Ecrit le mail dans un fichier. 199 * Utilisation pour le debuggage 200 */ 201 function write_to_file() 202 { 203 $headers = $this->smtp_headers . $this->mime_headers; 204 $message = $this->text_body . $this->text_encoded; 205 206 $fp = fopen("/tmp/mail","w"); 207 fputs($fp, $headers); 208 fputs($fp, $message); 209 fclose($fp); 210 } 211 212 /** 213 \brief Permet d'ecrire le corps du message 214 \param msgtext 215 \param filename_list 216 */ 217 function write_body($msgtext, $filename_list) 218 { 219 $out=''; 220 if (count($filename_list)) 221 { 222 $out = "--" . $this->mime_boundary . "\n"; 223 $out = $out . "Content-Type: text/plain; charset=\"iso8859-15\"\n\n"; 224 // $out = $out . "Content-Type: text/plain; charset=\"us-ascii\"\n\n"; 225 } 226 $out = $out . $msgtext . "\n"; 227 return $out; 228 } 229 230 /** 231 \brief création des headers mime 232 \param filename_list 233 \param mimefilename_list 234 */ 235 function write_mimeheaders($filename_list, $mimefilename_list) { 236 $out = "MIME-version: 1.0\n"; 237 $out = $out . "Content-type: multipart/mixed; "; 238 $out = $out . "boundary=\"$this->mime_boundary\"\n"; 239 $out = $out . "Content-transfer-encoding: 7BIT\n"; 240 for($i = 0; $i < count($filename_list); $i++) { 241 if ($mimefilename_list[$i]) $filename_list[$i] = $mimefilename_list[$i]; 242 $out = $out . "X-attachments: $filename_list[$i];\n\n"; 243 } 244 return $out; 245 } 246 247 /** 248 \brief création des headers smtp 249 */ 250 function write_smtpheaders() 251 { 252 $out = ""; 253 254 $out .= "X-Mailer: Dolibarr version " . DOL_VERSION ."\n"; 255 $out .= "X-Sender: <$this->addr_from_email>\n"; 256 //$out .= "X-Priority: 3\n"; 257 258 $out .= "Return-path: <$this->addr_from_email>\n"; 259 $out .= "From: $this->addr_from_name <".$this->addr_from_email.">\n"; 260 261 if (isset($this->addr_cc) && $this->addr_cc) $out .= "Cc: ".$this->addr_cc."\n"; 262 if (isset($this->addr_bcc) && $this->addr_bcc) $out .= "Bcc: ".$this->addr_bcc."\n"; 263 if (isset($this->reply_to) && $this->reply_to) $out .= "Reply-To: ".$this->reply_to."\n"; 264 // if($this->errors_to != "") 265 //$out = $out . "Errors-to: ".$this->errors_to."\n"; 266 267 dolibarr_syslog("CMailFile::write_smtpheaders $out"); 268 return $out; 269 } 270 271 } 272 273 274 /** 275 \brief Permet de diviser une chaine (RFC2045) 276 \param str 277 \remarks function chunk_split qui remplace celle de php si nécéssaire 278 \remarks 76 caractères par ligne, terminé par "\r\n" 279 */ 280 function my_chunk_split($str) 281 { 282 $stmp = $str; 283 $len = strlen($stmp); 284 $out = ""; 285 while ($len > 0) { 286 if ($len >= 76) { 287 $out = $out . substr($stmp, 0, 76) . "\r\n"; 288 $stmp = substr($stmp, 76); 289 $len = $len - 76; 290 } 291 else { 292 $out = $out . $stmp . "\r\n"; 293 $stmp = ""; $len = 0; 294 } 295 } 296 return $out; 297 } 298 299 300 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 12:29:37 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |