ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMimeMail.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
48 {
53  var $sendto = array();
54 
58  var $acc = array();
59 
63  var $abcc = array();
64 
69  var $aattach = array();
70 
75  var $xheaders = array();
76 
81  var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
82 
87  var $charset = "utf-8";
88  var $ctencoding = "8bit";
89  var $receipt = 0;
90 
91 
96  function ilMimeMail()
97  {
98  $this->autoCheck( false);
99  $this->boundary= "--" . md5( uniqid("myboundary") );
100  }
101 
102 
110  function autoCheck($bool )
111  {
112  if( $bool )
113  $this->checkAddress = true;
114  else
115  $this->checkAddress = false;
116  }
117 
118 
123  function Subject($subject )
124  {
125  $this->xheaders['Subject'] = ilMimeMail::_mimeEncode(strtr($subject,"\r\n"," "));
126  }
127 
128 
134  function From($from )
135  {
136 
137  if( ! is_string($from) ) {
138  echo "Class Mail: error, From is not a string";
139  exit;
140  }
141 
142  // base64_encode fullname but not email
143 
144  $this->xheaders['From'] = $from;
145  }
146 
151  function ReplyTo( $address )
152  {
153 
154  if( ! is_string($address) )
155  return false;
156 
157  $this->xheaders["Reply-To"] = $address;
158 
159  }
160 
161 
168  function Receipt()
169  {
170  $this->receipt = 1;
171  }
172 
173 
179  function To( $to )
180  {
181 
182  // TODO : test validit� sur to
183  if( is_array( $to ) )
184  $this->sendto= $to;
185  else
186  $this->sendto[] = $to;
187 
188  if( $this->checkAddress == true )
189  $this->CheckAdresses( $this->sendto );
190 
191  }
192 
193 
200  function Cc($cc)
201  {
202  if( is_array($cc) )
203  $this->acc= $cc;
204  else
205  $this->acc[]= $cc;
206 
207  if( $this->checkAddress == true )
208  $this->CheckAdresses( $this->acc );
209 
210  }
211 
212 
213 
221  function Bcc( $bcc )
222  {
223  if( is_array($bcc) ) {
224  $this->abcc = $bcc;
225  } else {
226  $this->abcc[]= $bcc;
227  }
228 
229  if( $this->checkAddress == true )
230  $this->CheckAdresses( $this->abcc );
231  }
232 
233 
243  function Body( $body, $charset="" )
244  {
245  $this->body = $body;
246 
247  if( $charset != "" ) {
248  $this->charset = strtolower($charset);
249  if( $this->charset == "us-ascii" )
250  $this->ctencoding = "7bit";
251  }
252  }
253 
254 
260  function Organization( $org )
261  {
262  if( trim( $org != "" ) )
263  $this->xheaders['Organization'] = $org;
264  }
265 
266 
274  function Priority( $priority )
275  {
276  if( ! intval( $priority ) )
277  return false;
278 
279  if( ! isset( $this->priorities[$priority-1]) )
280  return false;
281 
282  $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
283 
284  return true;
285 
286  }
287 
288 
296  function Attach( $filename, $filetype = "", $disposition = "inline" )
297  {
298  // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
299  if( $filetype == "" )
300  $filetype = "application/octet-stream";
301 
302  $this->aattach[] = $filename;
303  $this->actype[] = $filetype;
304  $this->adispo[] = $disposition;
305  }
306 
311  function BuildMail()
312  {
313 
314  // build the headers
315  $this->headers = "";
316  // $this->xheaders['To'] = implode( ", ", $this->sendto );
317 
318  if( count($this->acc) > 0 )
319  $this->xheaders['CC'] = implode( ", ", $this->acc );
320 
321  if( count($this->abcc) > 0 )
322  $this->xheaders['BCC'] = implode( ", ", $this->abcc );
323 
324 
325  if( $this->receipt ) {
326  if( isset($this->xheaders["Reply-To"] ) )
327  $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
328  else
329  $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
330  }
331 
332  if( $this->charset != "" ) {
333  $this->xheaders["Mime-Version"] = "1.0";
334  $this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
335  $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
336  }
337 
338  $this->xheaders["X-Mailer"] = "Php/libMailv1.3";
339 
340  // include attached files
341  if( count( $this->aattach ) > 0 ) {
342  $this->_build_attachement();
343  } else {
344  $this->fullBody = $this->body;
345  }
346 
347  reset($this->xheaders);
348  while( list( $hdr,$value ) = each( $this->xheaders ) ) {
349  if( $hdr != "Subject" )
350  $this->headers .= "$hdr: $value\n";
351  }
352 
353 
354  }
355 
360  function Send()
361  {
362  #global $ilLog;
363 
364  $this->BuildMail();
365 
366  $this->strTo = implode( ", ", $this->sendto );
367 
368  // envoie du mail
369  $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
370  #$ilLog->write($this->strTo.' '. $this->xheaders['Subject'].' '. $this->fullBody.' '. $this->headers);
371  }
372 
373 
374 
380  function Get()
381  {
382  $this->BuildMail();
383  $mail = "To: " . $this->strTo . "\n";
384  $mail .= $this->headers . "\n";
385  $mail .= $this->fullBody;
386  return $mail;
387  }
388 
389 
398  {
399  if( ereg( ".*<(.+)>", $address, $regs ) ) {
400  $address = $regs[1];
401  }
402  if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
403  return true;
404  else
405  return false;
406  }
407 
408 
415  function CheckAdresses( $aad )
416  {
417  for($i=0;$i< count( $aad); $i++ ) {
418  if( ! $this->ValidEmail( $aad[$i]) ) {
419  echo "Class Mail, method Mail : invalid address $aad[$i]";
420  exit;
421  }
422  }
423  }
424 
425 
432  {
433 
434  $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
435 
436  $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
437  $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
438  $this->body ."\n";
439 
440  $sep= chr(13) . chr(10);
441 
442  $ata= array();
443  $k=0;
444 
445  // for each attached file, do...
446  for( $i=0; $i < count( $this->aattach); $i++ ) {
447 
448  $filename = $this->aattach[$i];
449  $basename = basename($filename);
450  $ctype = $this->actype[$i]; // content-type
451  $disposition = $this->adispo[$i];
452 
453  if( ! file_exists( $filename) ) {
454  echo "Class Mail, method attach : file $filename can't be found"; exit;
455  }
456  $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
457  "base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n\n";
458  $ata[$k++] = $subhdr;
459  // non encoded line length
460  $linesz= filesize( $filename)+1;
461  $fp= fopen( $filename, 'r' );
462  $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
463  fclose($fp);
464  }
465  $this->fullBody .= implode($sep, $ata);
466  }
467 
468  function _mimeEncode($a_string)
469  {
470  $encoded = '=?utf-8?b?';
471  $encoded .= base64_encode($a_string);
472  $encoded .= '?=';
473 
474  return $encoded;
475  }
476 
477 } // class Mail
478 ?>