ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules 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 
7 class ilMimeMail
8 {
9  public const MAIL_SUBJECT_PREFIX = '[ILIAS]';
10 
12  protected static $defaultTransport = null;
13 
15  protected $subject = '';
16 
18  protected $body = '';
19 
21  protected $finalBody = '';
22 
24  protected $finalBodyAlt = '';
25 
27  protected $sendto = [];
28 
30  protected $acc = [];
31 
33  protected $abcc = [];
34 
36  protected $images = [];
37 
39  protected $aattach = [];
40 
42  protected $actype = [];
43 
45  protected $adispo = [];
46 
48  protected $adisplay = [];
49 
51  protected $sender;
52 
54  protected $settings;
55 
59  public function __construct()
60  {
61  global $DIC;
62 
63  $this->settings = $DIC->settings();
64 
65  if (!(self::getDefaultTransport() instanceof ilMailMimeTransport)) {
66  $factory = $DIC["mail.mime.transport.factory"];
67  self::setDefaultTransport($factory->getTransport());
68  }
69  }
70 
75  public static function setDefaultTransport(?ilMailMimeTransport $transport) : void
76  {
77  if ($transport !== null && !($transport instanceof ilMailMimeTransport)) {
78  throw new InvalidArgumentException(sprintf(
79  "The passed argument must be null or of type 'ilMailMimeTransport', %s given!",
80  gettype($transport)
81  ));
82  }
83 
84  self::$defaultTransport = $transport;
85  }
86 
87  public static function getDefaultTransport() : ?ilMailMimeTransport
88  {
89  return self::$defaultTransport;
90  }
91 
92  public function Subject(string $subject, bool $a_add_prefix = false) : void
93  {
94  if ($a_add_prefix) {
95  // #9096
96  $subjectPrefix = $this->settings->get('mail_subject_prefix');
97  if (false === $subjectPrefix) {
98  $subjectPrefix = self::MAIL_SUBJECT_PREFIX;
99  }
100  if ($subjectPrefix !== '') {
101  $subject = $subjectPrefix . ' ' . $subject;
102  }
103  }
104 
105  $this->subject = $subject;
106  }
107 
108  public function getSubject() : string
109  {
110  return $this->subject;
111  }
112 
113  public function From(ilMailMimeSender $sender) : void
114  {
115  $this->sender = $sender;
116  }
117 
122  public function To($to) : void
123  {
124  if (is_array($to)) {
125  $this->sendto = $to;
126  } else {
127  $this->sendto[] = $to;
128  }
129  }
130 
135  public function Cc($cc) : void
136  {
137  if (is_array($cc)) {
138  $this->acc = $cc;
139  } else {
140  $this->acc[] = $cc;
141  }
142  }
143 
148  public function Bcc($bcc) : void
149  {
150  if (is_array($bcc)) {
151  $this->abcc = $bcc;
152  } else {
153  $this->abcc[] = $bcc;
154  }
155  }
156 
160  public function getTo() : array
161  {
162  return $this->sendto;
163  }
164 
168  public function getCc() : array
169  {
170  return $this->acc;
171  }
172 
176  public function getBcc() : array
177  {
178  return $this->abcc;
179  }
180 
181  public function Body(string $body) : void
182  {
183  $this->body = $body;
184  }
185 
186  public function getFinalBody() : string
187  {
188  return $this->finalBody;
189  }
190 
191  public function getFinalBodyAlt() : string
192  {
193  return $this->finalBodyAlt;
194  }
195 
196  public function getFrom() : ilMailMimeSender
197  {
198  return $this->sender;
199  }
200 
208  public function Attach(
209  string $filename,
210  string $file_type = '',
211  string $disposition = 'inline',
212  ?string $display_name = null
213  ) : void {
214  if ($file_type === '') {
215  $file_type = 'application/octet-stream';
216  }
217 
218  $this->aattach[] = $filename;
219  $this->actype[] = $file_type;
220  $this->adispo[] = $disposition;
221  $this->adisplay[] = $display_name;
222  }
223 
227  public function getAttachments() : array
228  {
229  $attachments = [];
230 
231  $i = 0;
232  foreach ($this->aattach as $attachment) {
233  $name = '';
234  if (isset($this->adisplay[$i]) && is_string($this->adisplay[$i]) && $this->adisplay[$i] !== '') {
235  $name = $this->adisplay[$i];
236  }
237 
238  $attachments[] = [
239  'path' => $attachment,
240  'name' => $name
241  ];
242  ++$i;
243  }
244 
245  return $attachments;
246  }
247 
251  public function getImages() : array
252  {
253  return array_values($this->images);
254  }
255 
259  protected function build() : void
260  {
261  global $DIC;
262 
263  $this->finalBodyAlt = '';
264  $this->finalBody = '';
265  $this->images = [];
266 
267  if ($DIC->settings()->get('mail_send_html', 0)) {
268  $skin = $DIC['ilClientIniFile']->readVariable('layout', 'skin');
269 
270  $this->buildBodyMultiParts($skin);
271  $this->buildHtmlInlineImages($skin);
272  } else {
273  $this->finalBody = str_ireplace(["<br />", "<br>", "<br/>"], "\n", $this->body);
274  }
275  }
276 
277  protected function buildBodyMultiParts(string $skin) : void
278  {
279  if ($this->body === '') {
280  $this->body = ' ';
281  }
282 
283  if (strip_tags($this->body, '<b><u><i><a>') === $this->body) {
284  // Let's assume(!) that there is no HTML (except certain tags, e.g. used for object title formatting, where the consumer is not aware of this), so convert "\n" to "<br>"
285  $this->finalBodyAlt = $this->body;
286  $this->body = ilUtil::makeClickable(nl2br($this->body));
287  } else {
288  // if there is HTML, convert "<br>" to "\n" and strip tags for plain text alternative
289  $this->finalBodyAlt = strip_tags(str_ireplace(["<br />", "<br>", "<br/>"], "\n", $this->body));
290  }
291 
292  $this->finalBody = str_replace('{PLACEHOLDER}', $this->body, $this->getHtmlEnvelope($skin));
293  }
294 
295  protected function getHtmlEnvelope(string $skin) : string
296  {
297  $bracket_path = './Services/Mail/templates/default/tpl.html_mail_template.html';
298 
299  if ($skin !== 'default') {
300  $tplpath = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
301 
302  if (file_exists($tplpath)) {
303  $bracket_path = './Customizing/global/skin/' . $skin . '/Services/Mail/tpl.html_mail_template.html';
304  }
305  }
306 
307  return file_get_contents($bracket_path);
308  }
309 
310  protected function buildHtmlInlineImages(string $skin) : void
311  {
312  $this->gatherImagesFromDirectory('./Services/Mail/templates/default/img');
313 
314  if ($skin !== 'default') {
315  $skinDirectory = './Customizing/global/skin/' . $skin . '/Services/Mail/img';
316  if (is_dir($skinDirectory) && is_readable($skinDirectory)) {
317  $this->gatherImagesFromDirectory($skinDirectory, true);
318  }
319  }
320  }
321 
322  protected function gatherImagesFromDirectory(string $directory, bool $clearPrevious = false) : void
323  {
324  if ($clearPrevious) {
325  $this->images = [];
326  }
327 
328  foreach (new RegexIterator(new DirectoryIterator($directory), '/\.(jpg|svg|png)$/i') as $file) {
330  $cid = 'img/' . $file->getFilename();
331 
332  $this->images[$cid] = [
333  'path' => $file->getPathname(),
334  'cid' => $cid,
335  'name' => $file->getFilename()
336  ];
337  }
338  }
339 
344  public function Send(ilMailMimeTransport $transport = null) : bool
345  {
346  if (!($transport instanceof ilMailMimeTransport)) {
347  $transport = self::getDefaultTransport();
348  }
349 
350  $this->build();
351 
352  return $transport->send($this);
353  }
354 }
buildHtmlInlineImages(string $skin)
settings()
Definition: settings.php:2
Subject(string $subject, bool $a_add_prefix=false)
getHtmlEnvelope(string $skin)
Send(ilMailMimeTransport $transport=null)
Cc($cc)
Set the cc mail recipient.
static setDefaultTransport(?ilMailMimeTransport $transport)
__construct()
ilMimeMail constructor.
Bcc($bcc)
Set the bcc mail recipient.
static getDefaultTransport()
if($format !==null) $name
Definition: metadata.php:230
Interface ilMailMimeTransport.
Class ilMimeMail.
Interface ilMailMimeTransport.
buildBodyMultiParts(string $skin)
static makeClickable($a_text, $detectGotoLinks=false)
makeClickable In Texten enthaltene URLs und Mail-Adressen klickbar machen
build()
Build the relevant email data.
To($to)
Set the mail recipient.
$filename
Definition: buildRTE.php:89
const MAIL_SUBJECT_PREFIX
$DIC
Definition: xapitoken.php:46
From(ilMailMimeSender $sender)
static $defaultTransport
$factory
Definition: metadata.php:58
Attach(string $filename, string $file_type='', string $disposition='inline', ?string $display_name=null)
Attach a file to the mail.
Body(string $body)
$i
Definition: metadata.php:24