11 use Elgg\Traits\Loggable;
13 use Laminas\Mail\Header\ContentType;
14 use Laminas\Mail\Message as MailMessage;
15 use Laminas\Mail\Transport\TransportInterface;
16 use Laminas\Mime\Message as MimeMessage;
17 use Laminas\Mime\Exception\InvalidArgumentException;
18 use Laminas\Mime\Part;
44 protected TransportInterface $mailer,
60 $email = $this->events->triggerResults(
'prepare',
'system:email', [],
$email);
62 $msg =
"'prepare','system:email' event handlers should return an instance of " . Email::class;
66 $is_valid =
$email->getFrom() && !empty(
$email->getTo());
67 if (!$this->events->triggerResults(
'validate',
'system:email', [
'email' =>
$email], $is_valid)) {
71 return $this->transport(
$email);
83 if ($this->events->triggerResults(
'transport',
'system:email', [
'email' =>
$email], false)) {
98 'MIME-Version' =>
'1.0',
99 'Content-Transfer-Encoding' =>
'8bit',
108 $message->getHeaders()->addHeaderLine(
"{$name}: {$value}");
114 }
catch (InvalidArgumentException $e) {
115 $this->
getLogger()->error($e->getMessage());
120 $message->setSubject($this->prepareSubject(
$email->getSubject()));
128 $ct =
$message->getHeaders()->get(
'Content-Type');
129 if ($ct instanceof ContentType) {
130 $ct->addParameter(
'format',
'flowed');
135 }
catch (RuntimeException $e) {
136 $this->
getLogger()->error($e->getMessage());
169 $multipart = new MimeMessage();
170 $raw_body =
$email->getBody();
171 $message_content_type =
'';
175 $multipart->addPart($plain_text_part);
180 $multipart->addPart($this->makeHtmlPart(
$email));
181 $message_content_type = Mime::MULTIPART_ALTERNATIVE;
190 $multipart_content =
new Part($multipart->generateMessage());
191 $multipart_content->setType(Mime::MULTIPART_ALTERNATIVE);
192 $multipart_content->setBoundary($multipart->getMime()->boundary());
194 $body =
new MimeMessage();
195 $body->addPart($multipart_content);
199 $body->addPart($attachement);
202 $message_content_type = Mime::MULTIPART_MIXED;
207 if (!empty($message_content_type)) {
212 if (!
$header instanceof ContentType) {
216 $header->setType($message_content_type);
217 $header->addParameter(
'boundary',
$body->getMime()->boundary());
233 $mail_params =
$email->getParams();
234 $html_text =
elgg_extract(
'html_message', $mail_params);
235 if ($html_text instanceof Part) {
239 if (is_string($html_text)) {
244 $html_text = $this->html_formatter->inlineCss($html_text, $css);
247 $html_text = $this->makeHtmlBody([
248 'subject' =>
$email->getSubject(),
255 $html_text = $this->html_formatter->normalizeUrls($html_text);
256 if (empty($html_text)) {
257 return new HtmlPart($html_text);
261 if ($email_html_part_images !==
'base64' && $email_html_part_images !==
'attach') {
262 return new HtmlPart($html_text);
265 $images = $this->findImages($html_text);
266 if (empty($images)) {
267 return new HtmlPart($html_text);
270 if ($email_html_part_images ===
'base64') {
271 foreach ($images as
$url) {
273 $image_url = substr(
$url, 1, -1);
276 $image = $this->image_fetcher->getImage($image_url);
283 $base64image =
$image[
'content-type'] .
';charset=UTF-8;base64,' . base64_encode(
$image[
'data']);
286 $replacement = str_replace($image_url,
"data:{$base64image}",
$url);
289 $html_text = str_replace(
$url, $replacement, $html_text);
292 return new HtmlPart($html_text);
297 foreach ($images as
$url) {
299 $image_url = substr(
$url, 1, -1);
302 $image = $this->image_fetcher->getImage($image_url);
313 $replacement = str_replace($image_url,
"cid:{$uid}",
$url);
315 $html_text = str_replace(
$url, $replacement, $html_text);
320 $message->addPart(
new HtmlPart($html_text));
323 $attachment = Attachment::factory([
325 'content' => $image_data[
'data'],
326 'type' => $image_data[
'content-type'],
327 'filename' => $image_data[
'name'],
328 'encoding' => Mime::ENCODING_BASE64,
329 'disposition' => Mime::DISPOSITION_INLINE,
330 'charset' =>
'UTF-8',
336 $part =
new Part(
$message->generateMessage());
337 $part->setType(Mime::MULTIPART_RELATED);
338 $part->setBoundary(
$message->getMime()->boundary());
362 $options[
'body'] = $this->views->renderView(
'email/elements/body',
$options);
364 $css_views = $this->views->renderView(
'elements/variables.css',
$options);
365 $css_views .= $this->views->renderView(
'email/email.css',
$options);
367 $minifier = new \MatthiasMullie\Minify\CSS($css_views);
368 $css = $minifier->minify();
372 $html = $this->views->renderView(
'email/elements/html',
$options);
374 return $this->html_formatter->inlineCss(
$html, $css);
391 $pattern =
'/\ssrc=([\'"]\S+[\'"])/i';
393 preg_match_all($pattern,
$text, $matches);
395 if (empty($matches) || !isset($matches[1])) {
400 return array_unique($matches[1]);
getLogger()
Returns logger.
if(! $user||! $user->canDelete()) $name
$attachments
Outputs attachments.
foreach($categories as $key=> $category) $body
Gives access to CSS variables in the system.
Fetch external images server side.
prepareSubject(string $subject)
Prepare the subject string.
setMessageBody(MailMessage $message, Email $email)
Build the body part of the e-mail message.
makeHtmlBody(array $options=[])
Create the HTML content for use in a HTML email part.
findImages(string $text)
Find img src's in text.
send(Email $email)
Sends an email.
makeHtmlPart(\Elgg\Email $email)
Make the html part of the e-mail message.
__construct(protected Config $config, protected EventsService $events, protected TransportInterface $mailer, protected HtmlFormatter $html_formatter, protected ViewsService $views, protected ImageFetcherService $image_fetcher)
Constructor.
transport(Email $email)
Transports an email.
Plaintext part for email.
Exception thrown if an error which can only be found on runtime occurs.
Support class for MultiPart Mime Messages.
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
foreach($periods as $period) $header
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
$config
Advanced site settings, debugging section.
$subject
HTML body of an email.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
elgg_get_current_language()
Get the current system/user language or 'en'.
elgg_strip_tags(string $string, ?string $allowable_tags=null)
Strip tags and offer plugins the chance.
$html
A wrapper to render a section of the page shell.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.