ILIAS  Release_5_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 /* 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, $a_add_prefix = false)
102  {
103  if($a_add_prefix)
104  {
105  // #9096
106  include_once "Services/Mail/classes/class.ilMail.php";
107  $prefix = ilMail::getSubjectPrefix();
108  if(trim($prefix))
109  {
110  $subject = trim($prefix)." ".$subject;
111  }
112  }
113  $this->xheaders['Subject'] = ilMimeMail::_mimeEncode(strtr($subject,"\r\n"," "));
114  }
115 
116 
122  function From($from )
123  {
124 
125  if( ! is_string($from) ) {
126  echo "Class Mail: error, From is not a string";
127  exit;
128  }
129 
130  // base64_encode fullname but not email
131 
132  $this->xheaders['From'] = $from;
133  }
134 
139  function ReplyTo( $address )
140  {
141 
142  if( ! is_string($address) )
143  return false;
144 
145  $this->xheaders["Reply-To"] = $address;
146 
147  }
148 
149 
156  function Receipt()
157  {
158  $this->receipt = 1;
159  }
160 
161 
167  function To( $to )
168  {
169 
170  // TODO : test validit� sur to
171  if( is_array( $to ) )
172  $this->sendto= $to;
173  else
174  $this->sendto[] = $to;
175 
176  if( $this->checkAddress == true )
177  $this->CheckAdresses( $this->sendto );
178 
179  }
180 
181 
188  function Cc($cc)
189  {
190  if( is_array($cc) )
191  $this->acc= $cc;
192  else
193  $this->acc[]= $cc;
194 
195  if( $this->checkAddress == true )
196  $this->CheckAdresses( $this->acc );
197 
198  }
199 
200 
201 
209  function Bcc( $bcc )
210  {
211  if( is_array($bcc) ) {
212  $this->abcc = $bcc;
213  } else {
214  $this->abcc[]= $bcc;
215  }
216 
217  if( $this->checkAddress == true )
218  $this->CheckAdresses( $this->abcc );
219  }
220 
221 
231  function Body( $body, $charset="" )
232  {
233  $this->body = $body;
234 
235  if( $charset != "" ) {
236  $this->charset = strtolower($charset);
237  if( $this->charset == "us-ascii" )
238  $this->ctencoding = "7bit";
239  }
240  }
241 
242 
248  function Organization( $org )
249  {
250  if( trim( $org != "" ) )
251  $this->xheaders['Organization'] = $org;
252  }
253 
254 
262  function Priority( $priority )
263  {
264  if( ! intval( $priority ) )
265  return false;
266 
267  if( ! isset( $this->priorities[$priority-1]) )
268  return false;
269 
270  $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
271 
272  return true;
273 
274  }
275 
276 
285  function Attach( $filename, $filetype = "", $disposition = "inline", $display_name = null)
286  {
287  // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
288  if( $filetype == "" )
289  $filetype = "application/octet-stream";
290 
291  $this->aattach[] = $filename;
292  $this->actype[] = $filetype;
293  $this->adispo[] = $disposition;
294  $this->adisplay[] = $display_name;
295  }
296 
301  function BuildMail()
302  {
303 
304  // build the headers
305  $this->headers = "";
306  // $this->xheaders['To'] = implode( ", ", $this->sendto );
307 
308  if( count($this->acc) > 0 )
309  $this->xheaders['CC'] = implode( ", ", $this->acc );
310 
311  if( count($this->abcc) > 0 )
312  $this->xheaders['BCC'] = implode( ", ", $this->abcc );
313 
314 
315  if( $this->receipt ) {
316  if( isset($this->xheaders["Reply-To"] ) )
317  $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
318  else
319  $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
320  }
321 
322  if( $this->charset != "" ) {
323  $this->xheaders["Mime-Version"] = "1.0";
324  $this->xheaders["Content-Type"] = "text/plain; charset=$this->charset";
325  $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
326  }
327 
328  $this->xheaders["X-Mailer"] = "Php/libMailv1.3";
329 
330  // include attached files
331  if( count( $this->aattach ) > 0 ) {
332  $this->_build_attachement();
333  } else {
334  $this->fullBody = $this->body;
335  }
336 
337  reset($this->xheaders);
338  while( list( $hdr,$value ) = each( $this->xheaders ) ) {
339  if( $hdr != "Subject" )
340  $this->headers .= "$hdr: $value\n";
341  }
342 
343 
344  }
345 
350  function Send()
351  {
352  #global $ilLog;
353  global $ilSetting;
354 
355  $this->BuildMail();
356 
357  $this->strTo = implode( ", ", $this->sendto );
358 
359  // envoie du mail
360  if(!(int)$ilSetting->get('prevent_smtp_globally'))
361  {
362  $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
363  }
364  #$ilLog->write($this->strTo.' '. $this->xheaders['Subject'].' '. $this->fullBody.' '. $this->headers);
365  }
366 
367 
368 
374  function Get()
375  {
376  $this->BuildMail();
377  $mail = "To: " . $this->strTo . "\n";
378  $mail .= $this->headers . "\n";
379  $mail .= $this->fullBody;
380  return $mail;
381  }
382 
383 
391  function ValidEmail($address)
392  {
393  if( ereg( ".*<(.+)>", $address, $regs ) ) {
394  $address = $regs[1];
395  }
396  if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
397  return true;
398  else
399  return false;
400  }
401 
402 
409  function CheckAdresses( $aad )
410  {
411  for($i=0;$i< count( $aad); $i++ ) {
412  if( ! $this->ValidEmail( $aad[$i]) ) {
413  echo "Class Mail, method Mail : invalid address $aad[$i]";
414  exit;
415  }
416  }
417  }
418 
419 
426  {
427 
428  $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
429 
430  $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
431  $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
432  $this->body ."\n";
433 
434  $sep= chr(13) . chr(10);
435 
436  $ata= array();
437  $k=0;
438 
439  // for each attached file, do...
440  for( $i=0; $i < count( $this->aattach); $i++ ) {
441 
442  $filename = $this->aattach[$i];
443  $basename = basename($filename);
444  $ctype = $this->actype[$i]; // content-type
445  $disposition = $this->adispo[$i];
446  $display_name = $this->adisplay[$i];
447  if(!$display_name)
448  {
449  $display_name = $basename;
450  }
451 
452  if( ! file_exists( $filename) ) {
453  echo "Class Mail, method attach : file $filename can't be found"; exit;
454  }
455  $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
456  "base64\nContent-Disposition: $disposition;\n filename=\"$display_name\"\n\n";
457  $ata[$k++] = $subhdr;
458  // non encoded line length
459  $linesz= filesize( $filename)+1;
460  $fp= fopen( $filename, 'r' );
461  $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
462  fclose($fp);
463  }
464  $this->fullBody .= implode($sep, $ata);
465  }
466 
467  function _mimeEncode($a_string)
468  {
469  $encoded = '=?utf-8?b?';
470  $encoded .= base64_encode($a_string);
471  $encoded .= '?=';
472 
473  return $encoded;
474  }
475 
476 } // class Mail
477 ?>