public function testValidateNoErrors() { $this->context->expects($this->never())->method('addViolation'); $email = new Email(); $email->setTo(['*****@*****.**'])->setCc(['*****@*****.**'])->setBcc(['*****@*****.**']); $this->getValidator()->validate($email, $this->constraint); }
/** * {@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()); } }
/** * {@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); }
public function testFormShouldBeSubmittendAndViewShouldContainsRouteParameters() { $email = new Email(); $email->setEntityClass('entityClass_param'); $email->setEntityId('entityId_param'); $form = $this->factory->createBuilder('form', $email)->add('to', EmailAddressRecipientsType::NAME)->getForm(); $form->submit([]); $expectedRouteParameters = ['entityClass' => 'entityClass_param', 'entityId' => 'entityId_param']; $view = $form->createView(); $configs = $view->children['to']->vars['configs']; $this->assertTrue($form->isValid()); $this->assertTrue($form->isSynchronized()); $this->assertArrayHasKey('route_parameters', $configs); $this->assertEquals($configs['route_parameters'], $expectedRouteParameters); }
/** * @param mixed $emailOwner * @param OrganizationInterface $organization * @param string $originName * @param bool $enableUseUserEmailOrigin * * @return mixed|null|object|InternalEmailOrigin|UserEmailOrigin */ public function findEmailOrigin($emailOwner, $organization, $originName, $enableUseUserEmailOrigin) { if ($emailOwner instanceof User) { $origin = $this->getPreferredOrigin($emailOwner, $organization, $enableUseUserEmailOrigin); } elseif ($emailOwner instanceof Mailbox) { $origin = $emailOwner->getOrigin(); } else { $origin = $this->getEntityManager()->getRepository('OroEmailBundle:InternalEmailOrigin')->findOneBy(['internalName' => $originName]); } if ($this->isEmptyOrigin($origin)) { $user = $this->emailModel->getCampaignOwner(); $origin = $this->getPreferredOrigin($user, $organization, $enableUseUserEmailOrigin); } return $origin; }
/** * @param string $method * * @dataProvider methodsData */ public function testProcessException($method) { $this->request->setMethod($method); $this->model->setFrom('*****@*****.**')->setTo(['*****@*****.**'])->setSubject('testSubject')->setBody('testBody'); $this->form->expects($this->once())->method('setData')->with($this->model); $this->form->expects($this->once())->method('submit')->with($this->request); $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true)); $exception = new \Exception('TEST'); $this->emailProcessor->expects($this->once())->method('process')->with($this->model)->will($this->returnCallback(function () use($exception) { throw $exception; })); $this->logger->expects($this->once())->method('error')->with('Email sending failed.', ['exception' => $exception]); $this->form->expects($this->once())->method('addError')->with($this->isInstanceOf('Symfony\\Component\\Form\\FormError')); $this->assertFalse($this->handler->process($this->model)); }
/** * @dataProvider supportedMethods */ public function testProcessValidData($method) { $this->request->setMethod($method); $this->model->setFrom('*****@*****.**')->setTo(array('*****@*****.**'))->setSubject('testSubject')->setBody('testBody'); $this->form->expects($this->once())->method('setData')->with($this->model); $this->form->expects($this->once())->method('submit')->with($this->request); $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true)); $message = new \Swift_Message(); $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message)); $this->mailer->expects($this->once())->method('send')->will($this->returnValue(1)); $origin = new InternalEmailOrigin(); $folder = new EmailFolder(); $folder->setType(EmailFolder::SENT); $origin->addFolder($folder); $emailOriginRepo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock(); $emailOriginRepo->expects($this->once())->method('findOneBy')->with(array('name' => InternalEmailOrigin::BAP))->will($this->returnValue($origin)); $this->em->expects($this->once())->method('getRepository')->with('OroEmailBundle:InternalEmailOrigin')->will($this->returnValue($emailOriginRepo)); $this->emailEntityBuilder->expects($this->once())->method('setOrigin')->with($this->identicalTo($origin)); $email = new EmailEntity(); $this->emailEntityBuilder->expects($this->once())->method('email')->with('testSubject', '*****@*****.**', array('*****@*****.**'))->will($this->returnValue($email)); $body = new EmailBody(); $this->emailEntityBuilder->expects($this->once())->method('body')->with('testBody', false, true)->will($this->returnValue($body)); $batch = $this->getMock('Oro\\Bundle\\EmailBundle\\Builder\\EmailEntityBatchInterface'); $this->emailEntityBuilder->expects($this->once())->method('getBatch')->will($this->returnValue($batch)); $batch->expects($this->once())->method('persist')->with($this->identicalTo($this->em)); $this->em->expects($this->once())->method('flush'); $this->assertTrue($this->handler->process($this->model)); $this->assertNotNull($message); $this->assertEquals(array('*****@*****.**' => null), $message->getFrom()); $this->assertEquals(array('*****@*****.**' => null), $message->getTo()); $this->assertEquals('testSubject', $message->getSubject()); $this->assertEquals('testBody', $message->getBody()); $this->assertTrue($folder === $email->getFolder()); $this->assertTrue($body === $email->getEmailBody()); }
/** * {@inheritdoc} */ protected function executeAction($context) { $emailModel = new Email(); $emailModel->setFrom($this->getEmailAddress($context, $this->options['from'])); $to = array(); foreach ($this->options['to'] as $email) { if ($email) { $to[] = $this->getEmailAddress($context, $email); } } $emailModel->setTo($to); $emailModel->setSubject($this->contextAccessor->getValue($context, $this->options['subject'])); $emailModel->setBody($this->contextAccessor->getValue($context, $this->options['body'])); $email = $this->emailProcessor->process($emailModel); if (array_key_exists('attribute', $this->options)) { $this->contextAccessor->setValue($context, $this->options['attribute'], $email); } }
public function testHasEntity() { $obj = new Email(); $this->assertFalse($obj->hasEntity()); $obj->setEntityClass('Test\\Entity'); $this->assertFalse($obj->hasEntity()); $obj->setEntityId(123); $this->assertTrue($obj->hasEntity()); $obj->setEntityClass(null); $this->assertFalse($obj->hasEntity()); }
/** * @param int $id * @param string $entity * @param array $from * @param array $to * @param string $subject * @param string $body * * @dataProvider sendDataProvider */ public function testSend($id, $entity, array $from, array $to, $subject, $body, $expects) { $emails = array_keys($from); $this->helper->expects($this->once())->method('getSingleEntityIdentifier')->will($this->returnValue($id)); $this->emailHelper->expects($this->once())->method('buildFullEmailAddress')->will($this->returnValue(sprintf('%s <%s>', reset($emails), reset($from)))); $marketingList = new MarketingList(); $marketingList->setEntity($entity); $template = new EmailTemplate(); $template->setType('html'); $settings = new InternalTransportSettings(); $settings->setTemplate($template); $campaign = new EmailCampaign(); $campaign->setMarketingList($marketingList)->setTransportSettings($settings); $this->renderer->expects($this->once())->method('compileMessage')->will($this->returnValue([$subject, $body])); $emailModel = new Email(); $emailModel->setFrom(sprintf('%s <%s>', reset($emails), reset($from)))->setType($template->getType())->setEntityClass($entity)->setEntityId($id)->setTo($to)->setSubject($subject)->setBody($body); $this->processor->expects($expects)->method('process')->with($this->equalTo($emailModel)); $this->transport->send($campaign, $entity, $from, $to); }
/** * {@inheritdoc} */ public function process(Email $model) { $issue_contexts = array_filter($model->getContexts(), function ($context) { return $context instanceof Issue; }); /* @var $issue Issue */ $issue = array_pop($issue_contexts); if ($this->request->getMethod() === 'GET') { $tos = $model->getTo(); $tos[] = $issue->getReporter()->getEmail(); $model->setTo($tos); $model->setSubject(sprintf('%s %s', $issue->getCode(), $issue->getSummary())); } parent::process($model); }
/** * {@inheritdoc} */ protected function executeAction($context) { $type = 'txt'; $emailModel = new Email(); $emailModel->setFrom($this->getEmailAddress($context, $this->options['from'])); $to = []; foreach ($this->options['to'] as $email) { if ($email) { $to[] = $this->getEmailAddress($context, $email); } } $emailModel->setTo($to); $emailModel->setSubject($this->contextAccessor->getValue($context, $this->options['subject'])); $emailModel->setBody($this->contextAccessor->getValue($context, $this->options['body'])); if (array_key_exists('type', $this->options) && in_array($this->options['type'], ['txt', 'html'], true)) { $type = $this->options['type']; } $emailModel->setType($type); $emailUser = $this->emailProcessor->process($emailModel); if (array_key_exists('attribute', $this->options)) { $this->contextAccessor->setValue($context, $this->options['attribute'], $emailUser->getEmail()); } }
/** * @param EmailModel $emailModel */ protected function initAvailableAttachments(EmailModel $emailModel) { $attachments = []; if ($emailModel->getParentEmailId()) { $parentEmail = $this->entityManager->getRepository('OroEmailBundle:Email')->find($emailModel->getParentEmailId()); $threadAttachments = $this->emailAttachmentProvider->getThreadAttachments($parentEmail); $threadAttachments = $this->filterAttachmentsByName($threadAttachments); $attachments = array_merge($attachments, $threadAttachments); } if ($emailModel->getEntityClass() && $emailModel->getEntityId()) { $scopeEntity = $this->entityManager->getRepository($emailModel->getEntityClass())->find($emailModel->getEntityId()); if ($scopeEntity) { $scopeEntityAttachments = $this->emailAttachmentProvider->getScopeEntityAttachments($scopeEntity); $scopeEntityAttachments = $this->filterAttachmentsByName($scopeEntityAttachments); $attachments = array_merge($attachments, $scopeEntityAttachments); } } $emailModel->setAttachmentsAvailable($attachments); }
/** * @param Email $value * @param Constraint|EmailRecipients $constraint */ public function validate($value, Constraint $constraint) { if (!$value->getTo() && !$value->getCc() && !$value->getBcc()) { $this->context->addViolation($constraint->message); } }
/** * @param EmailModel $model * @throws \InvalidArgumentException */ protected function assertModel(EmailModel $model) { if (!$model->getFrom()) { throw new \InvalidArgumentException('Sender can not be empty'); } if (!$model->getTo()) { throw new \InvalidArgumentException('Recipient can not be empty'); } }
/** * Populate a model with initial data. * This method is used to load an initial data from a query string * * @param Email $model */ protected function initModel(Email $model) { if ($this->request->query->has('gridName')) { $model->setGridName($this->request->query->get('gridName')); } if ($this->request->query->has('entityClass')) { $model->setEntityClass($this->entityRoutingHelper->decodeClassName($this->request->query->get('entityClass'))); } if ($this->request->query->has('entityId')) { $model->setEntityId($this->request->query->get('entityId')); } if ($this->request->query->has('from')) { $from = $this->request->query->get('from'); if (!empty($from)) { $this->preciseFullEmailAddress($from); } $model->setFrom($from); } else { $user = $this->getUser(); if ($user) { $model->setFrom($this->emailAddressHelper->buildFullEmailAddress($user->getEmail(), $this->nameFormatter->format($user))); } } if ($this->request->query->has('to')) { $to = trim($this->request->query->get('to')); if (!empty($to)) { $this->preciseFullEmailAddress($to, $model->getEntityClass(), $model->getEntityId()); } $model->setTo(array($to)); } if ($this->request->query->has('subject')) { $subject = trim($this->request->query->get('subject')); $model->setSubject($subject); } }
/** * Populate a model with initial data. * This method is used to load an initial data from a query string * * @param Email $model */ protected function initModel(Email $model) { if ($this->request->query->has('gridName')) { $model->setGridName($this->request->query->get('gridName')); } if ($this->request->query->has('from')) { $from = $this->request->query->get('from'); if (!empty($from)) { $this->preciseFullEmailAddress($from); } $model->setFrom($from); } else { $user = $this->getUser(); if ($user) { $model->setFrom(EmailUtil::buildFullEmailAddress($user->getEmail(), $this->nameFormatter->format($user))); } } if ($this->request->query->has('to')) { $to = trim($this->request->query->get('to')); if (!empty($to)) { $this->preciseFullEmailAddress($to); } $model->setTo(array($to)); } if ($this->request->query->has('subject')) { $subject = trim($this->request->query->get('subject')); $model->setSubject($subject); } }
/** * @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); $emailModel->setSubject($subject === false ? $template->getSubject() : $subject)->setBody($content === false ? $template->getContent() : $content)->setType($template->getType()); }
/** * @param EmailModel $model * * @return string */ protected function getParentMessageId(EmailModel $model) { $messageId = ''; $parentEmailId = $model->getParentEmailId(); if ($parentEmailId && $model->getMailType() == EmailModel::MAIL_TYPE_REPLY) { $parentEmail = $this->getEntityManager()->getRepository('OroEmailBundle:Email')->find($parentEmailId); $messageId = $parentEmail->getMessageId(); } return $messageId; }