public function testUrlEncodeText()
 {
     $twigExtension = new AzineEmailTwigExtension();
     $percent = "%";
     $amp = "&";
     $backslash = "\\";
     $lineBreak = "\n";
     $textWithSpecialChars = "blabla {$percent} {$amp} {$backslash} {$lineBreak} blabla {$percent} {$amp} {$backslash} {$lineBreak} ";
     $textUrlEncoded = $twigExtension->urlEncodeText($textWithSpecialChars);
     $this->assertFalse(strpos($textUrlEncoded, $amp));
     $this->assertFalse(strpos($textUrlEncoded, $backslash));
     $this->assertFalse(strpos($textUrlEncoded, $lineBreak));
     $this->assertStringCount("%0D%0A", $textUrlEncoded, 2);
     $this->assertStringCount("%20", $textUrlEncoded, 10);
     $this->assertStringCount("%26", $textUrlEncoded, 2);
     $this->assertStringCount("%5C", $textUrlEncoded, 2);
     $this->assertStringCount("%25", $textUrlEncoded, 2);
     $this->assertStringCount("%", $textUrlEncoded, 20);
 }
 /**
  * Show a web-version of an email that has been sent to recipients and has been stored in the database.
  * @param string $token
  */
 public function webViewAction($token)
 {
     // find email recipients, template & params
     $sentEmail = $this->getSentEmailForToken($token);
     // check if the sent email is available
     if ($sentEmail != null) {
         // check if the current user is allowed to see the email
         if ($this->userIsAllowedToSeeThisMail($sentEmail)) {
             $template = $sentEmail->getTemplate();
             $emailVars = $sentEmail->getVariables();
             // re-attach all entities to the EntityManager.
             $this->reAttachAllEntities($emailVars);
             // remove the web-view-token from the param-array
             $templateProvider = $this->getTemplateProviderService();
             unset($emailVars[$templateProvider->getWebViewTokenId()]);
             // render & return email
             $response = $this->renderResponse("{$template}.html.twig", $emailVars);
             $campaignParams = $templateProvider->getCampaignParamsFor($template, $emailVars);
             if (sizeof($campaignParams) > 0) {
                 $response->setContent(AzineEmailTwigExtension::addCampaignParamsToAllUrls($response->getContent(), $campaignParams));
             }
             return $response;
             // if the user is not allowed to see this mail
         } else {
             $msg = $this->container->get('translator')->trans('web.pre.view.test.mail.access.denied');
             throw new AccessDeniedException($msg);
         }
     }
     // the parameters-array is null => the email is not available in webView
     $days = $this->container->getParameter("azine_email_web_view_retention");
     $response = $this->renderResponse("AzineEmailBundle:Webview:mail.not.available.html.twig", array('days' => $days));
     $response->setStatusCode(404);
     return $response;
 }
 /**
  * (non-PHPdoc)
  * @see Azine\EmailBundle\Services.TemplateTwigSwiftMailerInterface::sendEmail()
  * @param array $failedRecipients
  * @param string $subject
  * @param String $from
  * @param String $fromName
  * @param array|String $to
  * @param String $toName
  * @param array|String $cc
  * @param String $ccName
  * @param array|String $bcc
  * @param String $bccName
  * @param $replyTo
  * @param $replyToName
  * @param array $params
  * @param $template
  * @param array $attachments
  * @param null $emailLocale
  * @param \Swift_Message $message
  * @return int
  */
 public function sendEmail(&$failedRecipients, $subject, $from, $fromName, $to, $toName, $cc, $ccName, $bcc, $bccName, $replyTo, $replyToName, array $params, $template, $attachments = array(), $emailLocale = null, \Swift_Message &$message = null)
 {
     // create the message
     if ($message == null) {
         $message = \Swift_Message::newInstance();
     }
     $message->setSubject($subject);
     // set the from-Name & -Emali to the default ones if not given
     if ($from == null) {
         $from = $this->noReplyEmail;
     }
     if ($fromName == null) {
         $fromName = $this->noReplyName;
     }
     // add the from-email for the footer-text
     if (!array_key_exists('fromEmail', $params)) {
         $params['sendMailAccountName'] = $this->noReplyName;
         $params['sendMailAccountAddress'] = $this->noReplyEmail;
     }
     // get the baseTemplate. => templateId without the ending.
     $templateBaseId = substr($template, 0, strrpos($template, ".", -6));
     // check if this email should be stored for web-view
     if ($this->templateProvider->saveWebViewFor($templateBaseId)) {
         // keep a copy of the vars for the web-view
         $webViewParams = $params;
         // add the web-view token
         $params[$this->templateProvider->getWebViewTokenId()] = SentEmail::getNewToken();
     } else {
         $webViewParams = array();
     }
     // recursively add all template-variables for the wrapper-templates and contentItems
     $params = $this->templateProvider->addTemplateVariablesFor($templateBaseId, $params);
     // recursively attach all messages in the array
     $this->embedImages($message, $params);
     // change the locale for the email-recipients
     if ($emailLocale !== null && strlen($emailLocale) > 0) {
         $currentUserLocale = $this->translator->getLocale();
         // change the router-context locale
         $this->routerContext->setParameter("_locale", $emailLocale);
         // change the translator locale
         $this->translator->setLocale($emailLocale);
     } else {
         $emailLocale = $this->translator->getLocale();
     }
     // recursively add snippets for the wrapper-templates and contentItems
     $params = $this->templateProvider->addTemplateSnippetsWithImagesFor($templateBaseId, $params, $emailLocale);
     // add the emailLocale (used for web-view)
     $params['emailLocale'] = $emailLocale;
     // render the email parts
     $twigTemplate = $this->loadTemplate($template);
     $textBody = $twigTemplate->renderBlock('body_text', $params);
     $message->addPart($textBody, 'text/plain');
     $htmlBody = $twigTemplate->renderBlock('body_html', $params);
     $campaignParams = $this->templateProvider->getCampaignParamsFor($templateBaseId, $params);
     if (sizeof($campaignParams) > 0) {
         $htmlBody = $this->emailTwigExtension->addCampaignParamsToAllUrls($htmlBody, $campaignParams);
     }
     // if email-tracking is enabled
     if ($this->emailOpenTrackingCodeBuilder) {
         // add an image at the end of the html tag with the tracking-params to track email-opens
         $imgTrackingCode = $this->emailOpenTrackingCodeBuilder->getTrackingImgCode($templateBaseId, $campaignParams, $params, $message->getId(), $to, $cc, $bcc);
         if ($imgTrackingCode && strlen($imgTrackingCode) > 0) {
             $htmlCloseTagPosition = strpos($htmlBody, "</body>");
             $htmlBody = substr_replace($htmlBody, $imgTrackingCode, $htmlCloseTagPosition, 0);
         }
     }
     $message->setBody($htmlBody, 'text/html');
     // remove unused/unreferenced embeded items from the message
     $message = $this->removeUnreferecedEmbededItemsFromMessage($message, $params, $htmlBody);
     // change the locale back to the users locale
     if (isset($currentUserLocale) && $currentUserLocale != null) {
         $this->routerContext->setParameter("_locale", $currentUserLocale);
         $this->translator->setLocale($currentUserLocale);
     }
     // add attachments
     foreach ($attachments as $fileName => $file) {
         // add attachment from existing file
         if (is_string($file)) {
             // check that the file really exists!
             if (file_exists($file)) {
                 $attachment = \Swift_Attachment::fromPath($file);
                 if (strlen($fileName) >= 5) {
                     $attachment->setFilename($fileName);
                 }
             } else {
                 throw new FileException("File not found: " . $file);
             }
             // add attachment from generated data
         } else {
             $attachment = \Swift_Attachment::newInstance($file, $fileName);
         }
         $message->attach($attachment);
     }
     // set the addresses
     if ($from) {
         $message->setFrom($from, $fromName);
     }
     if ($replyTo) {
         $message->setReplyTo($replyTo, $replyToName);
     } elseif ($from) {
         $message->setReplyTo($from, $fromName);
     }
     if ($to) {
         $message->setTo($to, $toName);
     }
     if ($cc) {
         $message->setCc($cc, $ccName);
     }
     if ($bcc) {
         $message->setBcc($bcc, $bccName);
     }
     // add custom headers
     $this->templateProvider->addCustomHeaders($templateBaseId, $message, $params);
     // send the message
     $mailer = $this->getMailer($params);
     $messagesSent = $mailer->send($message, $failedRecipients);
     // if the message was successfully sent,
     // and it should be made available in web-view
     if ($messagesSent && array_key_exists($this->templateProvider->getWebViewTokenId(), $params)) {
         // store the email
         $sentEmail = new SentEmail();
         $sentEmail->setToken($params[$this->templateProvider->getWebViewTokenId()]);
         $sentEmail->setTemplate($templateBaseId);
         $sentEmail->setSent(new \DateTime());
         // recursively add all template-variables for the wrapper-templates and contentItems
         $webViewParams = $this->templateProvider->addTemplateVariablesFor($template, $webViewParams);
         // replace absolute image-paths with relative ones.
         $webViewParams = $this->templateProvider->makeImagePathsWebRelative($webViewParams, $emailLocale);
         // recursively add snippets for the wrapper-templates and contentItems
         $webViewParams = $this->templateProvider->addTemplateSnippetsWithImagesFor($template, $webViewParams, $emailLocale, true);
         $sentEmail->setVariables($webViewParams);
         // save only successfull recipients
         if (!is_array($to)) {
             $to = array($to);
         }
         $successfulRecipients = array_diff($to, $failedRecipients);
         $sentEmail->setRecipients($successfulRecipients);
         // write to db
         $em = $this->managerRegistry->getManager();
         $em->persist($sentEmail);
         $em->flush($sentEmail);
         $em->clear();
         gc_collect_cycles();
     }
     return $messagesSent;
 }