/**
  * 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);
     }
 }
Exemplo n.º 3
0
 /**
  * {@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);
 }
Exemplo n.º 4
0
 /**
  * {@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());
     }
 }
 /**
  * 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);
     }
 }
Exemplo n.º 6
0
 /**
  * @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);
     }
 }
Exemplo n.º 7
0
 /**
  * @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));
 }