Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00048 class ilMimeMail
00049 {
00054 var $sendto = array();
00055
00059 var $acc = array();
00060
00064 var $abcc = array();
00065
00070 var $aattach = array();
00071
00076 var $xheaders = array();
00077
00082 var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
00083
00088 var $charset = "utf-8";
00089 var $ctencoding = "7bit";
00090 var $receipt = 0;
00091
00092
00097 function ilMimeMail()
00098 {
00099 $this->autoCheck( false);
00100 $this->boundary= "--" . md5( uniqid("myboundary") );
00101 }
00102
00103
00111 function autoCheck($bool )
00112 {
00113 if( $bool )
00114 $this->checkAddress = true;
00115 else
00116 $this->checkAddress = false;
00117 }
00118
00119
00124 function Subject($subject )
00125 {
00126 $this->xheaders['Subject'] = ilMimeMail::_mimeEncode(strtr($subject,"\r\n"," "));
00127 }
00128
00129
00135 function From($from )
00136 {
00137
00138 if( ! is_string($from) ) {
00139 echo "Class Mail: error, From is not a string";
00140 exit;
00141 }
00142
00143
00144
00145 $this->xheaders['From'] = $from;
00146 }
00147
00152 function ReplyTo( $address )
00153 {
00154
00155 if( ! is_string($address) )
00156 return false;
00157
00158 $this->xheaders["Reply-To"] = $address;
00159
00160 }
00161
00162
00169 function Receipt()
00170 {
00171 $this->receipt = 1;
00172 }
00173
00174
00180 function To( $to )
00181 {
00182
00183
00184 if( is_array( $to ) )
00185 $this->sendto= $to;
00186 else
00187 $this->sendto[] = $to;
00188
00189 if( $this->checkAddress == true )
00190 $this->CheckAdresses( $this->sendto );
00191
00192 }
00193
00194
00201 function Cc($cc)
00202 {
00203 if( is_array($cc) )
00204 $this->acc= $cc;
00205 else
00206 $this->acc[]= $cc;
00207
00208 if( $this->checkAddress == true )
00209 $this->CheckAdresses( $this->acc );
00210
00211 }
00212
00213
00214
00222 function Bcc( $bcc )
00223 {
00224 if( is_array($bcc) ) {
00225 $this->abcc = $bcc;
00226 } else {
00227 $this->abcc[]= $bcc;
00228 }
00229
00230 if( $this->checkAddress == true )
00231 $this->CheckAdresses( $this->abcc );
00232 }
00233
00234
00244 function Body( $body, $charset="" )
00245 {
00246 $this->body = $body;
00247
00248 if( $charset != "" ) {
00249 $this->charset = strtolower($charset);
00250 if( $this->charset != "us-ascii" )
00251 $this->ctencoding = "8bit";
00252 }
00253 }
00254
00255
00261 function Organization( $org )
00262 {
00263 if( trim( $org != "" ) )
00264 $this->xheaders['Organization'] = $org;
00265 }
00266
00267
00275 function Priority( $priority )
00276 {
00277 if( ! intval( $priority ) )
00278 return false;
00279
00280 if( ! isset( $this->priorities[$priority-1]) )
00281 return false;
00282
00283 $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
00284
00285 return true;
00286
00287 }
00288
00289
00297 function Attach( $filename, $filetype = "", $disposition = "inline" )
00298 {
00299
00300 if( $filetype == "" )
00301 $filetype = "application/x-unknown-content-type";
00302
00303 $this->aattach[] = $filename;
00304 $this->actype[] = $filetype;
00305 $this->adispo[] = $disposition;
00306 }
00307
00312 function BuildMail()
00313 {
00314
00315
00316 $this->headers = "";
00317
00318
00319 if( count($this->acc) > 0 )
00320 $this->xheaders['CC'] = implode( ", ", $this->acc );
00321
00322 if( count($this->abcc) > 0 )
00323 $this->xheaders['BCC'] = implode( ", ", $this->abcc );
00324
00325
00326 if( $this->receipt ) {
00327 if( isset($this->xheaders["Reply-To"] ) )
00328 $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
00329 else
00330 $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
00331 }
00332
00333 if( $this->charset != "" ) {
00334 $this->xheaders["Mime-Version"] = "1.0";
00335 $this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
00336 $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
00337 }
00338
00339 $this->xheaders["X-Mailer"] = "Php/libMailv1.3";
00340
00341
00342 if( count( $this->aattach ) > 0 ) {
00343 $this->_build_attachement();
00344 } else {
00345 $this->fullBody = $this->body;
00346 }
00347
00348 reset($this->xheaders);
00349 while( list( $hdr,$value ) = each( $this->xheaders ) ) {
00350 if( $hdr != "Subject" )
00351 $this->headers .= "$hdr: $value\n";
00352 }
00353
00354
00355 }
00356
00361 function Send()
00362 {
00363 #global $ilLog;
00364
00365 $this->BuildMail();
00366
00367 $this->strTo = implode( ", ", $this->sendto );
00368
00369
00370 $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
00371 #$ilLog->write($this->strTo.' '. $this->xheaders['Subject'].' '. $this->fullBody.' '. $this->headers);
00372 }
00373
00374
00375
00381 function Get()
00382 {
00383 $this->BuildMail();
00384 $mail = "To: " . $this->strTo . "\n";
00385 $mail .= $this->headers . "\n";
00386 $mail .= $this->fullBody;
00387 return $mail;
00388 }
00389
00390
00398 function ValidEmail($address)
00399 {
00400 if( ereg( ".*<(.+)>", $address, $regs ) ) {
00401 $address = $regs[1];
00402 }
00403 if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
00404 return true;
00405 else
00406 return false;
00407 }
00408
00409
00416 function CheckAdresses( $aad )
00417 {
00418 for($i=0;$i< count( $aad); $i++ ) {
00419 if( ! $this->ValidEmail( $aad[$i]) ) {
00420 echo "Class Mail, method Mail : invalid address $aad[$i]";
00421 exit;
00422 }
00423 }
00424 }
00425
00426
00432 function _build_attachement()
00433 {
00434
00435 $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
00436
00437 $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
00438 $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
00439 $this->body ."\n";
00440
00441 $sep= chr(13) . chr(10);
00442
00443 $ata= array();
00444 $k=0;
00445
00446
00447 for( $i=0; $i < count( $this->aattach); $i++ ) {
00448
00449 $filename = $this->aattach[$i];
00450 $basename = basename($filename);
00451 $ctype = $this->actype[$i];
00452 $disposition = $this->adispo[$i];
00453
00454 if( ! file_exists( $filename) ) {
00455 echo "Class Mail, method attach : file $filename can't be found"; exit;
00456 }
00457 $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
00458 "base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n";
00459 $ata[$k++] = $subhdr;
00460
00461 $linesz= filesize( $filename)+1;
00462 $fp= fopen( $filename, 'r' );
00463 $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
00464 fclose($fp);
00465 }
00466 $this->fullBody .= implode($sep, $ata);
00467 }
00468
00469 function _mimeEncode($a_string)
00470 {
00471 $encoded = '=?utf-8?b?';
00472 $encoded .= base64_encode($a_string);
00473 $encoded .= '?=';
00474
00475 return $encoded;
00476 }
00477
00478 }
00479 ?>