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( true );
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'] = 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 $this->xheaders['From'] = $from;
00143 }
00144
00149 function ReplyTo( $address )
00150 {
00151
00152 if( ! is_string($address) )
00153 return false;
00154
00155 $this->xheaders["Reply-To"] = $address;
00156
00157 }
00158
00159
00166 function Receipt()
00167 {
00168 $this->receipt = 1;
00169 }
00170
00171
00177 function To( $to )
00178 {
00179
00180
00181 if( is_array( $to ) )
00182 $this->sendto= $to;
00183 else
00184 $this->sendto[] = $to;
00185
00186 if( $this->checkAddress == true )
00187 $this->CheckAdresses( $this->sendto );
00188
00189 }
00190
00191
00198 function Cc($cc)
00199 {
00200 if( is_array($cc) )
00201 $this->acc= $cc;
00202 else
00203 $this->acc[]= $cc;
00204
00205 if( $this->checkAddress == true )
00206 $this->CheckAdresses( $this->acc );
00207
00208 }
00209
00210
00211
00219 function Bcc( $bcc )
00220 {
00221 if( is_array($bcc) ) {
00222 $this->abcc = $bcc;
00223 } else {
00224 $this->abcc[]= $bcc;
00225 }
00226
00227 if( $this->checkAddress == true )
00228 $this->CheckAdresses( $this->abcc );
00229 }
00230
00231
00241 function Body( $body, $charset="" )
00242 {
00243 $this->body = $body;
00244
00245 if( $charset != "" ) {
00246 $this->charset = strtolower($charset);
00247 if( $this->charset != "us-ascii" )
00248 $this->ctencoding = "8bit";
00249 }
00250 }
00251
00252
00258 function Organization( $org )
00259 {
00260 if( trim( $org != "" ) )
00261 $this->xheaders['Organization'] = $org;
00262 }
00263
00264
00272 function Priority( $priority )
00273 {
00274 if( ! intval( $priority ) )
00275 return false;
00276
00277 if( ! isset( $this->priorities[$priority-1]) )
00278 return false;
00279
00280 $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
00281
00282 return true;
00283
00284 }
00285
00286
00294 function Attach( $filename, $filetype = "", $disposition = "inline" )
00295 {
00296
00297 if( $filetype == "" )
00298 $filetype = "application/x-unknown-content-type";
00299
00300 $this->aattach[] = $filename;
00301 $this->actype[] = $filetype;
00302 $this->adispo[] = $disposition;
00303 }
00304
00309 function BuildMail()
00310 {
00311
00312
00313 $this->headers = "";
00314
00315
00316 if( count($this->acc) > 0 )
00317 $this->xheaders['CC'] = implode( ", ", $this->acc );
00318
00319 if( count($this->abcc) > 0 )
00320 $this->xheaders['BCC'] = implode( ", ", $this->abcc );
00321
00322
00323 if( $this->receipt ) {
00324 if( isset($this->xheaders["Reply-To"] ) )
00325 $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
00326 else
00327 $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
00328 }
00329
00330 if( $this->charset != "" ) {
00331 $this->xheaders["Mime-Version"] = "1.0";
00332 $this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
00333 $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
00334 }
00335
00336 $this->xheaders["X-Mailer"] = "Php/libMailv1.3";
00337
00338
00339 if( count( $this->aattach ) > 0 ) {
00340 $this->_build_attachement();
00341 } else {
00342 $this->fullBody = $this->body;
00343 }
00344
00345 reset($this->xheaders);
00346 while( list( $hdr,$value ) = each( $this->xheaders ) ) {
00347 if( $hdr != "Subject" )
00348 $this->headers .= "$hdr: $value\n";
00349 }
00350
00351
00352 }
00353
00358 function Send()
00359 {
00360 $this->BuildMail();
00361
00362 $this->strTo = implode( ", ", $this->sendto );
00363
00364
00365 $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
00366
00367 }
00368
00369
00370
00376 function Get()
00377 {
00378 $this->BuildMail();
00379 $mail = "To: " . $this->strTo . "\n";
00380 $mail .= $this->headers . "\n";
00381 $mail .= $this->fullBody;
00382 return $mail;
00383 }
00384
00385
00393 function ValidEmail($address)
00394 {
00395 if( ereg( ".*<(.+)>", $address, $regs ) ) {
00396 $address = $regs[1];
00397 }
00398 if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
00399 return true;
00400 else
00401 return false;
00402 }
00403
00404
00411 function CheckAdresses( $aad )
00412 {
00413 for($i=0;$i< count( $aad); $i++ ) {
00414 if( ! $this->ValidEmail( $aad[$i]) ) {
00415 echo "Class Mail, method Mail : invalid address $aad[$i]";
00416 exit;
00417 }
00418 }
00419 }
00420
00421
00427 function _build_attachement()
00428 {
00429
00430 $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
00431
00432 $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
00433 $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
00434 $this->body ."\n";
00435
00436 $sep= chr(13) . chr(10);
00437
00438 $ata= array();
00439 $k=0;
00440
00441
00442 for( $i=0; $i < count( $this->aattach); $i++ ) {
00443
00444 $filename = $this->aattach[$i];
00445 $basename = basename($filename);
00446 $ctype = $this->actype[$i];
00447 $disposition = $this->adispo[$i];
00448
00449 if( ! file_exists( $filename) ) {
00450 echo "Class Mail, method attach : file $filename can't be found"; exit;
00451 }
00452 $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
00453 "base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n";
00454 $ata[$k++] = $subhdr;
00455
00456 $linesz= filesize( $filename)+1;
00457 $fp= fopen( $filename, 'r' );
00458 $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
00459 fclose($fp);
00460 }
00461 $this->fullBody .= implode($sep, $ata);
00462 }
00463 }
00464 ?>