ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMail.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/User/classes/class.ilObjUser.php';
5 require_once 'Services/Mail/exceptions/class.ilMailException.php';
6 
11 class ilMail
12 {
14  const ILIAS_HOST = 'ilias';
15 
16  const MAIL_SUBJECT_PREFIX = '[ILIAS]';
17 
19  protected $lng;
20 
22  protected $db;
23 
25  protected $mfile;
26 
28  protected $mail_options;
29 
31  protected $mailbox;
32 
34  public $user_id;
35 
37  protected $table_mail;
38 
40  protected $table_mail_saved;
41 
43  protected $mail_data = array();
44 
46  protected $mail_obj_ref_id;
47 
49  protected $save_in_sentbox;
50 
51  protected $soap_enabled = true;
52  protected $appendInstallationSignature = false;
53 
55  private $eventHandler;
56 
61  protected $properties = array();
62 
64  protected static $userInstances = array();
65 
68 
71 
73  protected $contextId = null;
74 
78  protected $contextParameters = [];
79 
86  public function __construct(
87  $a_user_id,
91  ) {
92  global $DIC;
93 
94  require_once 'Services/Mail/classes/class.ilFileDataMail.php';
95  require_once 'Services/Mail/classes/class.ilMailOptions.php';
96 
97  if ($mailAddressTypeFactory === null) {
99  }
100 
101  if ($mailAddressParserFactory === null) {
103  }
104 
105  if ($eventHandler === null) {
106  $eventHandler = $DIC->event();
107  }
108 
109  $this->mailAddressParserFactory = $mailAddressParserFactory;
110  $this->mailAddressTypeFactory = $mailAddressTypeFactory;
111  $this->eventHandler = $eventHandler;
112 
113  $this->lng = $DIC->language();
114  $this->db = $DIC->database();
115 
116  $this->lng->loadLanguageModule('mail');
117 
118  $this->table_mail = 'mail';
119  $this->table_mail_saved = 'mail_saved';
120 
121  $this->user_id = $a_user_id;
122 
123  $this->mfile = new ilFileDataMail($this->user_id);
124  $this->mail_options = new ilMailOptions($a_user_id);
125  $this->mailbox = new ilMailbox($this->user_id);
126 
127  $this->setSaveInSentbox(false);
128  $this->readMailObjectReferenceId();
129  }
130 
135  public function withContextId(string $contextId) : self
136  {
137  $clone = clone $this;
138 
139  $clone->contextId = $contextId;
140 
141  return $clone;
142  }
143 
148  public function withContextParameters(array $parameters) : self
149  {
150  $clone = clone $this;
151 
152  $clone->contextParameters = $parameters;
153 
154  return $clone;
155  }
156 
160  protected function isSystemMail()
161  {
162  return $this->user_id == ANONYMOUS_USER_ID;
163  }
164 
171  public function __get($name)
172  {
173  global $DIC;
174 
175  if (isset($this->properties[$name])) {
176  return $this->properties[$name];
177  }
178 
179  if ($name == 'mlists') {
180  if (is_object($DIC->user())) {
181  require_once 'Services/Contact/classes/class.ilMailingLists.php';
182  $this->properties[$name] = new ilMailingLists($DIC->user());
183  return $this->properties[$name];
184  }
185  }
186  }
187 
193  public function existsRecipient(string $newRecipient, string $existingRecipients) : bool
194  {
195  $newAddresses = new \ilMailAddressListImpl($this->parseAddresses($newRecipient));
196  $addresses = new \ilMailAddressListImpl($this->parseAddresses($existingRecipients));
197 
198  $list = new \ilMailDiffAddressList($newAddresses, $addresses);
199 
200  $diffedAddresses = $list->value();
201 
202  return count($diffedAddresses) === 0;
203  }
204 
210  public function enableSOAP($a_status)
211  {
212  $this->soap_enabled = (bool) $a_status;
213  }
214 
218  public function isSOAPEnabled()
219  {
220  global $DIC;
221 
222  if (!extension_loaded('curl') || !$DIC->settings()->get('soap_user_administration')) {
223  return false;
224  }
225 
227  return false;
228  }
229 
230  return (bool) $this->soap_enabled;
231  }
232 
236  public function setSaveInSentbox($a_save_in_sentbox)
237  {
238  $this->save_in_sentbox = (bool) $a_save_in_sentbox;
239  }
240 
244  public function getSaveInSentbox()
245  {
246  return (bool) $this->save_in_sentbox;
247  }
248 
252  protected function readMailObjectReferenceId()
253  {
254  require_once 'Services/Mail/classes/class.ilMailGlobalServices.php';
255  $this->mail_obj_ref_id = ilMailGlobalServices::getMailObjectRefId();
256  }
257 
261  public function getMailObjectReferenceId()
262  {
263  return $this->mail_obj_ref_id;
264  }
265 
272  public function formatNamesForOutput($a_recipients)
273  {
274  global $DIC;
275 
276  $a_recipients = trim($a_recipients);
277  if (!strlen($a_recipients)) {
278  return $this->lng->txt('not_available');
279  }
280 
281  $names = array();
282 
283  $recipients = array_filter(array_map('trim', explode(',', $a_recipients)));
284  foreach ($recipients as $recipient) {
285  $usr_id = ilObjUser::_lookupId($recipient);
286  if ($usr_id > 0) {
287  $pp = ilObjUser::_lookupPref($usr_id, 'public_profile');
288  if ($pp == 'y' || ($pp == 'g' && !$DIC->user()->isAnonymous())) {
289  $user = self::getCachedUserInstance($usr_id);
290  $names[] = $user->getFullname() . ' [' . $recipient . ']';
291  continue;
292  }
293  }
294 
295  $names[] = $recipient;
296  }
297 
298  return implode(', ', $names);
299  }
300 
305  public function getPreviousMail($a_mail_id)
306  {
307  $this->db->setLimit(1);
308 
309  $res = $this->db->queryF(
310  "
311  SELECT b.* FROM {$this->table_mail} a
312  INNER JOIN {$this->table_mail} b ON b.folder_id = a.folder_id
313  AND b.user_id = a.user_id AND b.send_time > a.send_time
314  WHERE a.user_id = %s AND a.mail_id = %s ORDER BY b.send_time ASC",
315  array('integer', 'integer'),
316  array($this->user_id, $a_mail_id)
317  );
318 
319  $this->mail_data = $this->fetchMailData($this->db->fetchAssoc($res));
320 
321  return $this->mail_data;
322  }
323 
328  public function getNextMail($a_mail_id)
329  {
330  $this->db->setLimit(1);
331 
332  $res = $this->db->queryF(
333  "
334  SELECT b.* FROM {$this->table_mail} a
335  INNER JOIN {$this->table_mail} b ON b.folder_id = a.folder_id
336  AND b.user_id = a.user_id AND b.send_time < a.send_time
337  WHERE a.user_id = %s AND a.mail_id = %s ORDER BY b.send_time DESC",
338  array('integer', 'integer'),
339  array($this->user_id, $a_mail_id)
340  );
341 
342  $this->mail_data = $this->fetchMailData($this->db->fetchAssoc($res));
343 
344  return $this->mail_data;
345  }
346 
352  public function getMailsOfFolder($a_folder_id, $filter = array())
353  {
354  $output = array();
355 
356  $query = "
357  SELECT sender_id, m_subject, mail_id, m_status, send_time FROM {$this->table_mail}
358  LEFT JOIN object_data ON obj_id = sender_id
359  WHERE user_id = %s AND folder_id = %s
360  AND ((sender_id > 0 AND sender_id IS NOT NULL AND obj_id IS NOT NULL) OR (sender_id = 0 OR sender_id IS NULL)) ";
361 
362  if (isset($filter['status']) && strlen($filter['status']) > 0) {
363  $query .= ' AND m_status = ' . $this->db->quote($filter['status'], 'text');
364  }
365 
366  if (isset($filter['type']) && strlen($filter['type']) > 0) {
367  $query .= ' AND ' . $this->db->like('m_type', 'text', '%%:"' . $filter['type'] . '"%%', false);
368  }
369 
370  $query .= " ORDER BY send_time DESC";
371 
372  $res = $this->db->queryF(
373  $query,
374  array('integer', 'integer'),
375  array($this->user_id, $a_folder_id)
376  );
377 
378  while ($row = $this->db->fetchAssoc($res)) {
379  $output[] = $this->fetchMailData($row);
380  }
381 
382  return $output;
383  }
384 
389  public function countMailsOfFolder($a_folder_id)
390  {
391  $res = $this->db->queryF(
392  "
393  SELECT COUNT(*) FROM {$this->table_mail}
394  WHERE user_id = %s AND folder_id = %s",
395  array('integer', 'integer'),
396  array($this->user_id, $a_folder_id)
397  );
398 
399  return $this->db->numRows($res);
400  }
401 
406  public function deleteMailsOfFolder($a_folder_id)
407  {
408  if ($a_folder_id) {
409  $mails = $this->getMailsOfFolder($a_folder_id);
410  foreach ((array) $mails as $mail_data) {
411  $this->deleteMails(array($mail_data['mail_id']));
412  }
413 
414  return true;
415  }
416 
417  return false;
418  }
419 
424  public function getMail($a_mail_id)
425  {
426  $res = $this->db->queryF(
427  "
428  SELECT * FROM {$this->table_mail}
429  WHERE user_id = %s AND mail_id = %s",
430  array('integer', 'integer'),
431  array($this->user_id, $a_mail_id)
432  );
433 
434  $this->mail_data = $this->fetchMailData($this->db->fetchAssoc($res));
435 
436  return $this->mail_data;
437  }
438 
443  public function markRead(array $a_mail_ids)
444  {
445  $data = array();
446  $data_types = array();
447 
448  $query = "UPDATE {$this->table_mail} SET m_status = %s WHERE user_id = %s ";
449  array_push($data_types, 'text', 'integer');
450  array_push($data, 'read', $this->user_id);
451 
452  if (count($a_mail_ids) > 0) {
453  $in = 'mail_id IN (';
454  $counter = 0;
455  foreach ($a_mail_ids as $a_mail_id) {
456  array_push($data, $a_mail_id);
457  array_push($data_types, 'integer');
458 
459  if ($counter > 0) {
460  $in .= ',';
461  }
462  $in .= '%s';
463  ++$counter;
464  }
465  $in .= ')';
466 
467  $query .= ' AND ' . $in;
468  }
469 
470  $this->db->manipulateF($query, $data_types, $data);
471 
472  return true;
473  }
474 
479  public function markUnread(array $a_mail_ids)
480  {
481  $data = array();
482  $data_types = array();
483 
484  $query = "UPDATE {$this->table_mail} SET m_status = %s WHERE user_id = %s ";
485  array_push($data_types, 'text', 'integer');
486  array_push($data, 'unread', $this->user_id);
487 
488  if (count($a_mail_ids) > 0) {
489  $in = 'mail_id IN (';
490  $counter = 0;
491  foreach ($a_mail_ids as $a_mail_id) {
492  array_push($data, $a_mail_id);
493  array_push($data_types, 'integer');
494 
495  if ($counter > 0) {
496  $in .= ',';
497  }
498  $in .= '%s';
499  ++$counter;
500  }
501  $in .= ')';
502 
503  $query .= ' AND ' . $in;
504  }
505 
506  $this->db->manipulateF($query, $data_types, $data);
507 
508  return true;
509  }
510 
516  public function moveMailsToFolder(array $mailIds, int $folderId) : bool
517  {
518  $values = [];
519  $dataTypes = [];
520 
521  $mailIds = array_filter(array_map('intval', $mailIds));
522 
523  if (0 === count($mailIds)) {
524  return false;
525  }
526 
527  $query = "
528  UPDATE {$this->table_mail}
529  INNER JOIN mail_obj_data
530  ON mail_obj_data.obj_id = %s AND mail_obj_data.user_id = %s
531  SET {$this->table_mail}.folder_id = mail_obj_data.obj_id
532  WHERE {$this->table_mail}.user_id = %s
533  ";
534  array_push($dataTypes, 'integer', 'integer', 'integer');
535  array_push($values, $folderId, $this->user_id, $this->user_id);
536 
537  $in = 'mail_id IN (';
538  $counter = 0;
539  foreach ($mailIds as $mailId) {
540  array_push($values, $mailId);
541  array_push($dataTypes, 'integer');
542 
543  if ($counter > 0) {
544  $in .= ',';
545  }
546  $in .= '%s';
547  ++$counter;
548  }
549  $in .= ')';
550 
551  $query .= ' AND ' . $in;
552 
553  $affectedRows = $this->db->manipulateF($query, $dataTypes, $values);
554 
555  return $affectedRows > 0;
556  }
557 
562  public function deleteMails(array $mailIds)
563  {
564  $mailIds = array_filter(array_map('intval', $mailIds));
565  foreach ($mailIds as $id) {
566  $this->db->manipulateF(
567  "
568  DELETE FROM {$this->table_mail} WHERE user_id = %s AND mail_id = %s",
569  array('integer', 'integer'),
570  array($this->user_id, $id)
571  );
572  $this->mfile->deassignAttachmentFromDirectory($id);
573  }
574 
575  return true;
576  }
577 
582  protected function fetchMailData($a_row)
583  {
584  if (!is_array($a_row) || empty($a_row)) {
585  return null;
586  }
587 
588  $a_row['attachments'] = unserialize(stripslashes($a_row['attachments']));
589  $a_row['m_type'] = unserialize(stripslashes($a_row['m_type']));
590  $a_row['tpl_ctx_params'] = (array) (@json_decode($a_row['tpl_ctx_params'], true));
591 
592  return $a_row;
593  }
594 
600  public function getNewDraftId($usrId, $folderId)
601  {
602  $next_id = $this->db->nextId($this->table_mail);
603  $this->db->insert($this->table_mail, array(
604  'mail_id' => array('integer', $next_id),
605  'user_id' => array('integer', $usrId),
606  'folder_id' => array('integer', $folderId),
607  'sender_id' => array('integer', $usrId)
608  ));
609 
610  return $next_id;
611  }
612 
613  public function updateDraft(
614  $a_folder_id,
615  $a_attachments,
616  $a_rcp_to,
617  $a_rcp_cc,
618  $a_rcp_bcc,
619  $a_m_type,
620  $a_m_email,
621  $a_m_subject,
622  $a_m_message,
623  $a_draft_id = 0,
624  $a_use_placeholders = 0,
625  $a_tpl_context_id = null,
626  $a_tpl_context_params = array()
627  ) {
628  $this->db->update(
629  $this->table_mail,
630  array(
631  'folder_id' => array('integer', $a_folder_id),
632  'attachments' => array('clob', serialize($a_attachments)),
633  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
634  'rcp_to' => array('clob', $a_rcp_to),
635  'rcp_cc' => array('clob', $a_rcp_cc),
636  'rcp_bcc' => array('clob', $a_rcp_bcc),
637  'm_status' => array('text', 'read'),
638  'm_type' => array('text', serialize($a_m_type)),
639  'm_email' => array('integer', $a_m_email),
640  'm_subject' => array('text', $a_m_subject),
641  'm_message' => array('clob', $a_m_message),
642  'use_placeholders' => array('integer', $a_use_placeholders),
643  'tpl_ctx_id' => array('text', $a_tpl_context_id),
644  'tpl_ctx_params' => array('blob', @json_encode((array) $a_tpl_context_params))
645  ),
646  array(
647  'mail_id' => array('integer', $a_draft_id)
648  )
649  );
650 
651  return $a_draft_id;
652  }
653 
674  private function sendInternalMail(
675  $a_folder_id,
676  $a_sender_id,
677  $a_attachments,
678  $a_rcp_to,
679  $a_rcp_cc,
680  $a_rcp_bcc,
681  $a_status,
682  $a_m_type,
683  $a_m_email,
684  $a_m_subject,
685  $a_m_message,
686  $a_user_id = 0,
687  $a_use_placeholders = 0,
688  $a_tpl_context_id = null,
689  $a_tpl_context_params = array()
690  ) {
691  $a_user_id = $a_user_id ? $a_user_id : $this->user_id;
692 
693  if ($a_use_placeholders) {
694  $a_m_message = $this->replacePlaceholders($a_m_message, $a_user_id);
695  }
696  $a_m_message = $this->formatLinebreakMessage($a_m_message);
697 
698  if (!$a_user_id) {
699  $a_user_id = '0';
700  }
701  if (!$a_folder_id) {
702  $a_folder_id = '0';
703  }
704  if (!$a_sender_id) {
705  $a_sender_id = null;
706  }
707  if (!$a_attachments) {
708  $a_attachments = null;
709  }
710  if (!$a_rcp_to) {
711  $a_rcp_to = null;
712  }
713  if (!$a_rcp_cc) {
714  $a_rcp_cc = null;
715  }
716  if (!$a_rcp_bcc) {
717  $a_rcp_bcc = null;
718  }
719  if (!$a_status) {
720  $a_status = null;
721  }
722  if (!$a_m_type) {
723  $a_m_type = null;
724  }
725  if (!$a_m_email) {
726  $a_m_email = null;
727  }
728  if (!$a_m_subject) {
729  $a_m_subject = null;
730  }
731  if (!$a_m_message) {
732  $a_m_message = null;
733  }
734 
735  $nextId = $this->db->nextId($this->table_mail);
736  $this->db->insert($this->table_mail, array(
737  'mail_id' => array('integer', $nextId),
738  'user_id' => array('integer', $a_user_id),
739  'folder_id' => array('integer', $a_folder_id),
740  'sender_id' => array('integer', $a_sender_id),
741  'attachments' => array('clob', serialize($a_attachments)),
742  'send_time' => array('timestamp', date('Y-m-d H:i:s', time())),
743  'rcp_to' => array('clob', $a_rcp_to),
744  'rcp_cc' => array('clob', $a_rcp_cc),
745  'rcp_bcc' => array('clob', $a_rcp_bcc),
746  'm_status' => array('text', $a_status),
747  'm_type' => array('text', serialize($a_m_type)),
748  'm_email' => array('integer', $a_m_email),
749  'm_subject' => array('text', $a_m_subject),
750  'm_message' => array('clob', $a_m_message),
751  'tpl_ctx_id' => array('text', $a_tpl_context_id),
752  'tpl_ctx_params' => array('blob', @json_encode((array) $a_tpl_context_params))
753  ));
754 
755  $raiseEvent = (int) $a_user_id !== (int) $this->mailbox->getUserId();
756  if (!$raiseEvent) {
757  $raiseEvent = (int) $a_folder_id !== (int) $this->mailbox->getSentFolder();
758  }
759 
760  if ($raiseEvent) {
761  $this->eventHandler->raise('Services/Mail', 'sentInternalMail', [
762  'id' => (int) $nextId,
763  'subject' => (string) $a_m_subject,
764  'body' => (string) $a_m_message,
765  'from_usr_id' => (int) $a_sender_id,
766  'to_usr_id' => (int) $a_user_id,
767  'rcp_to' => (string) $a_rcp_to,
768  'rcp_cc' => (string) $a_rcp_cc,
769  'rcp_bcc' => (string) $a_rcp_bcc,
770  'type' => (array) $a_m_type,
771  ]);
772  }
773 
774  return $nextId;
775  }
776 
783  protected function replacePlaceholders($a_message, $a_user_id = 0, $replace_empty = true)
784  {
785  try {
786  if ($this->contextId) {
788  } else {
790  }
791 
792  $user = $a_user_id > 0 ? self::getCachedUserInstance($a_user_id) : null;
793 
794  $processor = new ilMailTemplatePlaceholderResolver($context, $a_message);
795  $a_message = $processor->resolve($user, $this->contextParameters, $replace_empty);
796  } catch (Exception $e) {
797  ilLoggerFactory::getLogger('mail')->error(__METHOD__ . ' has been called with invalid context.');
798  }
799 
800  return $a_message;
801  }
802 
816  protected function distributeMail($a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_subject, $a_message, $a_attachments, $sent_mail_id, $a_type, $a_action, $a_use_placeholders = 0)
817  {
818  require_once 'Services/Mail/classes/class.ilMailbox.php';
819  require_once 'Services/User/classes/class.ilObjUser.php';
820 
821  $mbox = new ilMailbox();
822  if (!$a_use_placeholders) {
823  $rcp_ids = $this->getUserIds(array($a_rcp_to, $a_rcp_cc, $a_rcp_bcc));
824 
825  ilLoggerFactory::getLogger('mail')->debug(sprintf(
826  "Parsed TO/CC/BCC user ids from given recipients: %s",
827  implode(', ', $rcp_ids)
828  ));
829 
830  $as_email = array();
831 
832  foreach ($rcp_ids as $id) {
833  $tmp_mail_options = new ilMailOptions($id);
834 
835  $tmp_user = self::getCachedUserInstance($id);
836  $user_is_active = $tmp_user->getActive();
837  $user_can_read_internal_mails = !$tmp_user->hasToAcceptTermsOfService() && $tmp_user->checkTimeLimit();
838 
839  if (in_array('system', $a_type) && !$user_can_read_internal_mails) {
840  ilLoggerFactory::getLogger('mail')->debug(sprintf(
841  "Message is marked as 'system', skipped recipient with id %s (Accepted User Agreement:%s|Expired Account:%s)",
842  $id,
843  var_export(!$tmp_user->hasToAcceptTermsOfService(), 1),
844  var_export(!$tmp_user->checkTimeLimit(), 1)
845  ));
846  continue;
847  }
848 
849  if ($user_is_active) {
850  if (!$user_can_read_internal_mails
851  || $tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_EMAIL
852  || $tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_BOTH) {
853  $newEmailAddresses = ilMailOptions::getExternalEmailsByUser($tmp_user, $tmp_mail_options);
854  $as_email = array_unique(array_merge($newEmailAddresses, $as_email));
855 
856  if ($tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_EMAIL) {
857  ilLoggerFactory::getLogger('mail')->debug(sprintf(
858  "Recipient with id %s will only receive external emails sent to: %s",
859  $id,
860  implode(', ', $newEmailAddresses)
861  ));
862  continue;
863  } else {
864  ilLoggerFactory::getLogger('mail')->debug(sprintf(
865  "Recipient with id %s will additionally receive external emails sent to: %s",
866  $id,
867  implode(', ', $newEmailAddresses)
868  ));
869  }
870  }
871  }
872 
873  $mbox->setUserId($id);
874  $inbox_id = $mbox->getInboxFolder();
875 
876  $mail_id = $this->sendInternalMail(
877  $inbox_id,
878  $this->user_id,
879  $a_attachments,
880  $a_rcp_to,
881  $a_rcp_cc,
882  '',
883  'unread',
884  $a_type,
885  0,
886  $a_subject,
887  $a_message,
888  $id,
889  0
890  );
891 
892  if ($a_attachments) {
893  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id);
894  }
895  }
896 
897  $to = array();
898  $bcc = array();
899 
900  $as_email = array_values(array_unique($as_email));
901  if (count($as_email) == 1) {
902  $to[] = $as_email[0];
903  } else {
904  foreach ($as_email as $email) {
905  $bcc[] = $email;
906  }
907  }
908 
909  if (count($to) > 0) {
910  $this->sendMimeMail(implode(',', $to), '', implode(',', $bcc), $a_subject, $this->formatLinebreakMessage($a_message), $a_attachments);
911  } elseif (count($bcc) > 0) {
912  $remainingAddresses = '';
913  $maxRecipientCharacterLength = 998;
914  foreach ($bcc as $emailAddress) {
915  $sep = '';
916  if (strlen($remainingAddresses) > 0) {
917  $sep = ',';
918  }
919 
920  $recipientsLineLength = ilStr::strLen($remainingAddresses) + ilStr::strLen($sep . $emailAddress);
921  if ($recipientsLineLength >= $maxRecipientCharacterLength) {
922  $this->sendMimeMail(
923  '',
924  '',
925  $remainingAddresses,
926  $a_subject,
927  $this->formatLinebreakMessage($a_message),
928  $a_attachments
929  );
930 
931  $remainingAddresses = '';
932  $sep = '';
933  }
934 
935  $remainingAddresses .= ($sep . $emailAddress);
936  }
937 
938  if ('' !== $remainingAddresses) {
939  $this->sendMimeMail(
940  '',
941  '',
942  $remainingAddresses,
943  $a_subject,
944  $this->formatLinebreakMessage($a_message),
945  $a_attachments
946  );
947  }
948  }
949  } else {
950  $rcp_ids_replace = $this->getUserIds(array($a_rcp_to));
951  $rcp_ids_no_replace = $this->getUserIds(array($a_rcp_cc, $a_rcp_bcc));
952 
953  ilLoggerFactory::getLogger('mail')->debug(sprintf(
954  "Parsed TO user ids from given recipients for serial letter notification: %s",
955  implode(', ', $rcp_ids_replace)
956  ));
957  ilLoggerFactory::getLogger('mail')->debug(sprintf(
958  "Parsed CC/BCC user ids from given recipients for serial letter notification: %s",
959  implode(', ', $rcp_ids_no_replace)
960  ));
961 
962  $as_email = array();
963  $id_to_message_map = array();
964 
965  foreach ($rcp_ids_replace as $id) {
966  $tmp_mail_options = new ilMailOptions($id);
967 
968  $tmp_user = self::getCachedUserInstance($id);
969  $user_is_active = $tmp_user->getActive();
970  $user_can_read_internal_mails = !$tmp_user->hasToAcceptTermsOfService() && $tmp_user->checkTimeLimit();
971 
972  if (in_array('system', $a_type) && !$user_can_read_internal_mails) {
973  ilLoggerFactory::getLogger('mail')->debug(sprintf(
974  "Message is marked as 'system', skipped recipient with id %s (Accepted User Agreement:%s|Expired Account:%s)",
975  $id,
976  var_export(!$tmp_user->hasToAcceptTermsOfService(), 1),
977  var_export(!$tmp_user->checkTimeLimit(), 1)
978  ));
979  continue;
980  }
981 
982  $id_to_message_map[$id] = $this->replacePlaceholders($a_message, $id);
983 
984  if ($user_is_active) {
985  if (!$user_can_read_internal_mails
986  || $tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_EMAIL
987  || $tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_BOTH) {
988  $as_email[$tmp_user->getId()] = ilMailOptions::getExternalEmailsByUser($tmp_user, $tmp_mail_options);
989 
990  if ($tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_EMAIL) {
991  ilLoggerFactory::getLogger('mail')->debug(sprintf(
992  "Recipient with id %s will only receive external emails sent to: %s",
993  $id,
994  implode(', ', $as_email[$tmp_user->getId()])
995  ));
996  continue;
997  } else {
998  ilLoggerFactory::getLogger('mail')->debug(sprintf(
999  "Recipient with id %s will additionally receive external emails sent to: %s",
1000  $id,
1001  implode(', ', $as_email[$tmp_user->getId()])
1002  ));
1003  }
1004  }
1005  }
1006 
1007  $mbox->setUserId($id);
1008  $inbox_id = $mbox->getInboxFolder();
1009 
1010  $mail_id = $this->sendInternalMail(
1011  $inbox_id,
1012  $this->user_id,
1013  $a_attachments,
1014  $a_rcp_to,
1015  $a_rcp_cc,
1016  '',
1017  'unread',
1018  $a_type,
1019  0,
1020  $a_subject,
1021  $id_to_message_map[$id],
1022  $id,
1023  0
1024  );
1025 
1026  if ($a_attachments) {
1027  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id);
1028  }
1029  }
1030 
1031  if (count($as_email)) {
1032  foreach ($as_email as $id => $emails) {
1033  if (0 == count($emails)) {
1034  continue;
1035  }
1036 
1037  $toEmailAddresses = implode(',', $emails);
1038  $this->sendMimeMail($toEmailAddresses, '', '', $a_subject, $this->formatLinebreakMessage($id_to_message_map[$id]), $a_attachments);
1039  }
1040  }
1041 
1042  $as_email = array();
1043 
1044  $cc_and_bcc_message = $this->replacePlaceholders($a_message, 0, false);
1045 
1046  foreach ($rcp_ids_no_replace as $id) {
1047  $tmp_mail_options = new ilMailOptions($id);
1048 
1049  $tmp_user = self::getCachedUserInstance($id);
1050  $user_is_active = $tmp_user->getActive();
1051  $user_can_read_internal_mails = !$tmp_user->hasToAcceptTermsOfService() && $tmp_user->checkTimeLimit();
1052 
1053  if ($user_is_active) {
1054  if (in_array('system', $a_type) && !$user_can_read_internal_mails) {
1055  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1056  "Message is marked as 'system', skipped recipient with id %s (Accepted User Agreement:%s|Expired Account:%s)",
1057  $id,
1058  var_export(!$tmp_user->hasToAcceptTermsOfService(), 1),
1059  var_export(!$tmp_user->checkTimeLimit(), 1)
1060  ));
1061  continue;
1062  }
1063 
1064 
1065  if (!$user_can_read_internal_mails
1066  || $tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_EMAIL
1067  || $tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_BOTH) {
1068  $newEmailAddresses = ilMailOptions::getExternalEmailsByUser($tmp_user, $tmp_mail_options);
1069  $as_email = array_unique(array_merge($newEmailAddresses, $as_email));
1070 
1071  if ($tmp_mail_options->getIncomingType() == ilMailOptions::INCOMING_EMAIL) {
1072  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1073  "Recipient with id %s will only receive external emails sent to: %s",
1074  $id,
1075  implode(', ', $newEmailAddresses)
1076  ));
1077  continue;
1078  } else {
1079  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1080  "Recipient with id %s will additionally receive external emails sent to: %s",
1081  $id,
1082  implode(', ', $newEmailAddresses)
1083  ));
1084  }
1085  }
1086  }
1087 
1088  $mbox->setUserId($id);
1089  $inbox_id = $mbox->getInboxFolder();
1090 
1091  $mail_id = $this->sendInternalMail(
1092  $inbox_id,
1093  $this->user_id,
1094  $a_attachments,
1095  $a_rcp_to,
1096  $a_rcp_cc,
1097  '',
1098  'unread',
1099  $a_type,
1100  0,
1101  $a_subject,
1102  $cc_and_bcc_message,
1103  $id,
1104  0
1105  );
1106 
1107  if ($a_attachments) {
1108  $this->mfile->assignAttachmentsToDirectory($mail_id, $sent_mail_id);
1109  }
1110  }
1111 
1112  if (count($as_email)) {
1113  $this->sendMimeMail('', '', implode(',', $as_email), $a_subject, $this->formatLinebreakMessage($cc_and_bcc_message), $a_attachments);
1114  }
1115  }
1116 
1117  return true;
1118  }
1119 
1124  protected function getUserIds(array $recipients) : array
1125  {
1126  $usrIds = array();
1127 
1128  $joinedRecipients = implode(',', array_filter(array_map('trim', $recipients)));
1129 
1130  $addresses = $this->parseAddresses($joinedRecipients);
1131  foreach ($addresses as $address) {
1132  $addressType = $this->mailAddressTypeFactory->getByPrefix($address);
1133  $usrIds = array_merge($usrIds, $addressType->resolve());
1134  }
1135 
1136  return array_unique($usrIds);
1137  }
1138 
1146  protected function checkMail(string $to, string $cc, string $bcc, string $subject) : array
1147  {
1148  $errors = [];
1149 
1150  foreach (array(
1151  $subject => 'mail_add_subject',
1152  $to => 'mail_add_recipient'
1153  ) as $string => $error) {
1154  if (0 === strlen($string)) {
1155  $errors[] = new \ilMailError($error);
1156  }
1157  }
1158 
1159  return $errors;
1160  }
1161 
1168  protected function checkRecipients(string $recipients) : array
1169  {
1170  $errors = [];
1171 
1172  try {
1173  $addresses = $this->parseAddresses($recipients);
1174  foreach ($addresses as $address) {
1175  $addressType = $this->mailAddressTypeFactory->getByPrefix($address);
1176  if (!$addressType->validate($this->user_id)) {
1177  $newErrors = $addressType->getErrors();
1178  $errors = array_merge($errors, $newErrors);
1179  }
1180  }
1181  } catch (\ilException $e) {
1182  $colonPosition = strpos($e->getMessage(), ':');
1183  throw new \ilMailException(
1184  ($colonPosition === false) ? $e->getMessage() : substr($e->getMessage(), $colonPosition + 2)
1185  );
1186  }
1187 
1188  return $errors;
1189  }
1190 
1208  public function savePostData(
1209  $a_user_id,
1210  $a_attachments,
1211  $a_rcp_to,
1212  $a_rcp_cc,
1213  $a_rcp_bcc,
1214  $a_m_type,
1215  $a_m_email,
1216  $a_m_subject,
1217  $a_m_message,
1218  $a_use_placeholders,
1219  $a_tpl_context_id = null,
1220  $a_tpl_ctx_params = array()
1221  ) {
1222  if (!$a_attachments) {
1223  $a_attachments = null;
1224  }
1225  if (!$a_rcp_to) {
1226  $a_rcp_to = null;
1227  }
1228  if (!$a_rcp_cc) {
1229  $a_rcp_cc = null;
1230  }
1231  if (!$a_rcp_bcc) {
1232  $a_rcp_bcc = null;
1233  }
1234  if (!$a_m_type) {
1235  $a_m_type = null;
1236  }
1237  if (!$a_m_email) {
1238  $a_m_email = null;
1239  }
1240  if (!$a_m_message) {
1241  $a_m_message = null;
1242  }
1243  if (!$a_use_placeholders) {
1244  $a_use_placeholders = '0';
1245  }
1246 
1247  $this->db->replace(
1248  $this->table_mail_saved,
1249  array(
1250  'user_id' => array('integer', $this->user_id)
1251  ),
1252  array(
1253  'attachments' => array('clob', serialize($a_attachments)),
1254  'rcp_to' => array('clob', $a_rcp_to),
1255  'rcp_cc' => array('clob', $a_rcp_cc),
1256  'rcp_bcc' => array('clob', $a_rcp_bcc),
1257  'm_type' => array('text', serialize($a_m_type)),
1258  'm_email' => array('integer', $a_m_email),
1259  'm_subject' => array('text', $a_m_subject),
1260  'm_message' => array('clob', $a_m_message),
1261  'use_placeholders' => array('integer', $a_use_placeholders),
1262  'tpl_ctx_id' => array('text', $a_tpl_context_id),
1263  'tpl_ctx_params' => array('blob', json_encode((array) $a_tpl_ctx_params))
1264  )
1265  );
1266 
1267  $this->getSavedData();
1268 
1269  return true;
1270  }
1271 
1275  public function getSavedData()
1276  {
1277  $res = $this->db->queryF(
1278  "SELECT * FROM {$this->table_mail_saved} WHERE user_id = %s",
1279  array('integer'),
1280  array($this->user_id)
1281  );
1282 
1283  $this->mail_data = $this->fetchMailData($this->db->fetchAssoc($res));
1284 
1285  return $this->mail_data;
1286  }
1287 
1300  public function sendMail($a_rcp_to, $a_rcp_cc, $a_rcp_bc, $a_m_subject, $a_m_message, $a_attachment, $a_type, $a_use_placeholders = 0) : array
1301  {
1302  global $DIC;
1303 
1304  ilLoggerFactory::getLogger('mail')->debug(
1305  "New mail system task:" .
1306  " To: " . $a_rcp_to .
1307  " | CC: " . $a_rcp_cc .
1308  " | BCC: " . $a_rcp_bc .
1309  " | Subject: " . $a_m_subject
1310  );
1311 
1312  if (in_array('system', $a_type)) {
1313  $a_type = array('system');
1314  }
1315 
1316  if ($a_attachment && !$this->mfile->checkFilesExist($a_attachment)) {
1317  return [new \ilMailError('mail_attachment_file_not_exist', [$a_attachment])];
1318  }
1319 
1320  $errors = $this->checkMail((string) $a_rcp_to, (string) $a_rcp_cc, (string) $a_rcp_bc, (string) $a_m_subject);
1321  if (count($errors) > 0) {
1322  return $errors;
1323  }
1324 
1325  $errors = $this->validateRecipients((string) $a_rcp_to, (string) $a_rcp_cc, (string) $a_rcp_bc);
1326  if (count($errors) > 0) {
1327  return $errors;
1328  }
1329 
1330  $rcp_to = $a_rcp_to;
1331  $rcp_cc = $a_rcp_cc;
1332  $rcp_bc = $a_rcp_bc;
1333 
1334  if (null === $rcp_cc) {
1335  $rcp_cc = '';
1336  }
1337 
1338  if (null === $rcp_bc) {
1339  $rcp_bc = '';
1340  }
1341 
1342  $numberOfExternalAddresses = $this->getCountRecipients($rcp_to, $rcp_cc, $rcp_bc, true);
1343 
1344  if (
1345  $numberOfExternalAddresses > 0 &&
1346  !$this->isSystemMail() &&
1347  !$DIC->rbac()->system()->checkAccessOfUser($this->user_id, 'smtp_mail', $this->mail_obj_ref_id)
1348  ) {
1349  return [new \ilMailError('mail_no_permissions_write_smtp')];
1350  }
1351 
1352  if ($this->appendInstallationSignature()) {
1353  $a_m_message .= self::_getInstallationSignature();
1354  }
1355 
1356  $sent_id = $this->saveInSentbox($a_attachment, $a_rcp_to, $a_rcp_cc, $a_rcp_bc, $a_type, $a_m_subject, $a_m_message);
1357 
1358  if ($a_attachment) {
1359  $this->mfile->assignAttachmentsToDirectory($sent_id, $sent_id);
1360  $this->mfile->saveFiles($sent_id, $a_attachment);
1361  }
1362 
1363  if ($numberOfExternalAddresses > 0) {
1364  $externalMailRecipientsTo = $this->getEmailRecipients($rcp_to);
1365  $externalMailRecipientsCc = $this->getEmailRecipients($rcp_cc);
1366  $externalMailRecipientsBcc = $this->getEmailRecipients($rcp_bc);
1367 
1368  ilLoggerFactory::getLogger('mail')->debug(
1369  "Parsed external email addresses from given recipients:" .
1370  " To: " . $externalMailRecipientsTo .
1371  " | CC: " . $externalMailRecipientsCc .
1372  " | BCC: " . $externalMailRecipientsBcc .
1373  " | Subject: " . $a_m_subject
1374  );
1375 
1376  $this->sendMimeMail(
1377  $externalMailRecipientsTo,
1378  $externalMailRecipientsCc,
1379  $externalMailRecipientsBcc,
1380  $a_m_subject,
1381  $this->formatLinebreakMessage($a_use_placeholders ? $this->replacePlaceholders($a_m_message, 0, false) : $a_m_message),
1382  $a_attachment,
1383  0
1384  );
1385  } else {
1386  ilLoggerFactory::getLogger('mail')->debug('No external email addresses given in recipient string');
1387  }
1388 
1389  if (in_array('system', $a_type) && !$this->distributeMail($rcp_to, $rcp_cc, $rcp_bc, $a_m_subject, $a_m_message, $a_attachment, $sent_id, $a_type, 'system', $a_use_placeholders)) {
1390  return [new \ilMailError('mail_send_error')];
1391  }
1392 
1393  if (in_array('normal', $a_type) && !$this->distributeMail($rcp_to, $rcp_cc, $rcp_bc, $a_m_subject, $a_m_message, $a_attachment, $sent_id, $a_type, 'normal', $a_use_placeholders)) {
1394  return [new \ilMailError('mail_send_error')];
1395  }
1396 
1397  if (!$this->getSaveInSentbox()) {
1398  $this->deleteMails([$sent_id]);
1399  }
1400 
1401  return [];
1402  }
1403 
1410  public function validateRecipients(string $to, string $cc, string $bcc) : array
1411  {
1412  try {
1413  $errors = array();
1414  $errors = array_merge($errors, $this->checkRecipients($to));
1415  $errors = array_merge($errors, $this->checkRecipients($cc));
1416  $errors = array_merge($errors, $this->checkRecipients($bcc));
1417 
1418  if (count($errors) > 0) {
1419  return array_merge([new \ilMailError('mail_following_rcp_not_valid')], $errors);
1420  }
1421  } catch (\ilMailException $e) {
1422  return [new \ilMailError('mail_generic_rcp_error', [$e->getMessage()])];
1423  }
1424 
1425  return [];
1426  }
1427 
1439  protected function saveInSentbox($a_attachment, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_type, $a_m_subject, $a_m_message)
1440  {
1441  return $this->sendInternalMail(
1442  $this->mailbox->getSentFolder(),
1444  $a_attachment,
1445  $a_rcp_to,
1446  $a_rcp_cc,
1447  $a_rcp_bcc,
1448  'read',
1449  $a_type,
1450  0,
1451  $a_m_subject,
1452  $a_m_message,
1454  0
1455  );
1456  }
1457 
1469  public function sendMimeMail($a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_m_subject, $a_m_message, $a_attachments, $a_no_soap = false)
1470  {
1471  require_once 'Services/Mail/classes/class.ilMimeMail.php';
1472 
1473  $a_m_subject = self::getSubjectPrefix() . ' ' . $a_m_subject;
1474 
1475  // #10854
1476  if ($this->isSOAPEnabled() && !$a_no_soap) {
1477  require_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
1478  $soap_client = new ilSoapClient();
1479  $soap_client->setResponseTimeout(5);
1480  $soap_client->enableWSDL(true);
1481  $soap_client->init();
1482 
1483  $attachments = array();
1484  $a_attachments = $a_attachments ? $a_attachments : array();
1485  foreach ($a_attachments as $attachment) {
1486  $attachments[] = $this->mfile->getAbsoluteAttachmentPoolPathByFilename($attachment);
1487  }
1488 
1489  // mjansen: switched separator from "," to "#:#" because of mantis bug #6039
1490  $attachments = implode('#:#', $attachments);
1491  // mjansen: use "#:#" as leading delimiter
1492  if (strlen($attachments)) {
1493  $attachments = "#:#" . $attachments;
1494  }
1495 
1496  $soap_client->call('sendMail', array(
1497  session_id() . '::' . $_COOKIE['ilClientId'],
1498  $a_rcp_to,
1499  $a_rcp_cc,
1500  $a_rcp_bcc,
1501  $this->user_id,
1502  $a_m_subject,
1503  $a_m_message,
1504  $attachments
1505  ));
1506  } else {
1508  $senderFactory = $GLOBALS["DIC"]["mail.mime.sender.factory"];
1509 
1510  $mmail = new ilMimeMail();
1511  $mmail->From($senderFactory->getSenderByUsrId($this->user_id));
1512  $mmail->To($a_rcp_to);
1513  $mmail->Subject($a_m_subject);
1514  $mmail->Body($a_m_message);
1515 
1516  if ($a_rcp_cc) {
1517  $mmail->Cc($a_rcp_cc);
1518  }
1519 
1520  if ($a_rcp_bcc) {
1521  $mmail->Bcc($a_rcp_bcc);
1522  }
1523 
1524  if (is_array($a_attachments)) {
1525  foreach ($a_attachments as $attachment) {
1526  $mmail->Attach($this->mfile->getAbsoluteAttachmentPoolPathByFilename($attachment), '', 'inline', $attachment);
1527  }
1528  }
1529 
1530  $mmail->Send();
1531  }
1532  }
1533 
1538  public function saveAttachments($a_attachments)
1539  {
1540  $this->db->update(
1541  $this->table_mail_saved,
1542  array(
1543  'attachments' => array('clob', serialize($a_attachments))
1544  ),
1545  array(
1546  'user_id' => array('integer', $this->user_id)
1547  )
1548  );
1549 
1550  return true;
1551  }
1552 
1559  protected function parseAddresses($addresses)
1560  {
1561  if (strlen($addresses) > 0) {
1562  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1563  "Started parsing of recipient string: %s",
1564  $addresses
1565  ));
1566  }
1567 
1568  $parser = $this->mailAddressParserFactory->getParser((string) $addresses);
1569  $parsedAddresses = $parser->parse();
1570 
1571  if (strlen($addresses) > 0) {
1572  ilLoggerFactory::getLogger('mail')->debug(sprintf(
1573  "Parsed addresses: %s",
1574  implode(',', array_map(function (ilMailAddress $address) {
1575  return (string) $address;
1576  }, $parsedAddresses))
1577  ));
1578  }
1579 
1580  return $parsedAddresses;
1581  }
1582 
1588  protected function getCountRecipient(string $recipients, $onlyExternalAddresses = true) : int
1589  {
1590  $addresses = new \ilMailAddressListImpl($this->parseAddresses($recipients));
1591  if ($onlyExternalAddresses) {
1592  $addresses = new \ilMailOnlyExternalAddressList($addresses, self::ILIAS_HOST);
1593  }
1594 
1595  return count($addresses->value());
1596  }
1597 
1605  protected function getCountRecipients(
1606  string $toRecipients,
1607  string $ccRecipients,
1608  string $bccRecipients,
1609  $onlyExternalAddresses = true
1610  ) : int {
1611  return (
1612  $this->getCountRecipient($toRecipients, $onlyExternalAddresses) +
1613  $this->getCountRecipient($ccRecipients, $onlyExternalAddresses) +
1614  $this->getCountRecipient($bccRecipients, $onlyExternalAddresses)
1615  );
1616  }
1617 
1622  protected function getEmailRecipients(string $recipients) : string
1623  {
1624  $addresses = new \ilMailOnlyExternalAddressList(
1625  new \ilMailAddressListImpl($this->parseAddresses($recipients)),
1626  self::ILIAS_HOST
1627  );
1628 
1629  $emailRecipients = array_map(function (\ilMailAddress $address) {
1630  return (string) $address;
1631  }, $addresses->value());
1632 
1633  return implode(',', $emailRecipients);
1634  }
1635 
1641  public static function _getAutoGeneratedMessageString(ilLanguage $lang = null)
1642  {
1643  global $DIC;
1644 
1645  if (!($lang instanceof ilLanguage)) {
1646  require_once 'Services/Language/classes/class.ilLanguageFactory.php';
1648  }
1649 
1650  $lang->loadLanguageModule('mail');
1651 
1652  return sprintf(
1653  $lang->txt('mail_auto_generated_info'),
1654  $DIC->settings()->get('inst_name', 'ILIAS 5'),
1656  ) . "\n\n";
1657  }
1658 
1663  public static function _getIliasMailerName()
1664  {
1666  $senderFactory = $GLOBALS["DIC"]["mail.mime.sender.factory"];
1667 
1668  return $senderFactory->system()->getFromName();
1669  }
1670 
1676  public function appendInstallationSignature($a_flag = null)
1677  {
1678  if (null === $a_flag) {
1680  }
1681 
1682  $this->appendInstallationSignature = $a_flag;
1683  return $this;
1684  }
1685 
1689  public static function _getInstallationSignature()
1690  {
1691  global $DIC;
1692 
1693  $signature = $DIC->settings()->get('mail_system_sys_signature');
1694 
1695  $clientUrl = ilUtil::_getHttpPath();
1696  $clientdirs = glob(ILIAS_WEB_DIR . '/*', GLOB_ONLYDIR);
1697  if (is_array($clientdirs) && count($clientdirs) > 1) {
1698  $clientUrl .= '/login.php?client_id=' . CLIENT_ID; // #18051
1699  }
1700 
1701  $signature = str_ireplace('[CLIENT_NAME]', $DIC['ilClientIniFile']->readVariable('client', 'name'), $signature);
1702  $signature = str_ireplace('[CLIENT_DESC]', $DIC['ilClientIniFile']->readVariable('client', 'description'), $signature);
1703  $signature = str_ireplace('[CLIENT_URL]', $clientUrl, $signature);
1704 
1705  if (!preg_match('/^[\n\r]+/', $signature)) {
1706  $signature = "\n" . $signature;
1707  }
1708 
1709  return $signature;
1710  }
1711 
1716  public static function getSubjectPrefix()
1717  {
1718  global $DIC;
1719 
1720  $subjectPrefix = $DIC->settings()->get('mail_subject_prefix');
1721  if (false === $subjectPrefix) {
1722  $subjectPrefix = self::MAIL_SUBJECT_PREFIX;
1723  }
1724 
1725  return $subjectPrefix;
1726  }
1727 
1733  public static function getSalutation($a_usr_id, ilLanguage $a_language = null)
1734  {
1735  global $DIC;
1736 
1737  $lang = ($a_language instanceof ilLanguage) ? $a_language : $DIC->language();
1738  $lang->loadLanguageModule('mail');
1739 
1740  $gender = ilObjUser::_lookupGender($a_usr_id);
1741  $gender = $gender ? $gender : 'n';
1742  $name = ilObjUser::_lookupName($a_usr_id);
1743 
1744  if (!strlen($name['firstname'])) {
1745  return $lang->txt('mail_salutation_anonymous') . ',';
1746  }
1747 
1748  return
1749  $lang->txt('mail_salutation_' . $gender) . ' ' .
1750  ($name['title'] ? $name['title'] . ' ' : '') .
1751  ($name['firstname'] ? $name['firstname'] . ' ' : '') .
1752  $name['lastname'] . ',';
1753  }
1754 
1759  protected static function getCachedUserInstance($a_usr_id)
1760  {
1761  if (isset(self::$userInstances[$a_usr_id])) {
1762  return self::$userInstances[$a_usr_id];
1763  }
1764 
1765  self::$userInstances[$a_usr_id] = new ilObjUser($a_usr_id);
1766  return self::$userInstances[$a_usr_id];
1767  }
1768 
1772  public function formatLinebreakMessage($a_message)
1773  {
1774  return $a_message;
1775  }
1776 }
static _lookupName($a_user_id)
lookup user name
Class ilMailError.
if($orgName !==null) if($spconfig->hasValue('contacts')) $email
Definition: metadata.php:201
markRead(array $a_mail_ids)
Class ilMailOptions this class handles user mails.
Global event handler.
getEmailRecipients(string $recipients)
$_COOKIE['client_id']
Definition: server.php:9
isSOAPEnabled()
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
getCountRecipients(string $toRecipients, string $ccRecipients, string $bccRecipients, $onlyExternalAddresses=true)
distributeMail($a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_subject, $a_message, $a_attachments, $sent_mail_id, $a_type, $a_action, $a_use_placeholders=0)
static strLen($a_string)
Definition: class.ilStr.php:78
$context
Definition: webdav.php:25
$contextParameters
getNewDraftId($usrId, $folderId)
const ILIAS_HOST
global $DIC
Definition: saml.php:7
static getCachedUserInstance($a_usr_id)
Class ilFileDataMail.
checkMail(string $to, string $cc, string $bcc, string $subject)
__get($name)
Magic interceptor method __get Used to include files / instantiate objects at runtime.
existsRecipient(string $newRecipient, string $existingRecipients)
const CONTEXT_CRON
static $userInstances
if(!array_key_exists('StateId', $_REQUEST)) $id
Class ilMailException.
static _lookupId($a_user_str)
Lookup id by login.
setSaveInSentbox($a_save_in_sentbox)
updateDraft( $a_folder_id, $a_attachments, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_m_type, $a_m_email, $a_m_subject, $a_m_message, $a_draft_id=0, $a_use_placeholders=0, $a_tpl_context_id=null, $a_tpl_context_params=array())
static _lookupGender($a_user_id)
Lookup gender.
deleteMailsOfFolder($a_folder_id)
$save_in_sentbox
static getExternalEmailsByUser(ilObjUser $user, ilMailOptions $mail_options=null)
withContextParameters(array $parameters)
const MAIL_SUBJECT_PREFIX
$mailAddressParserFactory
$a_type
Definition: workflow.php:92
static _getLanguage($a_lang_key='')
Get langauge object.
Class ilMailRfc822AddressParserFactory.
foreach($_POST as $key=> $value) $res
Class ilMimeMail.
withContextId(string $contextId)
isSystemMail()
$mail_obj_ref_id
$appendInstallationSignature
$values
__construct( $a_user_id, ilMailAddressTypeFactory $mailAddressTypeFactory=null, ilMailRfc822AddressParserFactory $mailAddressParserFactory=null, \ilAppEventHandler $eventHandler=null)
getCountRecipient(string $recipients, $onlyExternalAddresses=true)
Mail Box class Base class for creating and handling mail boxes.
checkRecipients(string $recipients)
Check if recipients are valid.
$table_mail_saved
countMailsOfFolder($a_folder_id)
getNextMail($a_mail_id)
$query
getMail($a_mail_id)
getSaveInSentbox()
$user
Definition: migrateto20.php:57
appendInstallationSignature($a_flag=null)
Setter/Getter for appending the installation signarue.
savePostData( $a_user_id, $a_attachments, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_m_type, $a_m_email, $a_m_subject, $a_m_message, $a_use_placeholders, $a_tpl_context_id=null, $a_tpl_ctx_params=array())
save post data in table public
getMailObjectReferenceId()
getMailsOfFolder($a_folder_id, $filter=array())
$mailAddressTypeFactory
sendMail($a_rcp_to, $a_rcp_cc, $a_rcp_bc, $a_m_subject, $a_m_message, $a_attachment, $a_type, $a_use_placeholders=0)
Should be used to send notifcations over the internal or external mail channel.
$row
validateRecipients(string $to, string $cc, string $bcc)
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
formatLinebreakMessage($a_message)
replacePlaceholders($a_message, $a_user_id=0, $replace_empty=true)
$parser
Definition: BPMN2Parser.php:23
$errors
Definition: index.php:6
static _getHttpPath()
static _lookupPref($a_usr_id, $a_keyword)
deleteMails(array $mailIds)
static _getAutoGeneratedMessageString(ilLanguage $lang=null)
Get auto generated info string.
static getMailObjectRefId()
Determines the reference id of the mail object and stores this information in a local cache variable...
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349
fetchMailData($a_row)
sendInternalMail( $a_folder_id, $a_sender_id, $a_attachments, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_status, $a_m_type, $a_m_email, $a_m_subject, $a_m_message, $a_user_id=0, $a_use_placeholders=0, $a_tpl_context_id=null, $a_tpl_context_params=array())
save mail in folder private
language handling
getUserIds(array $recipients)
static getLogger($a_component_id)
Get component logger.
formatNamesForOutput($a_recipients)
Prepends the fullname of each ILIAS login name (if user has a public profile) found in the passed str...
enableSOAP($a_status)
Define if external mails should be sent using SOAP client or not.
static getType()
Get context type.
static getSubjectPrefix()
Get text that will be prepended to auto generated mails.
markUnread(array $a_mail_ids)
Class ilMailAddressTypeFactory.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
moveMailsToFolder(array $mailIds, int $folderId)
readMailObjectReferenceId()
Read and set the mail object ref id (administration node)
Class ilMailAddress.
getPreviousMail($a_mail_id)
saveInSentbox($a_attachment, $a_rcp_to, $a_rcp_cc, $a_rcp_bcc, $a_type, $a_m_subject, $a_m_message)
Stores a message in the sent bod of the current user.
static _getInstallationSignature()
$data
Definition: bench.php:6
static getSalutation($a_usr_id, ilLanguage $a_language=null)
Class ilMailAddressListImpl.
parseAddresses($addresses)
Explode recipient string, allowed separators are &#39;,&#39; &#39;;&#39; &#39; &#39; Returns an array with recipient ilMailAd...
saveAttachments($a_attachments)