14 use Laminas\Mail\Message as MailMessage;
16 use Laminas\Mime\Message as MimeMessage;
80 TransportInterface $mailer,
87 $this->events = $events;
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->events->triggerResults(
'prepare',
'system:email', [], $email);
105 if (!$email instanceof
Email) {
106 $msg =
"'prepare','system:email' event handlers should return an instance of " . Email::class;
110 $is_valid = $email->
getFrom() && !empty($email->
getTo());
111 if (!$this->events->triggerResults(
'validate',
'system:email', [
'email' => $email], $is_valid)) {
115 return $this->transport($email);
128 if ($this->events->triggerResults(
'transport',
'system:email', [
'email' => $email],
false)) {
143 'MIME-Version' =>
'1.0',
144 'Content-Transfer-Encoding' =>
'8bit',
153 $message->getHeaders()->addHeaderLine(
"{$name}: {$value}");
160 $this->
getLogger()->error($e->getMessage());
169 $message = $this->events->triggerResults(
'zend:message',
'system:email', [
'email' => $email],
$message);
173 $ct =
$message->getHeaders()->get(
'Content-Type');
174 if ($ct instanceof ContentType) {
175 $ct->addParameter(
'format',
'flowed');
181 $this->
getLogger()->error($e->getMessage());
198 $subject = html_entity_decode($subject, ENT_QUOTES,
'UTF-8');
200 $subject = preg_replace(
"/(\r\n|\r|\n)/",
' ', $subject);
201 return trim($subject);
214 $multipart =
new MimeMessage();
216 $message_content_type =
'';
220 $multipart->addPart($plain_text_part);
225 $multipart->addPart($this->makeHtmlPart($email));
226 $message_content_type = Mime::MULTIPART_ALTERNATIVE;
235 $multipart_content =
new Part($multipart->generateMessage());
236 $multipart_content->setType(Mime::MULTIPART_ALTERNATIVE);
237 $multipart_content->setBoundary($multipart->getMime()->boundary());
239 $body =
new MimeMessage();
240 $body->addPart($multipart_content);
244 $body->addPart($attachement);
247 $message_content_type = Mime::MULTIPART_MIXED;
250 $message->setBody(
$body);
252 if (!empty($message_content_type)) {
257 if (!$header instanceof ContentType) {
261 $header->setType($message_content_type);
262 $header->addParameter(
'boundary',
$body->getMime()->boundary());
278 $mail_params = $email->getParams();
279 $html_text =
elgg_extract(
'html_message', $mail_params);
280 if ($html_text instanceof Part) {
284 if (is_string($html_text)) {
289 $html_text = $this->html_formatter->inlineCss($html_text,
$css);
292 $html_text = $this->makeHtmlBody([
293 'subject' => $email->getSubject(),
294 'body' =>
elgg_extract(
'html_body', $mail_params, $email->getBody()),
300 $html_text = $this->html_formatter->normalizeUrls($html_text);
301 if (empty($html_text)) {
306 if ($email_html_part_images !==
'base64' && $email_html_part_images !==
'attach') {
310 $images = $this->findImages($html_text);
311 if (empty($images)) {
315 if ($email_html_part_images ===
'base64') {
316 foreach ($images as
$url) {
318 $image_url =
substr($url, 1, -1);
321 $image = $this->image_fetcher->getImage($image_url);
331 $replacement =
str_replace($image_url,
"data:{$base64image}", $url);
334 $html_text =
str_replace($url, $replacement, $html_text);
342 foreach ($images as
$url) {
344 $image_url =
substr($url, 1, -1);
347 $image = $this->image_fetcher->getImage($image_url);
358 $replacement =
str_replace($image_url,
"cid:{$uid}", $url);
360 $html_text =
str_replace($url, $replacement, $html_text);
368 $attachment = Attachment::factory([
370 'content' => $image_data[
'data'],
371 'type' => $image_data[
'content-type'],
372 'filename' => $image_data[
'name'],
373 'encoding' => Mime::ENCODING_BASE64,
374 'disposition' => Mime::DISPOSITION_INLINE,
375 'charset' =>
'UTF-8',
381 $part =
new Part(
$message->generateMessage());
382 $part->setType(Mime::MULTIPART_RELATED);
383 $part->setBoundary(
$message->getMime()->boundary());
407 $options[
'body'] = $this->views->renderView(
'email/elements/body',
$options);
409 $css_compiled = $this->css_compiler->compile($this->views->renderView(
'email/email.css',
$options));
410 $minifier = new \MatthiasMullie\Minify\CSS($css_compiled);
411 $css = $minifier->minify();
415 $html = $this->views->renderView(
'email/elements/html',
$options);
417 return $this->html_formatter->inlineCss(
$html,
$css);
434 $pattern =
'/\ssrc=([\'"]\S+[\'"])/i';
436 preg_match_all($pattern, $text, $matches);
438 if (empty($matches) || !isset($matches[1])) {
443 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.
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
if(!$user||!$user->canDelete()) $name
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.
__construct(Config $config, EventsService $events, TransportInterface $mailer, HtmlFormatter $html_formatter, ViewsService $views, ImageFetcherService $image_fetcher, CssCompiler $css_compiler)
Constructor.
transport(Email $email)
Transports an email.
setMessageBody(MailMessage $message, Email $email)
Build the body part of the e-mail message.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
$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.
Compile CSS with CSSCrush.
getLogger()
Returns logger.
Plaintext part for email.
foreach($periods as $period) $header
$attachments
Outputs attachments.
getHeaders()
Returns headers.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
getFrom()
Returns sender address.
elgg_strip_tags(string $string, string $allowable_tags=null)
Strip tags and offer plugins the chance.
getAttachments()
Get all attachments.