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