Пример #1
0
 public function sendEmail(Notification $notification)
 {
     $fromCircles = $this->dataService->getDataCircles($notification->getRevisionId());
     $toCircles = array_unique(array_merge($fromCircles, $notification->getTemplateId()->getCirclesTo()));
     $fromUser = $this->usersToEmailAddresses([$this->userService->getUser($notification->getUsername())]);
     $toUsers = $this->usersToEmailAddresses($this->userService->getUsersForRoleAndCircles($notification->getTemplateId()->getRoleTo(), $toCircles));
     $ccUsers = $this->usersToEmailAddresses($this->userService->getUsersForRoleAndCircles($notification->getTemplateId()->getRoleCc(), $toCircles));
     $message = \Swift_Message::newInstance();
     $params = ['notification' => $notification, 'source' => $notification->getRevisionId()->getRawData(), 'object' => $notification->getRevisionId()->buildObject(), 'status' => $notification->getStatus(), 'environment' => $notification->getEnvironmentId()];
     if ($notification->getStatus() == 'pending') {
         //it's a notification
         try {
             $body = $this->twig->createTemplate($notification->getTemplateId()->getBody())->render($params);
         } catch (\Exception $e) {
             $body = "Error in body template: " . $e->getMessage();
         }
         $message->setSubject($notification->getTemplateId() . ' for ' . $notification->getRevisionId())->setFrom($this->sender['address'], $this->sender['sender_name'])->setTo($toUsers)->setCc(array_unique(array_merge($ccUsers, $fromUser)))->setBody($body, empty($notification->getTemplateId()->getEmailContentType()) ? 'text/html' : $notification->getTemplateId()->getEmailContentType());
         $notification->setEmailed(new \DateTime());
     } else {
         //it's a notification
         try {
             $body = $this->twig->createTemplate($notification->getTemplateId()->getResponseTemplate())->render($params);
         } catch (\Exception $e) {
             $body = "Error in response template: " . $e->getMessage();
         }
         //it's a reminder
         $message->setSubject($notification->getTemplateId() . ' for ' . $notification->getRevisionId() . ' has been ' . $notification->getStatus())->setFrom($this->sender['address'], $this->sender['sender_name'])->setTo($fromUser)->setCc(array_unique(array_merge($ccUsers, $toUsers)))->setBody($body, 'text/html');
         $notification->setResponseEmailed(new \DateTime());
     }
     if (!$this->dryRun) {
         $em = $this->doctrine->getManager();
         try {
             /**@Swift_Mailer $mailer*/
             $mailer = $this->container->get('mailer');
             $mailer->send($message);
             $em->persist($notification);
             $em->flush();
         } catch (\Swift_TransportException $e) {
         }
     }
 }