public function render($params = []) { $path = explode('.', $this->template); $filePath = Yii::getPathOfAlias($this->template) . '.php'; if (count($path) == 2) { $filePath = Yii::getPathOfAlias(($path[0] == 'plansys' ? 'application' : 'app') . ".views.layouts.email." . $path[1]) . ".php"; } if (!is_file($filePath)) { throw new CException('File `' . $filePath . '` not found'); } extract($params); ob_start(); include $filePath; $result = ob_get_clean(); $htmldoc = new InlineStyle($result); $htmldoc->applyStylesheet($htmldoc->extractStylesheets()); return $this->renderCache = $htmldoc->getHTML(); }
/** * Send contact form data by email. * * @return boolean */ protected function send() { if (empty($this->assignation)) { throw new \Exception("Can’t send a contact form without data.", 1); } $emailBody = $this->templating->render($this->emailTemplate, $this->assignation); /* * inline CSS */ $htmldoc = new InlineStyle($emailBody); $htmldoc->applyStylesheet(file_get_contents(ROADIZ_ROOT . $this->emailStylesheet)); if (null === $this->receiver) { $this->receiver = SettingsBag::get('email_sender'); } /* * Add subject */ if (null !== $this->subject) { $this->subject = trim(strip_tags($this->subject)); } else { $this->subject = $this->translator->trans('new.contact.form.%site%', ['%site%' => SettingsBag::get('site_name')]); } // Create the message $this->message = \Swift_Message::newInstance()->setSubject($this->subject)->setTo([$this->receiver])->setBody($htmldoc->getHTML(), 'text/html'); if (null !== $this->sender) { // Set the From address with an associative array $this->message->setFrom([$this->sender]); } /* * Attach files */ foreach ($this->uploadedFiles as $uploadedFile) { $attachment = \Swift_Attachment::fromPath($uploadedFile->getRealPath())->setFilename($uploadedFile->getClientOriginalName()); $this->message->attach($attachment); } // Send the message return $this->mailer->send($this->message); }
/** * Export the newsletter in HTML with or without inline CSS * * @param Symfony\Component\HttpFoundation\Request $request * @param int $newsletterId * @param int $inline * * @return Symfony\Component\HttpFoundation\Response */ public function exportAction(Request $request, $newsletterId, $inline) { $newsletter = $this->getService("em")->find("RZ\\Roadiz\\Core\\Entities\\Newsletter", $newsletterId); $filename = $newsletter->getNode()->getNodeName(); $content = $this->getNewsletterHTML($request, $newsletter); // Get all css link in the newsletter $cssContent = DomHandler::getExternalStyles($content); if ((bool) $inline === true) { // inline newsletter html with css $htmldoc = new InlineStyle($content); $htmldoc->applyStylesheet($cssContent); $htmldoc = $htmldoc->getHtml(); $filename .= "-inlined"; $content = $htmldoc; } // Remove all link element and add style balise with all css file content $htmldoc = DomHandler::replaceExternalStylesheetsWithStyle($content, $cssContent); // Generate response $response = new Response(); // Set headers $response->headers->set('Content-type', "text/html"); $response->headers->set('Content-Disposition', 'attachment; filename= "' . $filename . '.html";'); $response->setContent($htmldoc); return $response; }
public function html($id) { $htmldoc = new InlineStyle(file_get_contents(url('campagne/' . $id))); $htmldoc->applyStylesheet($htmldoc->extractStylesheets()); $html = $htmldoc->getHTML(); $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html); return $html; }
/** * Send an email to reset user password. * * @param UrlGenerator $urlGenerator * * @return boolean */ public function sendPasswordResetLink(UrlGenerator $urlGenerator) { $emailContact = SettingsBag::get('email_sender'); if (empty($emailContact)) { $emailContact = "*****@*****.**"; } $siteName = SettingsBag::get('site_name'); if (empty($siteName)) { $siteName = "Unnamed site"; } $assignation = ['resetLink' => $urlGenerator->generate('loginResetPage', ['token' => $this->user->getConfirmationToken()], true), 'user' => $this->user, 'site' => $siteName, 'mailContact' => $emailContact]; $emailBody = Kernel::getService('twig.environment')->render('users/reset_password_email.html.twig', $assignation); /* * inline CSS */ $htmldoc = new InlineStyle($emailBody); $htmldoc->applyStylesheet(file_get_contents(ROADIZ_ROOT . "/src/Roadiz/CMS/Resources/css/transactionalStyles.css")); // Create the message $message = \Swift_Message::newInstance(); // Give the message a subject $message->setSubject(Kernel::getService('translator')->trans('reset.password.request')); // Set the From address with an associative array $message->setFrom([$emailContact => $siteName]); // Set the To addresses with an associative array $message->setTo([$this->user->getEmail()]); // Give it a body $message->setBody($htmldoc->getHTML(), 'text/html'); // Send the message return Kernel::getService('mailer')->send($message); }
function testLinkedMediaStylesheets31() { $htmldoc = new InlineStyle(file_get_contents($this->basedir . '/testLinkedMediaStylesheets31.html')); $htmldoc->applyStylesheet($htmldoc->extractStylesheets(null, $this->basedir)); $expected = <<<HTML <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title>Example</title> <link rel="stylesheet" href="external.css" media="print"> </head> <body> <h1>An example title</h1> <p>Paragraph <strong style="font-weight: bold">1</strong></p> </body> </html> HTML; $this->assertEquals($expected, $htmldoc->getHTML()); }
/** * Send an answer form by Email. * * @param array $assignation * @param string $receiver * @param \Twig_Environment $twigEnv * @param \Swift_Mailer $mailer * * @return boolean */ public static function sendAnswer($assignation, $receiver, \Twig_Environment $twigEnv, \Swift_Mailer $mailer) { $emailBody = $twigEnv->render('forms/answerForm.html.twig', $assignation); /* * inline CSS */ $htmldoc = new InlineStyle($emailBody); $htmldoc->applyStylesheet(file_get_contents(ROADIZ_ROOT . "/src/Roadiz/CMS/Resources/css/transactionalStyles.css")); if (empty($receiver)) { $receiver = SettingsBag::get('email_sender'); } // Create the message} $message = \Swift_Message::newInstance(); // Give the message a subject $message->setSubject($assignation['title']); // Set the From address with an associative array $message->setFrom([SettingsBag::get('email_sender')]); // Set the To addresses with an associative array $message->setTo([$receiver]); // Give it a body $message->setBody($htmldoc->getHTML(), 'text/html'); // Send the message return $mailer->send($message); }
/** * Send a contact form by Email. * * @param array $assignation * @param string $receiver * @param string|null $subject * @param array $files * * @return boolean */ protected function sendContactForm($assignation, $receiver, $subject = null, $files = null) { $emailBody = $this->getService('twig.environment')->render('forms/contactForm.html.twig', $assignation); /* * inline CSS */ $htmldoc = new InlineStyle($emailBody); $htmldoc->applyStylesheet(file_get_contents(ROADIZ_ROOT . "/src/Roadiz/CMS/Resources/css/transactionalStyles.css")); if (null !== $subject) { $subject = trim(strip_tags($subject)); } else { $subject = $this->getTranslator()->trans('new.contact.form.%site%', ['%site%' => SettingsBag::get('site_name')]); } // Create the message $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom([$assignation['email']])->setTo([$receiver])->setBody($htmldoc->getHTML(), 'text/html'); /* * Attach files */ foreach ($files as $uploadedFile) { $attachment = \Swift_Attachment::fromPath($uploadedFile->getRealPath())->setFilename($uploadedFile->getClientOriginalName()); $message->attach($attachment); } // Send the message return $this->getService('mailer')->send($message); }
/** * Generates the E-mail body * * @return $this */ private function generateBody() { if (!empty($this->htmlBody) && !empty($this->template)) { $mail = ['charset' => 'UTF-8', 'title' => $this->subject, 'body' => $this->htmlBody, 'signature' => $this->getHtmlSignature(), 'url_web_view' => $this->urlWeb]; $this->view->assign('mail', $mail); $htmlDocument = new InlineStyle($this->view->fetchTemplate($this->template)); $htmlDocument->applyStylesheet($htmlDocument->extractStylesheets()); $this->phpMailer->msgHTML($htmlDocument->getHTML()); // Fallback for E-mail clients which don't support HTML E-mails if (!empty($this->body)) { $this->phpMailer->AltBody = $this->decodeHtmlEntities($this->body . $this->getTextSignature()); } else { $this->phpMailer->AltBody = $this->phpMailer->html2text($this->htmlBody . $this->getHtmlSignature(), true); } } else { $this->phpMailer->Body = $this->decodeHtmlEntities($this->body . $this->getTextSignature()); } return $this; }