• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilMimeMail.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
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                 $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                 // TODO : test validité sur to
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                 // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
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                 // build the headers
00313                 $this->headers = "";
00314                 //      $this->xheaders['To'] = implode( ", ", $this->sendto );
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                 // include attached files
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                 #global $ilLog;
00361 
00362                 $this->BuildMail();
00363         
00364                 $this->strTo = implode( ", ", $this->sendto );
00365         
00366                 // envoie du mail
00367                 $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
00368                 #$ilLog->write($this->strTo.' '. $this->xheaders['Subject'].' '. $this->fullBody.' '. $this->headers);
00369         }
00370 
00371 
00372 
00378         function Get()
00379         {
00380                 $this->BuildMail();
00381                 $mail = "To: " . $this->strTo . "\n";
00382                 $mail .= $this->headers . "\n";
00383                 $mail .= $this->fullBody;
00384                 return $mail;
00385         }
00386 
00387 
00395         function ValidEmail($address)
00396         {
00397                 if( ereg( ".*<(.+)>", $address, $regs ) ) {
00398                         $address = $regs[1];
00399                 }
00400                 if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) ) 
00401                         return true;
00402                 else
00403                         return false;
00404         }
00405 
00406 
00413         function CheckAdresses( $aad )
00414         {
00415                 for($i=0;$i< count( $aad); $i++ ) {
00416                         if( ! $this->ValidEmail( $aad[$i]) ) {
00417                                 echo "Class Mail, method Mail : invalid address $aad[$i]";      
00418                                 exit;
00419                         }
00420                 }
00421         }
00422 
00423 
00429         function _build_attachement()
00430         {
00431 
00432                 $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
00433 
00434                 $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
00435                 $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
00436                         $this->body ."\n";
00437         
00438                 $sep= chr(13) . chr(10);
00439         
00440                 $ata= array();
00441                 $k=0;
00442         
00443                 // for each attached file, do...
00444                 for( $i=0; $i < count( $this->aattach); $i++ ) {
00445                 
00446                         $filename = $this->aattach[$i];
00447                         $basename = basename($filename);
00448                         $ctype = $this->actype[$i];     // content-type
00449                         $disposition = $this->adispo[$i];
00450                 
00451                         if( ! file_exists( $filename) ) {
00452                                 echo "Class Mail, method attach : file $filename can't be found"; exit;
00453                         }
00454                         $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
00455                                 "base64\nContent-Disposition: $disposition;\n  filename=\"$basename\"\n";
00456                         $ata[$k++] = $subhdr;
00457                         // non encoded line length
00458                         $linesz= filesize( $filename)+1;
00459                         $fp= fopen( $filename, 'r' );
00460                         $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
00461                         fclose($fp);
00462                 }
00463                 $this->fullBody .= implode($sep, $ata);
00464         }
00465 
00466         function _mimeEncode($a_string)
00467         {
00468                 $encoded = '=?utf-8?b?';
00469                 $encoded .= base64_encode($a_string);
00470                 $encoded .= '?=';
00471 
00472                 return $encoded;
00473         }
00474 
00475 } // class Mail
00476 ?>

Generated on Fri Dec 13 2013 11:57:54 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1