Changeset 395 for trunk/Mail.php
- Timestamp:
- Mar 2, 2012, 6:12:11 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mail.php
r394 r395 1 1 <?php 2 2 3 define( DISPOSITION_INLINE, 'inline');4 define( DISPOSITION_ATTACHMENT, 'attachment');5 6 define( CHARSET_ASCII, 'us-ascii');7 define( CHARSET_UTF8, 'utf-8');3 define('DISPOSITION_INLINE', 'inline'); 4 define('DISPOSITION_ATTACHMENT', 'attachment'); 5 6 define('CHARSET_ASCII', 'us-ascii'); 7 define('CHARSET_UTF8', 'utf-8'); 8 8 9 9 class Mail … … 17 17 var $charset = 'us-ascii'; 18 18 var $ctencoding = '7bit'; 19 var $receipt = 0; 19 var $receipt = 0; 20 20 21 21 function __construct() 22 22 { 23 23 $this->autoCheck(true); 24 $this-> boundary = '--'.md5(uniqid('myboundary'));24 $this->Boundary = md5(uniqid('myboundary')); 25 25 } 26 26 … … 39 39 { 40 40 if(!is_string($From)) 41 {42 41 throw new Exception('Class Mail: error, From is not a string'); 43 }44 42 $this->xheaders['From'] = $From; 45 43 } … … 76 74 function Bcc($bcc) 77 75 { 78 if(is_array($bcc)) 79 { 80 $this->abcc = $bcc; 81 } else 82 { 83 $this->abcc[]= $bcc; 84 } 85 86 if($this->checkAddress == true) 87 $this->CheckAdresses($this->abcc); 88 } 89 90 function Body($body, $charset = '') 91 { 92 $this->body = $body; 93 94 if($charset != '') 95 { 96 $this->charset = strtolower($charset); 76 if(is_array($bcc)) $this->abcc = $bcc; 77 else $this->abcc[]= $bcc; 78 79 if($this->checkAddress == true) $this->CheckAdresses($this->abcc); 80 } 81 82 function Body($Body, $Charset = '') 83 { 84 $this->Body = $Body; 85 86 if($Charset != '') 87 { 88 $this->charset = strtolower($Charset); 97 89 if($this->charset != CHARSET_ASCII) $this->ctencoding = '8bit'; 98 90 } 99 91 } 100 92 93 function AlternateBody($Body) 94 { 95 $this->AlternateBody = $Body; 96 } 97 101 98 function Organization($org) 102 99 { … … 104 101 } 105 102 106 function Priority($ priority)107 { 108 if(!intval($ priority)) return(false);109 110 if(!isset($this->priorities[$ priority - 1])) return(false);111 112 $this->xheaders['X-Priority'] = $this->priorities[$ priority - 1];103 function Priority($Priority) 104 { 105 if(!intval($Priority)) return(false); 106 107 if(!isset($this->priorities[$Priority - 1])) return(false); 108 109 $this->xheaders['X-Priority'] = $this->priorities[$Priority - 1]; 113 110 return(true); 114 111 } 115 112 116 function Attach($ filename, $filetype = '', $disposition = DISPOSITION_ATTACHMENT)113 function Attach($Filename, $FileType = '', $Disposition = DISPOSITION_ATTACHMENT) 117 114 { 118 115 // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier 119 if($filetype == '') $filetype = 'application/x-unknown-content-type'; 120 121 $this->aattach[] = $filename; 122 $this->actype[] = $filetype; 123 $this->adispo[] = $disposition; 116 if($FileType == '') $FileType = 'application/x-unknown-content-type'; 117 118 $this->Attachments[] = array('FileName' => $Filename, 'FileType' => $FileType, 119 'Disposition' => $Disposition); 124 120 } 125 121 … … 127 123 { 128 124 // build the headers 129 $this->headers = "";125 $this->headers = ''; 130 126 // $this->xheaders['To'] = implode( ", ", $this->sendto ); 131 127 132 128 if(count($this->acc) > 0) 133 $this->xheaders['CC'] = implode(', ', $this->acc 129 $this->xheaders['CC'] = implode(', ', $this->acc); 134 130 135 131 if(count($this->abcc) > 0) … … 150 146 } 151 147 152 $this->xheaders['X-Mailer'] = 'P hp/libMailv1.3';148 $this->xheaders['X-Mailer'] = 'PHP/Mail'; 153 149 154 150 // include attached files 155 if( count( $this->aattach ) > 0 ) 156 { 157 $this->BuildAttachement(); 158 } else 159 { 160 $this->fullBody = $this->body; 161 } 162 151 if(count($this->Attachments) > 0) $this->BuildAttachement(); 152 else $this->fullBody = $this->Body; 153 163 154 reset($this->xheaders); 164 while(list($hdr, $value) = each($this->xheaders))165 { 166 if($ hdr != 'Subject') $this->headers .= "$hdr: $value\n";155 foreach($this->xheaders as $Header => $Value) 156 { 157 if($Header != 'Subject') $this->headers .= $Header.': '.$Value."\n"; 167 158 } 168 159 } … … 171 162 { 172 163 $this->BuildMail(); 173 $this->strTo = implode(', ', $this->SendTo 174 175 // envoie du mail176 $res = @mail($this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers);164 $this->strTo = implode(', ', $this->SendTo); 165 166 $res = mail($this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers); 167 return($res); 177 168 } 178 169 … … 180 171 { 181 172 $this->BuildMail(); 182 $mail = "To: " . $this->strTo . "\n"; 183 $mail .= $this->headers . "\n"; 184 $mail .= $this->fullBody; 185 return($mail); 186 } 187 188 function ValidEmail($address) 189 { 190 if(ereg( ".*<(.+)>", $address, $regs)) 191 { 192 $address = $regs[1]; 193 } 194 if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address)) 173 $Mail = "To: " . $this->strTo."\n"; 174 $Mail .= $this->headers."\n"; 175 $Mail .= $this->fullBody; 176 return($Mail); 177 } 178 179 function ValidEmail($Address) 180 { 181 if(ereg(".*<(.+)>", $Address, $regs)) $Address = $regs[1]; 182 if(ereg("^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$", $Address)) 195 183 return(true); 196 184 else return(false); 197 185 } 198 186 199 function CheckAdresses( $aad ) 200 { 201 for($i=0; $i< count($aad); $i++) 202 { 203 if(!$this->ValidEmail($aad[$i])) 204 { 205 throw new Exception('Class Mail, method Mail : invalid address '.$aad[$i]); 206 } 187 function CheckAdresses($Addresses) 188 { 189 foreach($Addresses as $Address) 190 { 191 if(!$this->ValidEmail($Address)) 192 throw new Exception('Class Mail, method Mail : invalid address '.$Address); 207 193 } 208 194 } … … 210 196 private function BuildAttachement() 211 197 { 212 $this->xheaders['Content-Type'] = "multipart/mixed;\n boundary=\"$this->boundary\""; 213 214 $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n"; 215 $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n" . $this->body ."\n"; 216 217 $sep = chr(13).chr(10); 218 219 $ata = array(); 220 $k = 0; 221 222 // for each attached file, do... 223 for($i = 0; $i < count($this->aattach); $i++) 224 { 225 $filename = $this->aattach[$i]; 226 $basename = basename($filename); 227 $ctype = $this->actype[$i]; // content-type 228 $disposition = $this->adispo[$i]; 229 230 if(!file_exists($filename)) 231 { 232 throw new Exception('Class Mail, method attach : file '.$filename.' can\'t be found'); 233 } 234 $subhdr = '--'.$this->boundary."\nContent-type: ".$ctype.";\n name=\"".$basename. 235 "\"\nContent-Transfer-Encoding: base64\nContent-Disposition: ".$disposition. 236 ";\n filename=\"".$basename."\"\n"; 237 $ata[$k++] = $subhdr; 238 // non encoded line length 239 $linesz = filesize($filename) + 1; 240 $fp = fopen( $filename, 'r'); 241 $ata[$k++] = chunk_split(base64_encode(fread($fp, $linesz))); 242 fclose($fp); 243 } 244 $this->fullBody .= implode($sep, $ata); 198 $this->xheaders['Content-Type'] = "multipart/mixed;\n boundary=\"PHP-mixed-".$this->Boundary."\""; 199 200 $this->fullBody = "This is a multi-part message in MIME format.\n--PHP-mixed-".$this->Boundary."\n"; 201 if($this->AlternateBody != '') 202 { 203 $this->fullBody .= 'Content-Type: multipart/alternative; boundary="PHP-alt-'.$this->Boundary.'"'; 204 $this->fullBody .= "\n\n".'--PHP-alt-'.$this->Boundary."\n"; 205 $this->fullBody .= "Content-Type: text/plain; charset=".$this->charset. 206 "\nContent-Transfer-Encoding: ".$this->ctencoding."\n\n".$this->Body."\n"; 207 208 $this->fullBody .= "\n\n".'--PHP-alt-'.$this->Boundary."\n"; 209 $this->fullBody .= "Content-Type: text/html; charset=".$this->charset. 210 "\nContent-Transfer-Encoding: ".$this->ctencoding."\n\n".$this->AlternateBody."\n"; 211 //$this->fullBody .= "\n\n".'--PHP-alt-'.$this->Boundary."\n\n"; 212 } else 213 $this->fullBody .= "Content-Type: text/plain; charset=".$this->charset. 214 "\nContent-Transfer-Encoding: ".$this->ctencoding."\n\n".$this->Body."\n"; 215 216 $ata = array(); 217 foreach($this->Attachments as $Attachment) 218 { 219 $FileName = $Attachment['FileName']; 220 $BaseName = basename($FileName); 221 $ctype = $Attachment['FileType']; 222 $Disposition = $Attachment['Disposition'];; 223 224 if(!file_exists($FileName)) 225 throw new Exception('Mail: Attached file '.$FileName.' can\'t be found'); 226 $subhdr = "\n".'--PHP-mixed-'.$this->Boundary."\nContent-type: ".$ctype.";\n name=\"".$BaseName. 227 "\"\nContent-Transfer-Encoding: base64\nContent-Disposition: ".$Disposition. 228 ";\n filename=\"".$BaseName."\"\n"; 229 $ata[] = $subhdr; 230 $ata[] = chunk_split(base64_encode(file_get_contents($FileName))); 231 } 232 $this->fullBody .= implode("\r\n", $ata); 245 233 } 246 234 }
Note:
See TracChangeset
for help on using the changeset viewer.