private function getLatte()
{
if (!isset($this->latte)) {
$this->latte = new Latte\Engine();
$this->latte->setTempDirectory($this->tempDir);
$this->latte->setAutoRefresh(FALSE);
$this->latte->onCompile[] = function (Latte\Engine $latte) {
$set = new Latte\Macros\MacroSet($latte->getCompiler());
$set->addMacro('link', 'echo %escape(call_user_func($getLink, %node.word, %node.array))');
};
$this->latte->addFilter('attachmentLabel', function (MimePart $attachment) {
$contentDisposition = $attachment->getHeader('Content-Disposition');
$contentType = $attachment->getHeader('Content-Type');
$matches = Strings::match($contentDisposition, '#filename="(.+?)"#');
return ($matches ? "{$matches['1']} " : '') . "({$contentType})";
});
$this->latte->addFilter('plainText', function (MimePart $part) {
$ref = new \ReflectionProperty('Nette\\Mail\\MimePart', 'parts');
$ref->setAccessible(TRUE);
$queue = array($part);
for ($i = 0; $i < count($queue); $i++) {
/** @var MimePart $subPart */
foreach ($ref->getValue($queue[$i]) as $subPart) {
$contentType = $subPart->getHeader('Content-Type');
if (Strings::startsWith($contentType, 'text/plain') && $subPart->getHeader('Content-Transfer-Encoding') !== 'base64') {
// Take first available plain text
return (string) $subPart->getBody();
} elseif (Strings::startsWith($contentType, 'multipart/alternative')) {
$queue[] = $subPart;
}
}
}
return $part->getBody();
});
}
return $this->latte;
}