public function load(ObjectManager $manager) { $templateDir = dirname(__DIR__) . '/email_templates'; $languages = scandir($templateDir); foreach ($languages as $language) { if ($language[0] === '.') { continue; } $languageDir = $templateDir . DIRECTORY_SEPARATOR . $language; if (!is_dir($languageDir) || !is_readable($languageDir)) { continue; } $templates = scandir($languageDir); foreach ($templates as $template) { if ($template[0] === '.') { continue; } $templateFile = $languageDir . DIRECTORY_SEPARATOR . $template; if (!is_readable($templateFile) || !is_file($templateFile) || pathinfo($templateFile, PATHINFO_EXTENSION) !== 'twig') { echo 'ignoring template ' . $templateFile . PHP_EOL; continue; } $translationKey = 'skelleton.registration.email.' . basename($templateFile, '.' . pathinfo($templateFile, PATHINFO_EXTENSION)) . '.template'; $found = $manager->getRepository(Translation::class)->findOneBy(array('locale' => $language, 'textDomain' => 'default', 'translationKey' => $translationKey)); if ($found) { $translation = $found; } else { $translation = new Translation(); $translation->setLocale($language)->setTranslationKey($translationKey); $manager->persist($translation); } $translation->setTranslation(file_get_contents($templateFile)); } } for ($i = 0; $i < static::EMAIL_MSB; $i++) { foreach ($languages as $language) { if ($language[0] === '.') { continue; } $languageDir = $templateDir . DIRECTORY_SEPARATOR . $language; if (!is_dir($languageDir) || !is_readable($languageDir)) { continue; } $subjectKey = SiteRegistrationOptions::getSubjectTemplateKey(1 << $i); $found = $manager->getRepository(Translation::class)->findOneBy(array('locale' => $language, 'textDomain' => 'default', 'translationKey' => $subjectKey)); if ($found) { $translation = $found; } else { $translation = new Translation(); $translation->setLocale($language)->setTranslationKey($subjectKey); $manager->persist($translation); } $translation->setTranslation($this->getEmailSubject($subjectKey, $language)); } } $manager->flush(); }
public function getMessage($flag, $user, $parameters = null) { if (!($this->getRegistrationOptions()->getRegistrationEmailFlag() & $flag)) { return null; } if ($parameters === null) { $parameters = array('user' => $user); } $templateKey = SiteRegistrationOptions::getEmailTemplateKey($flag); if (!$templateKey) { return null; } $translator = $this->getTranslator(); // set locale to user-defined language $translatorLocale = $translator->getLocale(); if ($user->getLocale()) { $translator->setLocale($user->getLocale()); } $options = $this->getRegistrationOptions(); $emailFrom = $options->getRegistrationNotificationFrom(); $fromName = ''; $fromMail = ''; if (strpos($emailFrom, '<') !== false) { $pos1 = strpos($emailFrom, '<'); $pos2 = strpos($emailFrom, '>'); $fromName = trim(substr($emailFrom, 0, $pos1)); $fromMail = trim(substr($emailFrom, $pos1 + 1, $pos2 - $pos1 - 1)); } else { $fromMail = $emailFrom; $fromName = ucwords(trim(substr($emailFrom, 0, strpos($emailFrom, '@')))); } $transport = $this->getTransport(); $message = $transport->createHtmlMessage(array('name' => $fromName, 'email' => $fromMail), $user->getEmail(), $translator->translate(SiteRegistrationOptions::getSubjectTemplateKey($flag)), SiteRegistrationOptions::getEmailTemplateKey($flag), $parameters); $message->getHeaders()->get('content-type')->setType('multipart/alternative'); $messageParts = $message->getBody()->getParts(); $messageParts[0]->setCharset('utf-8'); $messageParts[1]->setCharset('utf-8'); $textPart = $messageParts[0]; $htmlContent = $messageParts[1]->getContent(); $textContent = str_replace(array("\r\n", "\r", "\n"), "", $htmlContent); $textContent = str_replace(array("<br>", "<br />"), PHP_EOL, $htmlContent); $textContent = strip_tags($textContent); $textPart->setContent($textContent); // restore locale $translator->setLocale($translatorLocale); return $message; }