19declare(strict_types=1);
27 private const string SKIN_LOGO_PATH =
'/public/Customizing/skin/%s/images/logo';
28 private const string SKIN_CSS_PATH =
'/public/Customizing/skin/%s/mail.css';
30 private const string MAIL_LOGO_PATH =
'/public/assets/images/logo/HeaderIcon.svg';
68 $factory =
$DIC->mail()->mime()->transportFactory();
78 self::$default_transport = $transport;
86 public function Subject(
string $subject,
bool $add_prefix =
false,
string $context_prefix =
''): void
88 $this->subject = $this->subject_builder->subject(
$subject, $add_prefix, $context_prefix);
104 public function To($to): void
109 $this->sendto[] = $to;
116 public function Cc($cc): void
128 public function Bcc($bcc): void
130 if (is_array($bcc)) {
133 $this->abcc[] = $bcc;
194 string $file_type =
'',
195 string $disposition =
'inline',
196 ?
string $display_name =
null
198 if ($file_type ===
'') {
199 $file_type =
'application/octet-stream';
203 $this->actype[] = $file_type;
204 $this->adispo[] = $disposition;
205 $this->adisplay[] = $display_name;
216 foreach ($this->aattach as $attachment) {
218 if (isset($this->adisplay[$i]) && is_string($this->adisplay[$i]) && $this->adisplay[$i] !==
'') {
219 $name = $this->adisplay[$i];
223 'path' => $attachment,
238 return array_values($this->images);
245 $this->final_body_alt =
'';
246 $this->final_body =
'';
249 if (
$DIC->settings()->get(
'mail_send_html',
'0')) {
250 $skin =
$DIC[
'ilClientIniFile']->readVariable(
'layout',
'skin');
251 $style =
$DIC[
'ilClientIniFile']->readVariable(
'layout',
'style');
254 $factory =
$DIC->ui()->factory();
257 $this->prepareHTMLBody();
259 $page = $factory->layout()->page()->mail(
260 $this->getStyleSheetPath($skin, $style),
261 "cid:{$this->getLogoCid($skin, $style)}",
263 $factory->legacy()->content($this->body),
267 $this->final_body =
$renderer->render($page);
268 $this->final_body_alt = $this->removeHtmlTags($this->body);
270 $this->final_body = $this->removeHtmlTags($this->body);
276 $maybe_html = str_ireplace([
'<br />',
'<br>',
'<br/>'],
"\n", $maybe_html);
278 return html_entity_decode(strip_tags($maybe_html), ENT_QUOTES);
283 $current_dir = realpath(__DIR__);
285 while ($current_dir !==
'.') {
286 if (file_exists($current_dir . self::ROOT_DIR_IDENTIFICATION_FILE)) {
290 $current_dir = dirname($current_dir);
298 if ($this->body ===
'') {
302 $transformed_body = $this->to_html_transformation ? ($this->to_html_transformation)($this->body) : $this->body;
304 $contains_html = $this->containsHtmlBlockElementsOrLineBreaks($transformed_body);
305 if ($contains_html) {
306 $this->final_body_alt = strip_tags(str_ireplace([
'<br />',
'<br>',
'<br/>'],
"\n", $this->body));
307 $this->body = $transformed_body;
309 $this->final_body_alt = strip_tags($this->body);
310 $this->body = nl2br($transformed_body);
313 $this->body = $this->
refinery->string()->makeClickable()->transform($this->body);
318 if (str_contains($email_body,
'<') ===
false || str_contains($email_body,
'>') ===
false) {
323 $pattern =
'~</?(p|br|div|ul|ol|li|code|pre|h[1-6])\b~i';
324 if (preg_match($pattern, $email_body) === 1) {
328 return strip_tags($email_body,
'<b><u><i><a>') !== $email_body;
333 if ($skin !==
'default') {
340 $custom_path = $this->getPathToRootDirectory() . sprintf(self::SKIN_CSS_PATH,
$location);
341 if (is_file($custom_path)) {
347 return self::MAIL_CSS_PATH;
350 private function getLogoCid(
string $skin,
string $style): string
352 if ($skin !==
'default') {
359 $custom_directory = $this->getPathToRootDirectory() . sprintf(self::SKIN_LOGO_PATH,
$location);
360 if (is_dir($custom_directory) && is_readable($custom_directory)) {
361 $this->gatherImagesFromDirectory($custom_directory);
365 $path = $this->getPathToRootDirectory() . self::MAIL_LOGO_PATH;
367 return $this->addImage(
new SplFileInfo(
$path),
true);
371 foreach ($this->images as $image) {
372 if ($image[
'as_logo']) {
373 return $image[
'cid'];
377 $logo_cid = count($this->images) > 1 ? current($this->images)[
'cid'] :
null;
379 foreach ($this->images as $cid => $image) {
380 $file_name = basename($image[
'path'],
'.' . pathinfo($image[
'path'], PATHINFO_EXTENSION));
381 if (in_array(strtolower($file_name), [
'logo',
'headericon'],
true)) {
387 if (is_string($logo_cid)) {
388 $this->images[$logo_cid][
'as_logo'] =
true;
392 $path = $this->getPathToRootDirectory() . self::MAIL_LOGO_PATH;
394 return $this->addImage(
new SplFileInfo(
$path),
true);
402 if ($clear_previous) {
406 foreach (
new RegexIterator(
407 new DirectoryIterator($directory),
408 '/\.(jpg|jpeg|gif|svg|png)$/i'
410 $this->addImage($file);
414 private function addImage(SplFileInfo $file,
bool $as_logo =
false): string
416 $cid =
'img/' . $file->getFilename();
418 $this->images[$cid] = [
419 'path' => $file->getPathname(),
421 'name' => $file->getFilename(),
422 'as_logo' => $as_logo
431 $transport = self::getDefaultTransport();
436 return $transport->send($this);
containsHtmlBlockElementsOrLineBreaks(string $email_body)
Body(string $body, ?Closure $to_html_transformation=null)
Attach(string $filename, string $file_type='', string $disposition='inline', ?string $display_name=null)
getLogoCid(string $skin, string $style)
final const string MAIL_SUBJECT_PREFIX
removeHtmlTags(string $maybe_html)
const string ROOT_DIR_IDENTIFICATION_FILE
Send(?ilMailMimeTransport $transport=null)
addImage(SplFileInfo $file, bool $as_logo=false)
Closure $to_html_transformation
const string SKIN_CSS_PATH
static ilMailMimeTransport $default_transport
gatherImagesFromDirectory(string $directory, bool $clear_previous=false)
const string SKIN_LOGO_PATH
static getDefaultTransport()
static setDefaultTransport(?ilMailMimeTransport $transport)
getStyleSheetPath(string $skin, string $style)
const string MAIL_LOGO_PATH
Subject(string $subject, bool $add_prefix=false, string $context_prefix='')
ilMailMimeSubjectBuilder $subject_builder
readonly Refinery $refinery
const string MAIL_CSS_PATH
From(ilMailMimeSender $sender)
if(!file_exists('../ilias.ini.php'))