[ 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: dolibarrmail.class.php,v 1.20 2005/05/09 12:12:13 rodolphe Exp $ 22 * $Source: /cvsroot/dolibarr/dolibarr/htdocs/lib/dolibarrmail.class.php,v $ 23 * 24 * Lots of code inspired from Dan Potter's CMailFile class 25 */ 26 27 /** 28 \file htdocs/lib/dolibarrmail.class.php 29 \brief Classe permettant d'envoyer des mail avec attachements, reecriture de CMailFile 30 \author Dan Potter. 31 \author Eric Seigne 32 \author Rodolphe Quiedeville 33 \author Laurent Destailleur. 34 \version $Revision: 1.20 $ 35 */ 36 37 /** 38 \class DolibarrMail 39 \brief Classe permettant d'envoyer des attachements par mail 40 \todo Classe en double avec CMailFile.class.php 41 */ 42 43 class DolibarrMail 44 { 45 var $subject; 46 var $addr_to; 47 var $addr_cc; 48 var $addr_bcc; 49 var $text_body; 50 var $text_encoded; 51 var $mime_headers; 52 var $boundary; 53 var $smtp_headers; 54 55 /** 56 \brief DolibarrMail 57 \param subject 58 \param to 59 \param from 60 \param msg 61 */ 62 63 function DolibarrMail($subject, $to, $from, $msg) 64 { 65 $this->subject = $subject; 66 $this->addr_to = $to; 67 $this->from = $from; 68 69 $this->message = wordwrap($msg, 78); 70 71 $this->errors_to = $from; 72 73 $this->boundary = md5( uniqid("dolibarr") ); 74 75 $this->addr_bcc = ""; 76 $this->addr_cc = ""; 77 $this->reply_to = ""; 78 79 $this->filename_list = array(); 80 } 81 82 /** 83 \brief PrepareFile 84 \param filename_list 85 \param mimetype_list 86 \param mimefilename_list 87 */ 88 89 function PrepareFile($filename_list, $mimetype_list, $mimefilename_list) 90 { 91 $this->filename_list = $filename_list; 92 93 $this->mime_headers=""; 94 95 if (count($filename_list)) 96 { 97 $this->mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list); 98 99 $this->text_encoded = $this->attach_file($filename_list, 100 $mimetype_list, 101 $mimefilename_list); 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 112 function attach_file($filename_list,$mimetype_list,$mimefilename_list) 113 { 114 for ($i = 0; $i < count($filename_list); $i++) 115 { 116 $encoded = $this->encode_file($filename_list[$i]); 117 118 if ($mimefilename_list[$i]) 119 { 120 $filename_list[$i] = $mimefilename_list[$i]; 121 } 122 123 $out = $out . "--".$this->boundary . "\n"; 124 125 if (! $mimetype_list[$i]) 126 { 127 $mimetype_list[$i] = "application/octet-stream"; 128 } 129 130 $out .= "Content-Type: " . $mimetype_list[$i]."\n"; 131 $out .= ' name="'.$filename_list[$i].'"'."\n"; 132 $out .= "Content-Transfer-Encoding: base64\n"; 133 $out .= "Content-Disposition: inline;\n"; 134 $out .= " filename=\"".$filename_list[$i]."\"\n\n"; 135 $out .= $encoded . "\n"; 136 } 137 $out = $out . "--".$this->boundary . "\n"; 138 139 return $out; 140 // added -- to notify email client attachment is done 141 } 142 143 /** 144 \brief Permet d'encoder un fichier 145 \param sourcefile 146 */ 147 148 function encode_file($sourcefile) 149 { 150 if (is_readable($sourcefile)) 151 { 152 $fd = fopen($sourcefile, "r"); 153 $contents = fread($fd, filesize($sourcefile)); 154 $encoded = chunk_split(base64_encode($contents)); 155 fclose($fd); 156 } 157 else 158 { 159 dolibarr_syslog("DolibarrMail::encode_file Erreur"); 160 } 161 return $encoded; 162 } 163 164 /** 165 \brief Envoi le mail 166 \return boolean vrai si mail envoyé, faux sinon 167 */ 168 169 function sendfile() 170 { 171 172 $this->smtp_headers = $this->write_smtpheaders(); 173 174 $this->text_body = $this->write_body(); 175 176 $headers = $this->smtp_headers . $this->mime_headers; 177 $message_comp = $this->text_body . $this->text_encoded; 178 179 if ($this->errors_to) 180 { 181 //dolibarr_syslog("DolibarrMail::sendfile with errorsto : ".$this->errors_to); 182 $res = mail($this->addr_to,$this->subject,stripslashes($message_comp),$headers,"-f".$this->errors_to); 183 } 184 else 185 { 186 //dolibarr_syslog("DolibarrMail::sendfile without errorsto"); 187 $res = mail($this->addr_to,$this->subject,stripslashes($message_comp),$headers); 188 } 189 190 $this->write_to_file(); 191 192 return $res; 193 } 194 195 /** 196 * \brief Ecrit le mail dans un fichier 197 * Utilisation pour le debuggage 198 */ 199 function write_to_file() 200 { 201 $this->smtp_headers = $this->write_smtpheaders(); 202 203 $this->text_body = $this->write_body(); 204 205 $headers = $this->smtp_headers . $this->mime_headers; 206 $message_comp = $this->text_body . $this->text_encoded; 207 208 $fp = fopen("/tmp/mail","w"); 209 fputs($fp, $headers); 210 fputs($fp, $message_comp); 211 fclose($fp); 212 } 213 214 /** 215 \brief Permet d'ecrire le corps du message 216 */ 217 218 function write_body() 219 { 220 $out = "\n"; 221 if (count($this->filename_list)) 222 { 223 $out = $out . "--".$this->boundary . "\n"; 224 $out = $out . 'Content-Type: text/plain; charset="iso-8859-15"'."\n"; 225 $out .= "Content-Transfer-Encoding: 8bit\n\n"; 226 } 227 else 228 { 229 //dolibarr_syslog("DolibarrMail::write_body"); 230 } 231 232 $out = $out . $this->message . "\n\n"; 233 return $out; 234 } 235 236 /** 237 \brief création des headers mime 238 \param filename_list 239 \param mimefilename_list 240 */ 241 242 function write_mimeheaders($filename_list, $mimefilename_list) 243 { 244 $out = "MIME-Version: 1.0\n"; 245 $out = $out . 'Content-type: multipart/mixed; '."\n"; 246 $out = $out . ' boundary="'.$this->boundary.'"'."\n"; 247 248 // $out = $out . "Content-transfer-encoding: 8BIT\n"; 249 250 for($i = 0; $i < count($filename_list); $i++) 251 { 252 if ($mimefilename_list[$i]) 253 { 254 $filename_list[$i] = $mimefilename_list[$i]; 255 } 256 257 //$out = $out . "X-attachments: $filename_list[$i];\n\n"; 258 } 259 260 return $out; 261 } 262 263 /** 264 \brief création des headers smtp 265 */ 266 267 function write_smtpheaders() 268 { 269 $out = "From: $this->from\n"; 270 271 if($this->addr_cc != "") 272 $out = $out . "Cc: ".$this->addr_cc."\n"; 273 274 if($this->addr_bcc != "") 275 $out = $out . "BCc: ".$this->addr_bcc."\n"; 276 277 if($this->reply_to != "") 278 $out = $out . "Reply-To: ".$this->reply_to."\n"; 279 280 // if($this->errors_to != "") 281 //$out = $out . "Errors-to: ".$this->errors_to."\n"; 282 283 $out = $out . "X-Mailer: Dolibarr version " . DOL_VERSION ."\n"; 284 $out = $out . "X-Sender: $this->from\n"; 285 286 287 return $out; 288 } 289 290 } 291 292 ?>
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 |
![]() |