/** * Applies the given notifications to the given object * * @param mixed $object * @param EmailNotificationInterface[] $notifications * @param LoggerInterface $logger Override for default logger. If this parameter is specified * this logger will be used instead of a logger specified * in the constructor */ public function process($object, $notifications, LoggerInterface $logger = null) { if (!$logger) { $logger = $this->logger; } foreach ($notifications as $notification) { $emailTemplate = $notification->getTemplate(); try { list($subjectRendered, $templateRendered) = $this->renderer->compileMessage($emailTemplate, ['entity' => $object]); } catch (\Twig_Error $e) { $identity = method_exists($emailTemplate, '__toString') ? (string) $emailTemplate : $emailTemplate->getSubject(); $logger->error(sprintf('Rendering of email template "%s" failed. %s', $identity, $e->getMessage()), ['exception' => $e]); continue; } $senderEmail = $this->cm->get('oro_notification.email_notification_sender_email'); $senderName = $this->cm->get('oro_notification.email_notification_sender_name'); if ($notification instanceof SenderAwareEmailNotificationInterface && $notification->getSenderEmail()) { $senderEmail = $notification->getSenderEmail(); $senderName = $notification->getSenderName(); } if ($emailTemplate->getType() == 'txt') { $type = 'text/plain'; } else { $type = 'text/html'; } foreach ((array) $notification->getRecipientEmails() as $email) { $message = \Swift_Message::newInstance()->setSubject($subjectRendered)->setFrom($senderEmail, $senderName)->setTo($email)->setBody($templateRendered, $type); $this->mailer->send($message); } $this->addJob(self::SEND_COMMAND); } }
/** * Applies the given notifications to the given object * * @param mixed $object * @param EmailNotificationInterface[] $notifications * @param LoggerInterface $logger Override for default logger. If this parameter is specified * this logger will be used instead of a logger specified * in the constructor */ public function process($object, $notifications, LoggerInterface $logger = null) { if (!$logger) { $logger = $this->logger; } foreach ($notifications as $notification) { /** @var EmailTemplate $emailTemplate */ $emailTemplate = $notification->getTemplate(); try { list($subjectRendered, $templateRendered) = $this->renderer->compileMessage($emailTemplate, array('entity' => $object)); } catch (\Twig_Error $e) { $logger->error(sprintf('Rendering of email template "%s"%s failed. %s', $emailTemplate->getSubject(), method_exists($emailTemplate, 'getId') ? sprintf(' (id: %d)', $emailTemplate->getId()) : '', $e->getMessage()), array('exception' => $e)); continue; } $senderEmail = $this->cm->get('oro_notification.email_notification_sender_email'); $senderName = $this->cm->get('oro_notification.email_notification_sender_name'); $type = $emailTemplate->getType() == 'txt' ? 'text/plain' : 'text/html'; $recipients = $notification->getRecipientEmails(); foreach ((array) $recipients as $email) { $message = \Swift_Message::newInstance()->setSubject($subjectRendered)->setFrom($senderEmail, $senderName)->setTo($email)->setBody($templateRendered, $type); $this->mailer->send($message); } $this->addJob(self::SEND_COMMAND); } }
/** * {@inheritdoc} */ public function send(EmailCampaign $campaign, $entity, array $from, array $to) { $entityId = $this->doctrineHelper->getSingleEntityIdentifier($entity); $marketingList = $campaign->getMarketingList(); /** @var EmailTemplate $template */ $template = $campaign->getTransportSettings()->getSettingsBag()->get('template'); list($subjectRendered, $templateRendered) = $this->emailRenderer->compileMessage($template, ['entity' => $entity]); $emailModel = new Email(); $emailModel->setType($template->getType())->setFrom($this->buildFullEmailAddress($from))->setEntityClass($marketingList->getEntity())->setEntityId($entityId)->setTo($to)->setSubject($subjectRendered)->setBody($templateRendered); $this->processor->process($emailModel); }
/** * {@inheritdoc} */ protected function executeAction($context) { $emailModel = new Email(); $from = $this->getEmailAddress($context, $this->options['from']); $this->validateAddress($from); $emailModel->setFrom($from); $to = []; foreach ($this->options['to'] as $email) { if ($email) { $address = $this->getEmailAddress($context, $email); $this->validateAddress($address); $to[] = $this->getEmailAddress($context, $address); } } $emailModel->setTo($to); $entity = $this->contextAccessor->getValue($context, $this->options['entity']); $template = $this->contextAccessor->getValue($context, $this->options['template']); $emailTemplate = $this->objectManager->getRepository('OroEmailBundle:EmailTemplate')->findByName($template); if (!$emailTemplate) { $errorMessage = sprintf('Template "%s" not found.', $template); $this->logger->error('Workflow send email action.' . $errorMessage); throw new EntityNotFoundException($errorMessage); } $templateData = $this->renderer->compileMessage($emailTemplate, ['entity' => $entity]); $type = $emailTemplate->getType() == 'txt' ? 'text/plain' : 'text/html'; list($subjectRendered, $templateRendered) = $templateData; $emailModel->setSubject($subjectRendered); $emailModel->setBody($templateRendered); $emailModel->setType($type); $emailUser = $this->emailProcessor->process($emailModel); if (array_key_exists('attribute', $this->options)) { $this->contextAccessor->setValue($context, $this->options['attribute'], $emailUser->getEmail()); } }
/** * @dataProvider executeOptionsDataProvider * @param array $options * @param array $expected */ public function testExecute($options, $expected) { $context = []; $this->contextAccessor->expects($this->any())->method('getValue')->will($this->returnArgument(1)); $this->entityNameResolver->expects($this->any())->method('getName')->will($this->returnCallback(function () { return '_Formatted'; })); $this->objectRepository->expects($this->once())->method('findByName')->with($options['template'])->willReturn($this->emailTemplate); $this->emailTemplate->expects($this->once())->method('getType')->willReturn('txt'); $this->renderer->expects($this->once())->method('compileMessage')->willReturn([$expected['subject'], $expected['body']]); $self = $this; $emailUserEntity = $this->getMockBuilder('\\Oro\\Bundle\\EmailBundle\\Entity\\EmailUser')->disableOriginalConstructor()->setMethods(['getEmail'])->getMock(); $emailEntity = $this->getMock('\\Oro\\Bundle\\EmailBundle\\Entity\\Email'); $emailUserEntity->expects($this->any())->method('getEmail')->willReturn($emailEntity); $this->emailProcessor->expects($this->once())->method('process')->with($this->isInstanceOf('Oro\\Bundle\\EmailBundle\\Form\\Model\\Email'))->will($this->returnCallback(function (Email $model) use($emailUserEntity, $expected, $self) { $self->assertEquals($expected['body'], $model->getBody()); $self->assertEquals($expected['subject'], $model->getSubject()); $self->assertEquals($expected['from'], $model->getFrom()); $self->assertEquals($expected['to'], $model->getTo()); return $emailUserEntity; })); if (array_key_exists('attribute', $options)) { $this->contextAccessor->expects($this->once())->method('setValue')->with($context, $options['attribute'], $emailEntity); } $this->action->initialize($options); $this->action->execute($context); }
/** * @param EmailModel $emailModel * @param EmailTemplate $template * @param Email $email */ protected function applyTemplate(EmailModel $emailModel, EmailTemplate $template, Email $email) { $locales = array_merge($email->getAcceptedLocales(), [$this->defaultLocale]); $flippedLocales = array_flip($locales); $translatedSubjects = []; $translatedContents = []; foreach ($template->getTranslations() as $translation) { switch ($translation->getField()) { case 'content': $translatedContents[$translation->getLocale()] = $translation->getContent(); break; case 'subject': $translatedSubjects[$translation->getLocale()] = $translation->getContent(); break; } } $comparator = ArrayUtil::createOrderedComparator($flippedLocales); uksort($translatedSubjects, $comparator); uksort($translatedContents, $comparator); $validContents = array_intersect_key($translatedContents, $flippedLocales); $validsubjects = array_intersect_key($translatedSubjects, $flippedLocales); $content = reset($validContents); $subject = reset($validsubjects); $content = $content === false ? $template->getContent() : $content; $subject = $subject === false ? $template->getSubject() : $subject; $emailModel->setSubject($this->emailRender->renderWithDefaultFilters($subject, ['entity' => $email]))->setBody($this->emailRender->renderWithDefaultFilters($content, ['entity' => $email]))->setType($template->getType()); }
/** * @param string $templateName * @param array $templateParams * @param \Swift_Message $expectedMessage * @param string $emailType */ protected function assertSendCalled($templateName, array $templateParams, \Swift_Message $expectedMessage, $emailType = 'txt') { $this->emailTemplate->expects($this->once())->method('getType')->willReturn($emailType); $this->objectRepository->expects($this->once())->method('findOneBy')->with(['name' => $templateName])->willReturn($this->emailTemplate); $this->renderer->expects($this->once())->method('compileMessage')->with($this->emailTemplate, $templateParams)->willReturn([$expectedMessage->getSubject(), $expectedMessage->getBody()]); $to = $expectedMessage->getTo(); $toKeys = array_keys($to); $this->emailHolderHelper->expects($this->once())->method('getEmail')->with($this->isInstanceOf('Oro\\Bundle\\UserBundle\\Entity\\UserInterface'))->willReturn(array_shift($toKeys)); $this->mailer->expects($this->once())->method('send')->with($this->callback(function (\Swift_Message $actualMessage) use($expectedMessage) { $this->assertEquals($expectedMessage->getSubject(), $actualMessage->getSubject()); $this->assertEquals($expectedMessage->getFrom(), $actualMessage->getFrom()); $this->assertEquals($expectedMessage->getTo(), $actualMessage->getTo()); $this->assertEquals($expectedMessage->getBody(), $actualMessage->getBody()); $this->assertEquals($expectedMessage->getContentType(), $actualMessage->getContentType()); return true; })); }
/** * Applies the given notifications to the given object * * @param mixed $object * @param EmailNotificationInterface[] $notifications * @param LoggerInterface $logger Override for default logger. If this parameter is specified * this logger will be used instead of a logger specified * in the constructor */ public function process($object, $notifications, LoggerInterface $logger = null) { if (!$logger) { $logger = $this->logger; } foreach ($notifications as $notification) { $emailTemplate = $notification->getTemplate(); try { list($subjectRendered, $templateRendered) = $this->renderer->compileMessage($emailTemplate, array('entity' => $object)); } catch (\Twig_Error $ex) { $logger->error(sprintf('Rendering of email template "%s"%s failed. %s', $emailTemplate->getSubject(), method_exists($emailTemplate, 'getId') ? sprintf(' (id: %d)', $emailTemplate->getId()) : '', $ex->getMessage()), array('exception' => $ex)); continue; } // TODO: use locale for subject and body $params = new ParameterBag(array('subject' => $subjectRendered, 'body' => $templateRendered, 'from' => $this->sendFrom, 'to' => $notification->getRecipientEmails(), 'type' => $emailTemplate->getType() == 'txt' ? 'text/plain' : 'text/html')); $this->notify($params); $this->addJob(self::SEND_COMMAND); } }
/** * @param FormEvent $event */ public function fillFormByTemplate(FormEvent $event) { /** @var Email|null $data */ $data = $event->getData(); if (null === $data || !is_object($data) || null === $data->getTemplate()) { return; } if (null !== $data->getSubject() && null !== $data->getBody()) { return; } $emailTemplate = $data->getTemplate(); $targetEntity = $this->emailModelBuilderHelper->getTargetEntity($data->getEntityClass(), $data->getEntityId()); list($emailSubject, $emailBody) = $this->emailRenderer->compileMessage($emailTemplate, ['entity' => $targetEntity]); if (null === $data->getSubject()) { $data->setSubject($emailSubject); } if (null === $data->getBody()) { $data->setBody($emailBody); } }
/** * @param Email $inputData * @param array $expectedData * * @dataProvider fillFormByTemplateProvider */ public function testFillFormByTemplate(Email $inputData = null, array $expectedData = []) { $this->markTestSkipped('Test Skipped because of unresolved relation to \\Oro\\Component\\Testing\\Unit\\Form\\Type\\Stub\\EntityType'); $emailTemplate = $this->createEmailTemplate(); $this->emailRenderer->expects($this->any())->method('compileMessage')->with($emailTemplate)->willReturn([$emailTemplate->getSubject(), $emailTemplate->getContent()]); $formType = $this->createEmailType(); $form = $this->factory->create($formType, $inputData); $formType->fillFormByTemplate(new FormEvent($form, $inputData)); $formData = $form->getData(); $propertyAccess = PropertyAccess::createPropertyAccessor(); foreach ($expectedData as $propertyPath => $expectedValue) { $value = $propertyAccess->getValue($formData, $propertyPath); $this->assertEquals($expectedValue, $value); } }
/** * @param UserInterface $user * @param string $emailTemplateName * @param array $emailTemplateParams * * @return int */ public function getEmailTemplateAndSendEmail(UserInterface $user, $emailTemplateName, array $emailTemplateParams = []) { $emailTemplate = $this->findEmailTemplateByName($emailTemplateName); return $this->sendEmail($user, $this->renderer->compileMessage($emailTemplate, $emailTemplateParams), $this->getEmailTemplateType($emailTemplate)); }