14 use Laminas\Mail\Message as MailMessage;
16 use Laminas\Mime\Message as MimeMessage;
80 TransportInterface $mailer,
87 $this->hooks = $hooks;
88 $this->mailer = $mailer;
89 $this->html_formatter = $html_formatter;
91 $this->image_fetcher = $image_fetcher;
92 $this->css_compiler = $css_compiler;
104 $email = $this->hooks->trigger(
'prepare',
'system:email', null, $email);
105 if (!$email instanceof
Email) {
106 $msg =
"'prepare','system:email' hook handlers should return an instance of " . Email::class;
114 $is_valid = $email->
getFrom() && !empty($email->
getTo());
115 if (!$this->hooks->trigger(
'validate',
'system:email', $hook_params, $is_valid)) {
119 return $this->transport($email);
132 if ($this->hooks->trigger(
'transport',
'system:email', [
'email' => $email],
false)) {
147 'MIME-Version' =>
'1.0',
148 'Content-Transfer-Encoding' =>
'8bit',
157 $message->getHeaders()->addHeaderLine(
"{$name}: {$value}");
164 $this->
getLogger()->error($e->getMessage());
173 $message = $this->hooks->trigger(
'zend:message',
'system:email', [
'email' => $email],
$message);
177 $ct =
$message->getHeaders()->get(
'Content-Type');
178 if ($ct instanceof ContentType) {
179 $ct->addParameter(
'format',
'flowed');
185 $this->
getLogger()->error($e->getMessage());
202 $subject = html_entity_decode($subject, ENT_QUOTES,
'UTF-8');
204 $subject = preg_replace(
"/(\r\n|\r|\n)/",
" ", $subject);
205 return trim($subject);
218 $multipart =
new MimeMessage();
220 $message_content_type =
'';
224 $multipart->addPart($plain_text_part);
229 $multipart->addPart($this->makeHtmlPart($email));
230 $message_content_type = Mime::MULTIPART_ALTERNATIVE;
239 $multipart_content =
new Part($multipart->generateMessage());
240 $multipart_content->setType(Mime::MULTIPART_ALTERNATIVE);
241 $multipart_content->setBoundary($multipart->getMime()->boundary());
243 $body =
new MimeMessage();
244 $body->addPart($multipart_content);
248 $body->addPart($attachement);
251 $message_content_type = Mime::MULTIPART_MIXED;
254 $message->setBody(
$body);
256 if (!empty($message_content_type)) {
261 if (!$header instanceof ContentType) {
265 $header->setType($message_content_type);
266 $header->addParameter(
'boundary',
$body->getMime()->boundary());
282 $mail_params = $email->getParams();
283 $html_text =
elgg_extract(
'html_message', $mail_params);
284 if ($html_text instanceof Part) {
288 if (is_string($html_text)) {
293 $html_text = $this->html_formatter->inlineCss($html_text,
$css);
296 $html_text = $this->makeHtmlBody([
297 'subject' => $email->getSubject(),
298 'body' =>
elgg_extract(
'html_body', $mail_params, $email->getBody()),
304 $html_text = $this->html_formatter->normalizeUrls($html_text);
305 if (empty($html_text)) {
310 if ($email_html_part_images !==
'base64' && $email_html_part_images !==
'attach') {
314 $images = $this->findImages($html_text);
315 if (empty($images)) {
319 if ($email_html_part_images ===
'base64') {
320 foreach ($images as
$url) {
322 $image_url =
substr($url, 1, -1);
325 $image = $this->image_fetcher->getImage($image_url);
335 $replacement =
str_replace($image_url,
"data:{$base64image}", $url);
338 $html_text =
str_replace($url, $replacement, $html_text);
346 foreach ($images as
$url) {
348 $image_url =
substr($url, 1, -1);
351 $image = $this->image_fetcher->getImage($image_url);
362 $replacement =
str_replace($image_url,
"cid:{$uid}", $url);
364 $html_text =
str_replace($url, $replacement, $html_text);
372 $attachment = Attachment::factory([
374 'content' => $image_data[
'data'],
375 'type' => $image_data[
'content-type'],
376 'filename' => $image_data[
'name'],
377 'encoding' => Mime::ENCODING_BASE64,
378 'disposition' => Mime::DISPOSITION_INLINE,
379 'charset' =>
'UTF-8',
385 $part =
new Part(
$message->generateMessage());
386 $part->setType(Mime::MULTIPART_RELATED);
387 $part->setBoundary(
$message->getMime()->boundary());
411 $options[
'body'] = $this->views->renderView(
'email/elements/body',
$options);
413 $css_compiled = $this->css_compiler->compile($this->views->renderView(
'email/email.css',
$options));
414 $minifier = new \MatthiasMullie\Minify\CSS($css_compiled);
415 $css = $minifier->minify();
419 $html = $this->views->renderView(
'email/elements/html',
$options);
421 return $this->html_formatter->inlineCss(
$html,
$css);
438 $pattern =
'/\ssrc=([\'"]\S+[\'"])/i';
440 preg_match_all($pattern, $text, $matches);
442 if (empty($matches) || !isset($matches[1])) {
447 return array_unique($matches[1]);
elgg_get_current_language()
Get the current system/user language or "en".
getTo()
Returns recipient address.
findImages(string $text)
Find img src's in text.
Exception thrown if an argument is not of the expected type.
Exception thrown if an error which can only be found on runtime occurs.
if(!$user||!$user->canDelete()) $name
elgg_strip_tags($string, $allowable_tags=null)
Strip tags and offer plugins the chance.
getBcc()
Returns recipient address from bcc.
prepareSubject(string $subject)
Prepare the subject string.
makeHtmlPart(\Elgg\Email $email)
Make the html part of the e-mail message.
transport(Email $email)
Transports an email.
__construct(Config $config, PluginHooksService $hooks, TransportInterface $mailer, HtmlFormatter $html_formatter, ViewsService $views, ImageFetcherService $image_fetcher, CssCompiler $css_compiler)
Constructor.
setMessageBody(MailMessage $message, Email $email)
Build the body part of the e-mail message.
$config
Advanced site settings, debugging section.
trait Loggable
Enables adding a logger.
Fetch external images server side.
send(Email $email)
Sends an email.
getCc()
Returns recipient address from cc.
getSubject()
Returns the subject.
makeHtmlBody(array $options=[])
Create the HTML content for use in a HTML email part.
getBody()
Returns email body.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Compile CSS with CSSCrush.
getLogger()
Returns logger.
Plaintext part for email.
$attachments
Outputs attachments.
getHeaders()
Returns headers.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
getFrom()
Returns sender address.
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
getAttachments()
Get all attachments.