14 use Laminas\Mail\Message as MailMessage;
16 use Laminas\Mime\Message as MimeMessage;
45 protected TransportInterface $mailer,
62 $email = $this->events->triggerResults(
'prepare',
'system:email', [], $email);
63 if (!$email instanceof
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}");
117 $this->
getLogger()->error($e->getMessage());
126 $message = $this->events->triggerResults(
'zend:message',
'system:email', [
'email' => $email],
$message);
130 $ct =
$message->getHeaders()->get(
'Content-Type');
131 if ($ct instanceof ContentType) {
132 $ct->addParameter(
'format',
'flowed');
138 $this->
getLogger()->error($e->getMessage());
155 $subject = html_entity_decode($subject, ENT_QUOTES,
'UTF-8');
157 $subject = preg_replace(
"/(\r\n|\r|\n)/",
' ', $subject);
158 return trim($subject);
171 $multipart =
new MimeMessage();
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;
207 $message->setBody(
$body);
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(),
251 'body' =>
elgg_extract(
'html_body', $mail_params, $email->getBody()),
257 $html_text = $this->html_formatter->normalizeUrls($html_text);
258 if (empty($html_text)) {
263 if ($email_html_part_images !==
'base64' && $email_html_part_images !==
'attach') {
267 $images = $this->findImages($html_text);
268 if (empty($images)) {
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);
288 $replacement =
str_replace($image_url,
"data:{$base64image}", $url);
291 $html_text =
str_replace($url, $replacement, $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);
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]);
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.
transport(Email $email)
Transports an email.
setMessageBody(MailMessage $message, Email $email)
Build the body part of the e-mail message.
$html
A wrapper to render a section of the page shell.
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.
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
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.
__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.
elgg_strip_tags(string $string, string $allowable_tags=null)
Strip tags and offer plugins the chance.
getAttachments()
Get all attachments.