Exemplo n.º 1
0
 /**
  * @Given /^the sender name is "([^"]*)"$/
  */
 public function theSenderNameIs($name)
 {
     $this->newsletter->setSenderNameTemplate($name);
 }
 /**
  * @param Newsletter $newsletter
  */
 public function sendNewsletter(Newsletter $newsletter, $languageKey = NULL)
 {
     $client = new Client();
     try {
         $bodyTemplate = $this->convertTemplatesToGoTemplates($newsletter->getHtmlContent());
         $bodyTemplate = str_replace('href="SHOWINBROWSER"', '{{ .showInBrowser }}', $bodyTemplate);
         $bodyTemplate = str_replace('href="UNSUBSCRIBE"', '{{ .unsubscribeUri }}', $bodyTemplate);
         $placeholdersInBodyTemplate = $this->foundPlaceholdersInLastProcessedTemplate;
         $senderEmailTemplate = $this->convertTemplatesToGoTemplates($newsletter->getSenderEmailTemplate() ?: '*****@*****.**');
         $replyToEmailTemplate = $senderEmailTemplate;
         if ($newsletter->getReplyToEmailTemplate()) {
             $replyToEmailTemplate = $this->convertTemplatesToGoTemplates($newsletter->getReplyToEmailTemplate());
         }
         $request = array('RecipientsList' => $newsletter->getReceiverGroup()->getCacheFileName($languageKey), 'Templates' => array('SubjectTemplate' => $this->convertTemplatesToGoTemplates($newsletter->getSubject()), 'BodyTemplate' => $bodyTemplate, 'ReceiverEmailTemplate' => $this->convertTemplatesToGoTemplates($newsletter->getReceiverEmailTemplate() ?: '{email}'), 'ReceiverNameTemplate' => $this->convertTemplatesToGoTemplates($newsletter->getReceiverNameTemplate() ?: '{firstName} {lastName}'), 'SenderEmailTemplate' => $senderEmailTemplate, 'SenderNameTemplate' => $this->convertTemplatesToGoTemplates($newsletter->getSenderNameTemplate() ?: 'Newsletter'), 'ReplyToEmailTemplate' => $replyToEmailTemplate, 'LinkTemplates' => array('unsubscribeUri' => array('EncryptionKey' => $this->hmacUrlSecret, 'BaseLink' => $newsletter->getUnsubscribeLink(), 'Parameters' => ['email']), 'showInBrowser' => array('EncryptionKey' => $this->hmacUrlSecret, 'BaseLink' => $newsletter->getNewsletterLink(), 'Parameters' => $placeholdersInBodyTemplate))));
         $blacklistFileName = $newsletter->getReceiverGroup()->getUnsubscribeList()->getUnsubscribeFileName();
         if (file_exists($blacklistFileName)) {
             $request['Blacklist'] = $blacklistFileName;
         }
         $response = $client->post(rtrim($this->sendingApiUri, '/') . '/' . $this->authToken . '/newsletter/' . $newsletter->getIdentifier() . '/send', array('body' => json_encode($request)));
     } catch (RequestException $e) {
         $errorMessage = 'There was an error communicating with the newsletter API -- HTTP Status Code: ' . $e->getCode();
         if ($e->hasResponse()) {
             $errorMessage .= "\nResponse Body:\n" . $e->getResponse()->getBody()->__toString();
         }
         throw new \Exception($errorMessage, 1431428827, $e);
     }
 }
 private function sendNewsletter(NodeInterface $node, $previewEmail = NULL, $languageKey = NULL, array $languageConfiguration = NULL)
 {
     // The Receiver Group association is specified in each individual node dimension,
     // but as the user submitted it in a certain Node Dimension, we're using *exactly* this
     // $receiverGroup which the user has submitted.
     /* @var $receiverGroup \Sandstorm\Newsletter\Domain\Model\ReceiverGroup */
     $receiverGroup = $this->receiverGroupRepository->findByIdentifier($node->getProperty('receiverGroup'));
     if ($receiverGroup == NULL) {
         // TODO: log!
         return;
     }
     $context = ['workspaceName' => 'live'];
     if ($languageKey) {
         $context['dimensions'] = array('language' => $languageConfiguration['values']);
         $context['targetDimensions'] = array('language' => reset($languageConfiguration['values']));
     }
     /* @var $nodeInCorrectDimension NodeInterface */
     $nodeInCorrectDimension = (new FlowQuery(array($node)))->context($context)->get(0);
     if ($nodeInCorrectDimension == NULL) {
         // Skip un-existing nodes
         return;
     }
     $this->newsletterRenderingView->assign('value', $nodeInCorrectDimension);
     $html = $this->newsletterRenderingView->render();
     $html = $this->styleInliningService->inlineStyles($html);
     $newsletter = new Newsletter();
     $newsletterIdentifier = $node->getIdentifier();
     if ($languageKey) {
         $newsletterIdentifier .= '_' . $languageKey;
     }
     if ($previewEmail) {
         $newsletterIdentifier .= uniqid('__', true);
     }
     $newsletter->setIdentifier($newsletterIdentifier);
     $newsletter->setHtmlContent($html);
     $newsletter->setSubject($nodeInCorrectDimension->getProperty('subjectTemplate'));
     if ($previewEmail !== NULL) {
         $newsletter->setReceiverEmailTemplate($previewEmail);
         $newsletter->setReceiverGroup($receiverGroup->singlePersonReceiverGroup());
     } else {
         $newsletter->setReceiverEmailTemplate($nodeInCorrectDimension->getProperty('receiverEmailTemplate'));
         $newsletter->setReceiverGroup($receiverGroup);
     }
     $newsletter->setReceiverNameTemplate($nodeInCorrectDimension->getProperty('receiverNameTemplate'));
     $newsletter->setSenderEmailTemplate($nodeInCorrectDimension->getProperty('senderEmailTemplate'));
     $newsletter->setSenderNameTemplate($nodeInCorrectDimension->getProperty('senderNameTemplate'));
     $newsletter->setReplyToEmailTemplate($nodeInCorrectDimension->getProperty('replyToEmailTemplate'));
     $newsletter->setNewsletterLink($this->linkingService->createNodeUri($this->getControllerContext(), $nodeInCorrectDimension, NULL, NULL, TRUE));
     $unsubscribeListIdentifier = null;
     if ($receiverGroup->getUnsubscribeList()) {
         $unsubscribeListIdentifier = $this->persistenceManager->getIdentifierByObject($receiverGroup->getUnsubscribeList());
     }
     $newsletter->setUnsubscribeLink($this->uriBuilder->reset()->setCreateAbsoluteUri(TRUE)->uriFor('unsubscribe', array('unsubscribeList' => $unsubscribeListIdentifier), 'Unsubscribe', 'Sandstorm.Newsletter'));
     $this->newsletterSendingService->sendNewsletter($newsletter, $languageKey);
 }