ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
94 function autoCheck($bool )
95 {
96 if( $bool )
97 {
98 $this->checkAddress = true;
99 }
100 else
101 {
102 $this->checkAddress = false;
103 }
104 }
105
110 function Subject($subject, $a_add_prefix = false)
111 {
112 if($a_add_prefix)
113 {
114 // #9096
115 include_once "Services/Mail/classes/class.ilMail.php";
116 $prefix = ilMail::getSubjectPrefix();
117 if(trim($prefix))
118 {
119 $subject = trim($prefix)." ".$subject;
120 }
121 }
122 $this->xheaders['Subject'] = $subject;
123 }
124
129 function From($from )
130 {
131 if( ! is_string($from) && !is_array($from)) {
132 echo "Class Mail: error, From is not a string or array";
133 exit;
134 }
135 if(is_array($from))
136 {
137 $this->xheaders['From'] = $from[0];
138 $this->xheaders['FromName'] = $from[1];
139 return;
140 }
141
142 $this->xheaders['From'] = $from;
143 }
144
149 function ReplyTo( $address )
150 {
151 if( ! is_string($address) )
152 {
153 return false;
154 }
155
156 $this->xheaders["Reply-To"] = $address;
157 }
158
164 function Receipt()
165 {
166 $this->receipt = 1;
167 }
168
173 function To( $to )
174 {
175 // TODO : test validit� sur to
176 if( is_array( $to ) )
177 {
178 $this->sendto= $to;
179 }
180 else
181 {
182 $this->sendto[] = $to;
183 }
184
185 if( $this->checkAddress == true )
186 {
187 $this->CheckAdresses( $this->sendto );
188 }
189 }
190
197 function Cc($cc)
198 {
199 if( is_array($cc) )
200 {
201 $this->acc= $cc;
202 }
203 else
204 {
205 $this->acc[]= $cc;
206 }
207
208 if( $this->checkAddress == true )
209 {
210 $this->CheckAdresses( $this->acc );
211 }
212 }
213
220 function Bcc( $bcc )
221 {
222 if( is_array($bcc) )
223 {
224 $this->abcc = $bcc;
225 }
226 else
227 {
228 $this->abcc[]= $bcc;
229 }
230
231 if( $this->checkAddress == true )
232 {
233 $this->CheckAdresses( $this->abcc );
234 }
235 }
236
246 function Body( $body, $charset="" )
247 {
248 $this->body = $body;
249
250 if( $charset != "" )
251 {
252 $this->charset = strtolower($charset);
253 if( $this->charset == "us-ascii" )
254 {
255 $this->ctencoding = "7bit";
256 }
257 }
258 }
259
265 function Organization( $org )
266 {
267 if( trim( $org != "" ) )
268 {
269 $this->xheaders['Organization'] = $org;
270 }
271 }
272
280 function Priority( $priority )
281 {
282 if( ! intval( $priority ) )
283 {
284 return false;
285 }
286
287 if( ! isset( $this->priorities[$priority-1]) )
288 {
289 return false;
290 }
291
292 $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
293
294 return true;
295 }
296
304 function Attach( $filename, $filetype = "", $disposition = "inline", $display_name = null)
305 {
306 // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
307 if( $filetype == "" )
308 {
309 $filetype = "application/octet-stream";
310 }
311
312 $this->aattach[] = $filename;
313 $this->actype[] = $filetype;
314 $this->adispo[] = $disposition;
315 $this->adisplay[] = $display_name;
316 }
317
322 protected function BuildMail()
323 {
329 global $ilUser, $ilSetting, $ilClientIniFile;
330
331 require_once 'libs/composer/vendor/autoload.php';
332 $mail = new PHPMailer();
333
334 if($ilSetting->get('mail_system_return_path', ''))
335 {
336 $mail->Sender = $ilSetting->get('mail_system_return_path', '');
337 }
338
339 require_once 'Services/Mail/classes/class.ilMail.php';
340 $addr = ilMail::getIliasMailerAddress();
341 if($this->xheaders['From'] == $addr[0])
342 {
343 $mail->setFrom($this->xheaders['From'], $this->xheaders['FromName']);
344 }
345 else
346 {
347 $mail->addReplyTo($this->xheaders['From'], $this->xheaders['FromName']);
348 $mail->setFrom($addr[0], $addr[1]);
349 }
350 foreach($this->sendto as $recipients)
351 {
352 $recipient_pieces = array_filter(array_map('trim', explode(',', $recipients)));
353 foreach($recipient_pieces as $recipient)
354 {
355 $mail->AddAddress($recipient, '');
356 }
357 }
358
359 foreach($this->acc as $carbon_copies)
360 {
361 $cc_pieces = array_filter(array_map('trim', explode(',', $carbon_copies)));
362 foreach($cc_pieces as $carbon_copy)
363 {
364 $mail->AddCC($carbon_copy, '');
365 }
366 }
367
368 foreach($this->abcc as $blind_carbon_copies)
369 {
370 $bcc_pieces = array_filter(array_map('trim', explode(',', $blind_carbon_copies)));
371 foreach($bcc_pieces as $blind_carbon_copy)
372 {
373 $mail->AddBCC($blind_carbon_copy, '');
374 }
375 }
376
377 $mail->CharSet = 'utf-8';
378 $mail->Subject = $this->xheaders['Subject'];
379
380 if($ilSetting->get('mail_send_html', 0))
381 {
382 $mail->IsHTML(true);
383
384 $skin = $ilClientIniFile->readVariable('layout', 'skin');
385
386 $bracket_path = './Services/Mail/templates/default/tpl.html_mail_template.html';
387 if($skin != 'default')
388 {
389 $tplpath = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
390
391 if(@file_exists($tplpath))
392 {
393 $bracket_path = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
394 }
395 }
396 $bracket = file_get_contents($bracket_path);
397
398 if(!$this->body)
399 {
400 $this->body = ' ';
401 }
402
403 $mail->AltBody = $this->body;
404
405 if(strip_tags($this->body, '<b><u><i><a>') == $this->body)
406 {
407 // Let's assume that there is no HTML, so convert "\n" to "<br>"
408 $this->body = nl2br($this->body);
409 }
410 $mail->Body = str_replace( '{PLACEHOLDER}', ilUtil::makeClickable( $this->body ), $bracket );
411
412 $directory = './Services/Mail/templates/default/img/';
413 if($skin != 'default')
414 {
415 if(is_dir('./Customizing/global/skin/' . $skin . '/Services/Mail/img'))
416 {
417 $directory = './Customizing/global/skin/' . $skin . '/Services/Mail/img/';
418 }
419 }
420 $directory_handle = @opendir($directory);
421 $files = array();
422 if($directory_handle)
423 {
424 while ($filename = @readdir($directory_handle))
425 {
426 $files[] = $filename;
427 }
428
429 $images = preg_grep ('/\.jpg$/i', $files);
430
431 foreach($images as $image)
432 {
433 $mail->AddEmbeddedImage($directory.$image, 'img/'.$image, $image);
434 }
435 }
436 }
437 else
438 {
439 $mail->IsHTML(false);
440 $mail->Body = $this->body;
441 }
442
443 $i = 0;
444 foreach($this->aattach as $attachment)
445 {
446 $name = '';
447 if(isset($this->adisplay[$i]) && strlen($this->adisplay[$i]) > 0)
448 {
449 $name = $this->adisplay[$i];
450 }
451
452 $mail->AddAttachment($attachment, $name);
453 ++$i;
454 }
455
456 ilLoggerFactory::getLogger('mail')->info(
457 "Trying to delegate external email delivery:" .
458 " Initiated by: " . $ilUser->getLogin() . " (" . $ilUser->getId() . ")" .
459 " | From: " . $this->xheaders['From'] .
460 " | To: " . implode(', ', $this->sendto) .
461 " | CC: " . implode(', ', $this->acc) .
462 " | BCC: " . implode(', ', $this->abcc) .
463 " | Subject: " .$mail->Subject
464 );
465
466 if(!(int)$ilSetting->get('prevent_smtp_globally'))
467 {
468 $result = $mail->Send();
469
470 if($result)
471 {
473 'Successfully delegated external mail delivery'
474 ));
475 }
476 else
477 {
479 'Could not deliver external email: %s', $mail->ErrorInfo
480 ));
481 }
482 }
483 else
484 {
486 'Suppressed delegation of email delivery according to global setting ( prevent_smtp_globally ).'
487 ));
488 }
489 }
490
495 function Send()
496 {
497 $this->BuildMail();
498 }
499
504 function Get()
505 {
506 $this->BuildMail();
507 $mail = "To: " . $this->strTo . "\n";
508 $mail .= $this->headers . "\n";
509 $mail .= $this->fullBody;
510 return $mail;
511 }
512
521 function ValidEmail($address)
522 {
523 $regs = array();
524 if(preg_match("/.*<(.+)>/", $address, $regs))
525 {
526 $address = $regs[1];
527 }
528
529 if(preg_match('/^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)$/', $address))
530 {
531 return true;
532 }
533 else
534 {
535 return false;
536 }
537 }
538
545 function CheckAdresses( $aad )
546 {
547 for($i=0;$i< count( $aad); $i++ ) {
548 if( ! $this->ValidEmail( $aad[$i]) )
549 {
550 echo "Class Mail, method Mail : invalid address $aad[$i]";
551 exit;
552 }
553 }
554 }
555
561 {
562 $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
563
564 $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
565 $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n".
566 $this->body ."\n";
567
568 $sep= chr(13) . chr(10);
569
570 $ata= array();
571 $k=0;
572
573 // for each attached file, do...
574 for( $i=0; $i < count( $this->aattach); $i++ ) {
575
576 $filename = $this->aattach[$i];
577 $basename = basename($filename);
578 $ctype = $this->actype[$i]; // content-type
579 $disposition = $this->adispo[$i];
580 $display_name = $this->adisplay[$i];
581 if(!$display_name)
582 {
583 $display_name = $basename;
584 }
585
586 if( ! file_exists( $filename) ) {
587 echo "Class Mail, method attach : file $filename can't be found";
588 exit;
589 }
590
591 $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding:".
592 "base64\nContent-Disposition: $disposition;\n filename=\"$display_name\"\n\n";
593 $ata[$k++] = $subhdr;
594 // non encoded line length
595 $linesz= filesize( $filename)+1;
596 $fp= fopen( $filename, 'r' );
597 $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
598 fclose($fp);
599 }
600
601 $this->fullBody .= implode($sep, $ata);
602 }
603
604 public static function _mimeEncode($a_string)
605 {
606 $encoded = '=?utf-8?b?';
607 $encoded .= base64_encode($a_string);
608 $encoded .= '?=';
609
610 return $encoded;
611 }
612}
sprintf('%.4f', $callTime)
$result
$files
Definition: add-vimline.php:18
An exception for terminatinating execution or to throw for unit testing.
static getLogger($a_component_id)
Get component logger.
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)
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...
static _mimeEncode($a_string)
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
PHPMailer - PHP email creation and transport class.
global $ilSetting
Definition: privfeed.php:17
$ilUser
Definition: imgupload.php:18