ILIAS  Release_4_2_x_branch Revision 61807
 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  global $ilSetting;
364 
365  $this->BuildMail();
366 
367  $this->strTo = implode( ", ", $this->sendto );
368 
369  // envoie du mail
370  if(!(int)$ilSetting->get('prevent_smtp_globally'))
371  {
372  $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
373  }
374  #$ilLog->write($this->strTo.' '. $this->xheaders['Subject'].' '. $this->fullBody.' '. $this->headers);
375  }
376 
377 
378 
384  function Get()
385  {
386  $this->BuildMail();
387  $mail = "To: " . $this->strTo . "\n";
388  $mail .= $this->headers . "\n";
389  $mail .= $this->fullBody;
390  return $mail;
391  }
392 
393 
401  function ValidEmail($address)
402  {
403  if( ereg( ".*<(.+)>", $address, $regs ) ) {
404  $address = $regs[1];
405  }
406  if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
407  return true;
408  else
409  return false;
410  }
411 
412 
419  function CheckAdresses( $aad )
420  {
421  for($i=0;$i< count( $aad); $i++ ) {
422  if( ! $this->ValidEmail( $aad[$i]) ) {
423  echo "Class Mail, method Mail : invalid address $aad[$i]";
424  exit;
425  }
426  }
427  }
428 
429 
436  {
437 
438  $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
439 
440  $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
441  $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
442  $this->body ."\n";
443 
444  $sep= chr(13) . chr(10);
445 
446  $ata= array();
447  $k=0;
448 
449  // for each attached file, do...
450  for( $i=0; $i < count( $this->aattach); $i++ ) {
451 
452  $filename = $this->aattach[$i];
453  $basename = basename($filename);
454  $ctype = $this->actype[$i]; // content-type
455  $disposition = $this->adispo[$i];
456 
457  if( ! file_exists( $filename) ) {
458  echo "Class Mail, method attach : file $filename can't be found"; exit;
459  }
460  $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
461  "base64\nContent-Disposition: $disposition;\n filename=\"$basename\"\n\n";
462  $ata[$k++] = $subhdr;
463  // non encoded line length
464  $linesz= filesize( $filename)+1;
465  $fp= fopen( $filename, 'r' );
466  $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
467  fclose($fp);
468  }
469  $this->fullBody .= implode($sep, $ata);
470  }
471 
472  function _mimeEncode($a_string)
473  {
474  $encoded = '=?utf-8?b?';
475  $encoded .= base64_encode($a_string);
476  $encoded .= '?=';
477 
478  return $encoded;
479  }
480 
481 } // class Mail
482 ?>