ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMimeMail.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
27 {
32  var $sendto = array();
33 
37  var $acc = array();
38 
42  var $abcc = array();
43 
48  var $aattach = array();
49 
54  var $xheaders = array();
55 
60  var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
61 
66  var $charset = "utf-8";
67  var $ctencoding = "8bit";
68  var $receipt = 0;
69 
70 
74  public function __construct()
75  {
76  $this->autoCheck( false);
77  $this->boundary= "--" . md5( uniqid("myboundary") );
78  }
79 
80 
88  function autoCheck($bool )
89  {
90  if( $bool )
91  $this->checkAddress = true;
92  else
93  $this->checkAddress = false;
94  }
95 
96 
101  function Subject($subject )
102  {
103  $this->xheaders['Subject'] = ilMimeMail::_mimeEncode(strtr($subject,"\r\n"," "));
104  }
105 
106 
112  function From($from )
113  {
114 
115  if( ! is_string($from) ) {
116  echo "Class Mail: error, From is not a string";
117  exit;
118  }
119 
120  // base64_encode fullname but not email
121 
122  $this->xheaders['From'] = $from;
123  }
124 
129  function ReplyTo( $address )
130  {
131 
132  if( ! is_string($address) )
133  return false;
134 
135  $this->xheaders["Reply-To"] = $address;
136 
137  }
138 
139 
146  function Receipt()
147  {
148  $this->receipt = 1;
149  }
150 
151 
157  function To( $to )
158  {
159 
160  // TODO : test validit� sur to
161  if( is_array( $to ) )
162  $this->sendto= $to;
163  else
164  $this->sendto[] = $to;
165 
166  if( $this->checkAddress == true )
167  $this->CheckAdresses( $this->sendto );
168 
169  }
170 
171 
178  function Cc($cc)
179  {
180  if( is_array($cc) )
181  $this->acc= $cc;
182  else
183  $this->acc[]= $cc;
184 
185  if( $this->checkAddress == true )
186  $this->CheckAdresses( $this->acc );
187 
188  }
189 
190 
191 
199  function Bcc( $bcc )
200  {
201  if( is_array($bcc) ) {
202  $this->abcc = $bcc;
203  } else {
204  $this->abcc[]= $bcc;
205  }
206 
207  if( $this->checkAddress == true )
208  $this->CheckAdresses( $this->abcc );
209  }
210 
211 
221  function Body( $body, $charset="" )
222  {
223  $this->body = $body;
224 
225  if( $charset != "" ) {
226  $this->charset = strtolower($charset);
227  if( $this->charset == "us-ascii" )
228  $this->ctencoding = "7bit";
229  }
230  }
231 
232 
238  function Organization( $org )
239  {
240  if( trim( $org != "" ) )
241  $this->xheaders['Organization'] = $org;
242  }
243 
244 
252  function Priority( $priority )
253  {
254  if( ! intval( $priority ) )
255  return false;
256 
257  if( ! isset( $this->priorities[$priority-1]) )
258  return false;
259 
260  $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
261 
262  return true;
263 
264  }
265 
266 
275  function Attach( $filename, $filetype = "", $disposition = "inline", $display_name = null)
276  {
277  // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
278  if( $filetype == "" )
279  $filetype = "application/octet-stream";
280 
281  $this->aattach[] = $filename;
282  $this->actype[] = $filetype;
283  $this->adispo[] = $disposition;
284  $this->adisplay[] = $display_name;
285  }
286 
291  function BuildMail()
292  {
293 
294  // build the headers
295  $this->headers = "";
296  // $this->xheaders['To'] = implode( ", ", $this->sendto );
297 
298  if( count($this->acc) > 0 )
299  $this->xheaders['CC'] = implode( ", ", $this->acc );
300 
301  if( count($this->abcc) > 0 )
302  $this->xheaders['BCC'] = implode( ", ", $this->abcc );
303 
304 
305  if( $this->receipt ) {
306  if( isset($this->xheaders["Reply-To"] ) )
307  $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
308  else
309  $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
310  }
311 
312  if( $this->charset != "" ) {
313  $this->xheaders["Mime-Version"] = "1.0";
314  $this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
315  $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
316  }
317 
318  $this->xheaders["X-Mailer"] = "Php/libMailv1.3";
319 
320  // include attached files
321  if( count( $this->aattach ) > 0 ) {
322  $this->_build_attachement();
323  } else {
324  $this->fullBody = $this->body;
325  }
326 
327  reset($this->xheaders);
328  while( list( $hdr,$value ) = each( $this->xheaders ) ) {
329  if( $hdr != "Subject" )
330  $this->headers .= "$hdr: $value\n";
331  }
332 
333 
334  }
335 
340  function Send()
341  {
342  #global $ilLog;
343  global $ilSetting;
344 
345  $this->BuildMail();
346 
347  $this->strTo = implode( ", ", $this->sendto );
348 
349  // envoie du mail
350  if(!(int)$ilSetting->get('prevent_smtp_globally'))
351  {
352  $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
353  }
354  #$ilLog->write($this->strTo.' '. $this->xheaders['Subject'].' '. $this->fullBody.' '. $this->headers);
355  }
356 
357 
358 
364  function Get()
365  {
366  $this->BuildMail();
367  $mail = "To: " . $this->strTo . "\n";
368  $mail .= $this->headers . "\n";
369  $mail .= $this->fullBody;
370  return $mail;
371  }
372 
373 
381  function ValidEmail($address)
382  {
383  if( ereg( ".*<(.+)>", $address, $regs ) ) {
384  $address = $regs[1];
385  }
386  if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
387  return true;
388  else
389  return false;
390  }
391 
392 
399  function CheckAdresses( $aad )
400  {
401  for($i=0;$i< count( $aad); $i++ ) {
402  if( ! $this->ValidEmail( $aad[$i]) ) {
403  echo "Class Mail, method Mail : invalid address $aad[$i]";
404  exit;
405  }
406  }
407  }
408 
409 
416  {
417 
418  $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
419 
420  $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
421  $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
422  $this->body ."\n";
423 
424  $sep= chr(13) . chr(10);
425 
426  $ata= array();
427  $k=0;
428 
429  // for each attached file, do...
430  for( $i=0; $i < count( $this->aattach); $i++ ) {
431 
432  $filename = $this->aattach[$i];
433  $basename = basename($filename);
434  $ctype = $this->actype[$i]; // content-type
435  $disposition = $this->adispo[$i];
436  $display_name = $this->adisplay[$i];
437  if(!$display_name)
438  {
439  $display_name = $basename;
440  }
441 
442  if( ! file_exists( $filename) ) {
443  echo "Class Mail, method attach : file $filename can't be found"; exit;
444  }
445  $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
446  "base64\nContent-Disposition: $disposition;\n filename=\"$display_name\"\n\n";
447  $ata[$k++] = $subhdr;
448  // non encoded line length
449  $linesz= filesize( $filename)+1;
450  $fp= fopen( $filename, 'r' );
451  $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
452  fclose($fp);
453  }
454  $this->fullBody .= implode($sep, $ata);
455  }
456 
457  function _mimeEncode($a_string)
458  {
459  $encoded = '=?utf-8?b?';
460  $encoded .= base64_encode($a_string);
461  $encoded .= '?=';
462 
463  return $encoded;
464  }
465 
466 } // class Mail
467 ?>