ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
4require_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
5
29{
34 var $sendto = array();
35
39 var $acc = array();
40
44 var $abcc = array();
45
50 protected $aattach = array();
51
55 protected $adisplay = array();
56
61 var $xheaders = array();
62
67 var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
68
73 var $charset = "utf-8";
74 var $ctencoding = "8bit";
75 var $receipt = 0;
76
80 public function __construct()
81 {
82 $this->autoCheck( false);
83 $this->boundary= "--" . md5( uniqid("myboundary") );
84 }
85
93 function autoCheck($bool )
94 {
95 if( $bool )
96 {
97 $this->checkAddress = true;
98 }
99 else
100 {
101 $this->checkAddress = false;
102 }
103 }
104
109 function Subject($subject, $a_add_prefix = false)
110 {
111 if($a_add_prefix)
112 {
113 // #9096
114 include_once "Services/Mail/classes/class.ilMail.php";
115 $prefix = ilMail::getSubjectPrefix();
116 if(trim($prefix))
117 {
118 $subject = trim($prefix)." ".$subject;
119 }
120 }
121 $this->xheaders['Subject'] = $subject;
122 }
123
128 function From($from )
129 {
130 if( ! is_string($from) && !is_array($from)) {
131 echo "Class Mail: error, From is not a string or array";
132 exit;
133 }
134 if(is_array($from))
135 {
136 $this->xheaders['From'] = $from[0];
137 $this->xheaders['FromName'] = $from[1];
138 return;
139 }
140
141 $this->xheaders['From'] = $from;
142 }
143
148 function ReplyTo( $address )
149 {
150 if( ! is_string($address) )
151 {
152 return false;
153 }
154
155 $this->xheaders["Reply-To"] = $address;
156 }
157
163 function Receipt()
164 {
165 $this->receipt = 1;
166 }
167
172 function To( $to )
173 {
174 // TODO : test validit� sur to
175 if( is_array( $to ) )
176 {
177 $this->sendto= $to;
178 }
179 else
180 {
181 $this->sendto[] = $to;
182 }
183
184 if( $this->checkAddress == true )
185 {
186 $this->CheckAdresses( $this->sendto );
187 }
188 }
189
196 function Cc($cc)
197 {
198 if( is_array($cc) )
199 {
200 $this->acc= $cc;
201 }
202 else
203 {
204 $this->acc[]= $cc;
205 }
206
207 if( $this->checkAddress == true )
208 {
209 $this->CheckAdresses( $this->acc );
210 }
211 }
212
219 function Bcc( $bcc )
220 {
221 if( is_array($bcc) )
222 {
223 $this->abcc = $bcc;
224 }
225 else
226 {
227 $this->abcc[]= $bcc;
228 }
229
230 if( $this->checkAddress == true )
231 {
232 $this->CheckAdresses( $this->abcc );
233 }
234 }
235
245 function Body( $body, $charset="" )
246 {
247 $this->body = $body;
248
249 if( $charset != "" )
250 {
251 $this->charset = strtolower($charset);
252 if( $this->charset == "us-ascii" )
253 {
254 $this->ctencoding = "7bit";
255 }
256 }
257 }
258
264 function Organization( $org )
265 {
266 if( trim( $org != "" ) )
267 {
268 $this->xheaders['Organization'] = $org;
269 }
270 }
271
279 function Priority( $priority )
280 {
281 if( ! intval( $priority ) )
282 {
283 return false;
284 }
285
286 if( ! isset( $this->priorities[$priority-1]) )
287 {
288 return false;
289 }
290
291 $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
292
293 return true;
294 }
295
303 function Attach( $filename, $filetype = "", $disposition = "inline", $display_name = null)
304 {
305 // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
306 if( $filetype == "" )
307 {
308 $filetype = "application/octet-stream";
309 }
310
311 $this->aattach[] = $filename;
312 $this->actype[] = $filetype;
313 $this->adispo[] = $disposition;
314 $this->adisplay[] = $display_name;
315 }
316
321 function BuildMail()
322 {
328 global $ilUser, $ilSetting, $ilClientIniFile;
329
330 require_once './Services/Mail/phpmailer/class.phpmailer.php';
331 $mail = new PHPMailer();
332
333 if($ilSetting->get('mail_system_return_path', ''))
334 {
335 $mail->Sender = $ilSetting->get('mail_system_return_path', '');
336 }
337
338 require_once 'Services/Mail/classes/class.ilMail.php';
340 if($this->xheaders['From'] == $addr[0])
341 {
342 $mail->setFrom($this->xheaders['From'], $this->xheaders['FromName']);
343 }
344 else
345 {
346 $mail->addReplyTo($this->xheaders['From'], $this->xheaders['FromName']);
347 $mail->setFrom($addr[0], $addr[1]);
348 }
349 foreach($this->sendto as $recipients)
350 {
351 $recipient_pieces = array_filter(array_map('trim', explode(',', $recipients)));
352 foreach($recipient_pieces as $recipient)
353 {
354 $mail->AddAddress($recipient, '');
355 }
356 }
357
358 foreach($this->acc as $carbon_copies)
359 {
360 $cc_pieces = array_filter(array_map('trim', explode(',', $carbon_copies)));
361 foreach($cc_pieces as $carbon_copy)
362 {
363 $mail->AddCC($carbon_copy, '');
364 }
365 }
366
367 foreach($this->abcc as $blind_carbon_copies)
368 {
369 $bcc_pieces = array_filter(array_map('trim', explode(',', $blind_carbon_copies)));
370 foreach($bcc_pieces as $blind_carbon_copy)
371 {
372 $mail->AddBCC($blind_carbon_copy, '');
373 }
374 }
375
376 $mail->CharSet = 'utf-8';
377 $mail->Subject = $this->xheaders['Subject'];
378
379 if($ilSetting->get('mail_send_html', 0))
380 {
381 $mail->IsHTML(true);
382
383 $skin = $ilClientIniFile->readVariable('layout', 'skin');
384
385 $bracket_path = './Services/Mail/templates/default/tpl.html_mail_template.html';
386 if($skin != 'default')
387 {
388 $tplpath = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
389
390 if(@file_exists($tplpath))
391 {
392 $bracket_path = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
393 }
394 }
395 $bracket = file_get_contents($bracket_path);
396
397 if(!$this->body)
398 {
399 $this->body = ' ';
400 }
401
402 $mail->AltBody = $this->body;
403
404 if(strip_tags($this->body, '<b><u><i><a>') == $this->body)
405 {
406 // Let's assume that there is no HTML, so convert "\n" to "<br>"
407 $this->body = nl2br($this->body);
408 }
409 $mail->Body = str_replace( '{PLACEHOLDER}', ilUtil::makeClickable( $this->body ), $bracket );
410
411 $directory = './Services/Mail/templates/default/img/';
412 if($skin != 'default')
413 {
414 if(is_dir('./Customizing/global/skin/' . $skin . '/Services/Mail/img'))
415 {
416 $directory = './Customizing/global/skin/' . $skin . '/Services/Mail/img/';
417 }
418 }
419 $directory_handle = @opendir($directory);
420 $files = array();
421 if($directory_handle)
422 {
423 while ($filename = @readdir($directory_handle))
424 {
425 $files[] = $filename;
426 }
427
428 $images = preg_grep ('/\.jpg$/i', $files);
429
430 foreach($images as $image)
431 {
432 $mail->AddEmbeddedImage($directory.$image, 'img/'.$image, $image);
433 }
434 }
435 }
436 else
437 {
438 $mail->IsHTML(false);
439 $mail->Body = $this->body;
440 }
441
442 $i = 0;
443 foreach($this->aattach as $attachment)
444 {
445 $name = '';
446 if(isset($this->adisplay[$i]) && strlen($this->adisplay[$i]) > 0)
447 {
448 $name = $this->adisplay[$i];
449 }
450
451 $mail->AddAttachment($attachment, $name);
452 ++$i;
453 }
454
455 ilLoggerFactory::getLogger('mail')->debug(
456 "Trying to delegate external email delivery:" .
457 " Initiated by: " . $ilUser->getLogin() . " (" . $ilUser->getId() . ")" .
458 " | From: " . $this->xheaders['From'] .
459 " | To: " . implode(', ', $this->sendto) .
460 " | CC: " . implode(', ', $this->acc) .
461 " | BCC: " . implode(', ', $this->abcc) .
462 " | Subject: " .$mail->Subject
463 );
464
465 if(!(int)$ilSetting->get('prevent_smtp_globally'))
466 {
467 $result = $mail->Send();
468
469 if($result)
470 {
471 ilLoggerFactory::getLogger('mail')->debug(sprintf(
472 'Successfully delegated external mail delivery'
473 ));
474 }
475 else
476 {
477 ilLoggerFactory::getLogger('mail')->debug(sprintf(
478 'Could not deliver external email: %s', $mail->ErrorInfo
479 ));
480 }
481 }
482 else
483 {
484 ilLoggerFactory::getLogger('mail')->debug(sprintf(
485 'Suppressed delegation of email delivery according to global setting ( prevent_smtp_globally ).'
486 ));
487 }
488 }
489
494 function Send()
495 {
496 $this->BuildMail();
497 }
498
503 function Get()
504 {
505 $this->BuildMail();
506 $mail = "To: " . $this->strTo . "\n";
507 $mail .= $this->headers . "\n";
508 $mail .= $this->fullBody;
509 return $mail;
510 }
511
519 function ValidEmail($address)
520 {
521 if( ereg( ".*<(.+)>", $address, $regs ) ) {
522 $address = $regs[1];
523 }
524 if(ereg( "^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
525 {
526 return true;
527 }
528 else
529 {
530 return false;
531 }
532 }
533
539 function CheckAdresses( $aad )
540 {
541 for($i=0;$i< count( $aad); $i++ ) {
542 if( ! $this->ValidEmail( $aad[$i]) )
543 {
544 echo "Class Mail, method Mail : invalid address $aad[$i]";
545 exit;
546 }
547 }
548 }
549
555 {
556 $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
557
558 $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
559 $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
560 $this->body ."\n";
561
562 $sep= chr(13) . chr(10);
563
564 $ata= array();
565 $k=0;
566
567 // for each attached file, do...
568 for( $i=0; $i < count( $this->aattach); $i++ ) {
569
570 $filename = $this->aattach[$i];
571 $basename = basename($filename);
572 $ctype = $this->actype[$i]; // content-type
573 $disposition = $this->adispo[$i];
574 $display_name = $this->adisplay[$i];
575 if(!$display_name)
576 {
577 $display_name = $basename;
578 }
579
580 if( ! file_exists( $filename) ) {
581 echo "Class Mail, method attach : file $filename can't be found";
582 exit;
583 }
584
585 $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
586 "base64\nContent-Disposition: $disposition;\n filename=\"$display_name\"\n\n";
587 $ata[$k++] = $subhdr;
588 // non encoded line length
589 $linesz= filesize( $filename)+1;
590 $fp= fopen( $filename, 'r' );
591 $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
592 fclose($fp);
593 }
594
595 $this->fullBody .= implode($sep, $ata);
596 }
597
598 function _mimeEncode($a_string)
599 {
600 $encoded = '=?utf-8?b?';
601 $encoded .= base64_encode($a_string);
602 $encoded .= '?=';
603
604 return $encoded;
605 }
606}
$result
$filename
Definition: buildRTE.php:89
static getLogger($a_component_id)
Get component logger.
static getIliasMailerAddress()
Builds an email address used for system notifications.
static getSubjectPrefix()
Get text that will be prepended to auto generated mails.
this class encapsulates the PHP mail() function.
CheckAdresses( $aad)
check validity of email addresses return if unvalid, output an error message and exit,...
ValidEmail($address)
check an email address validity @access public
_mimeEncode($a_string)
Cc($cc)
Cc() cc : email address(es), accept both array and string.
From($from)
set the sender of the mail
_build_attachement()
check and encode attach file(s) .
Bcc( $bcc)
Bcc() set the Bcc headers ( blank carbon copy ).
Get()
return the whole e-mail , headers + message can be used for displaying the message in plain text or l...
autoCheck($bool)
activate or desactivate the email addresses validator ex: autoCheck( true ) turn the validator on by ...
Attach( $filename, $filetype="", $disposition="inline", $display_name=null)
Attach a file to the mail.
Send()
fornat and send the mail @access public
ReplyTo( $address)
set the Reply-to header
Subject($subject, $a_add_prefix=false)
Define the subject line of the email.
__construct()
Mail contructor.
Organization( $org)
Organization( $org ) set the Organization header.
Body( $body, $charset="")
Body( text [, charset] ) set the body (message) of the mail define the charset if the message contain...
Receipt()
add a receipt to the mail ie.
To( $to)
set the mail recipient
Priority( $priority)
Priority( $priority ) set the mail priority $priority : integer taken between 1 (highest) and 5 ( low...
static makeClickable($a_text, $detectGotoLinks=false)
makeClickable In Texten enthaltene URLs und Mail-Adressen klickbar machen
exit
Definition: login.php:54
PHPMailer - PHP email creation and transport class.
global $ilSetting
Definition: privfeed.php:40
global $ilUser
Definition: imgupload.php:15