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;
45 protected TransportInterface $mailer,
62 $email = $this->events->triggerResults(
'prepare',
'system:email', [],
$email);
64 $msg =
"'prepare','system:email' event handlers should return an instance of " . Email::class;
68 $is_valid =
$email->getFrom() && !empty(
$email->getTo());
69 if (!$this->events->triggerResults(
'validate',
'system:email', [
'email' =>
$email], $is_valid)) {
73 return $this->transport(
$email);
85 if ($this->events->triggerResults(
'transport',
'system:email', [
'email' =>
$email], false)) {
100 'MIME-Version' =>
'1.0',
101 'Content-Transfer-Encoding' =>
'8bit',
110 $message->getHeaders()->addHeaderLine(
"{$name}: {$value}");
116 }
catch (InvalidArgumentException $e) {
117 $this->
getLogger()->error($e->getMessage());
122 $message->setSubject($this->prepareSubject(
$email->getSubject()));
130 $ct =
$message->getHeaders()->get(
'Content-Type');
131 if ($ct instanceof ContentType) {
132 $ct->addParameter(
'format',
'flowed');
137 }
catch (RuntimeException $e) {
138 $this->
getLogger()->error($e->getMessage());
171 $multipart = new MimeMessage();
172 $raw_body =
$email->getBody();
173 $message_content_type =
'';
177 $multipart->addPart($plain_text_part);
182 $multipart->addPart($this->makeHtmlPart(
$email));
183 $message_content_type = Mime::MULTIPART_ALTERNATIVE;
192 $multipart_content =
new Part($multipart->generateMessage());
193 $multipart_content->setType(Mime::MULTIPART_ALTERNATIVE);
194 $multipart_content->setBoundary($multipart->getMime()->boundary());
196 $body =
new MimeMessage();
197 $body->addPart($multipart_content);
201 $body->addPart($attachement);
204 $message_content_type = Mime::MULTIPART_MIXED;
209 if (!empty($message_content_type)) {
214 if (!
$header instanceof ContentType) {
218 $header->setType($message_content_type);
219 $header->addParameter(
'boundary',
$body->getMime()->boundary());
235 $mail_params =
$email->getParams();
236 $html_text =
elgg_extract(
'html_message', $mail_params);
237 if ($html_text instanceof Part) {
241 if (is_string($html_text)) {
246 $html_text = $this->html_formatter->inlineCss($html_text,
$css);
249 $html_text = $this->makeHtmlBody([
250 'subject' =>
$email->getSubject(),
257 $html_text = $this->html_formatter->normalizeUrls($html_text);
258 if (empty($html_text)) {
259 return new HtmlPart($html_text);
263 if ($email_html_part_images !==
'base64' && $email_html_part_images !==
'attach') {
264 return new HtmlPart($html_text);
267 $images = $this->findImages($html_text);
268 if (empty($images)) {
269 return new HtmlPart($html_text);
272 if ($email_html_part_images ===
'base64') {
273 foreach ($images as
$url) {
275 $image_url = substr(
$url, 1, -1);
278 $image = $this->image_fetcher->getImage($image_url);
285 $base64image =
$image[
'content-type'] .
';charset=UTF-8;base64,' . base64_encode(
$image[
'data']);
288 $replacement = str_replace($image_url,
"data:{$base64image}",
$url);
291 $html_text = str_replace(
$url, $replacement, $html_text);
294 return new HtmlPart($html_text);
299 foreach ($images as
$url) {
301 $image_url = substr(
$url, 1, -1);
304 $image = $this->image_fetcher->getImage($image_url);
315 $replacement = str_replace($image_url,
"cid:{$uid}",
$url);
317 $html_text = str_replace(
$url, $replacement, $html_text);
322 $message->addPart(
new HtmlPart($html_text));
325 $attachment = Attachment::factory([
327 'content' => $image_data[
'data'],
328 'type' => $image_data[
'content-type'],
329 'filename' => $image_data[
'name'],
330 'encoding' => Mime::ENCODING_BASE64,
331 'disposition' => Mime::DISPOSITION_INLINE,
332 'charset' =>
'UTF-8',
338 $part =
new Part(
$message->generateMessage());
339 $part->setType(Mime::MULTIPART_RELATED);
340 $part->setBoundary(
$message->getMime()->boundary());
364 $options[
'body'] = $this->views->renderView(
'email/elements/body',
$options);
366 $css_compiled = $this->css_compiler->compile($this->views->renderView(
'email/email.css',
$options));
367 $minifier = new \MatthiasMullie\Minify\CSS($css_compiled);
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.
Compile CSS with CSSCrush.
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, protected CssCompiler $css_compiler)
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.
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.