コード例 #1
0
ファイル: ActivityListener.php プロジェクト: bix0r/Stjornvisi
 private function send($recipients, $subject, $body)
 {
     $channel = false;
     $connection = false;
     try {
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         foreach ($recipients as $recipient) {
             $message = new Mail();
             $message->name = $recipient->name;
             $message->email = $recipient->address;
             $message->subject = $subject;
             $message->body = $body;
             $msg = new AMQPMessage($message->serialize(), ['delivery_mode' => 2]);
             $channel->basic_publish($msg, '', 'mail_queue');
         }
     } catch (\Exception $e) {
         while ($e) {
             $this->logger->critical(get_class($this) . ":send says: {$e->getMessage()}", $e->getTrace());
             $e = $e->getPrevious();
         }
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
     }
 }
コード例 #2
0
ファイル: Attend.php プロジェクト: bix0r/Stjornvisi
 /**
  * @return $this
  * @throws NotifyException
  */
 public function send()
 {
     //DATA-OBJECTS
     //  get data-objects from persistence layer.
     $eventObject = $this->getEvent($this->params->event_id);
     $userObject = $this->getUser($this->params->recipients);
     //VIEW
     //	create and configure view
     $child = new ViewModel(array('user' => $userObject, 'event' => $eventObject));
     $child->setTemplate($this->params->type ? 'attend' : 'unattend');
     $layout = new ViewModel();
     $layout->setTemplate('layout');
     $layout->addChild($child, 'content');
     $phpRenderer = new PhpRenderer();
     $phpRenderer->setCanRenderTrees(true);
     $resolver = new Resolver\TemplateMapResolver();
     $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'attend' => __DIR__ . '/../../../view/email/attending.phtml', 'unattend' => __DIR__ . '/../../../view/email/un-attending.phtml'));
     $phpRenderer->setResolver($resolver);
     foreach ($layout as $child) {
         $child->setOption('has_parent', true);
         $result = $phpRenderer->render($child);
         $child->setOption('has_parent', null);
         $capture = $child->captureTo();
         if (!empty($capture)) {
             $layout->setVariable($capture, $result);
         }
     }
     //MESSAGE
     //  create and configure message.
     $message = new Mail();
     $message->body = $phpRenderer->render($layout);
     $message->email = $userObject->email;
     $message->name = $userObject->name;
     $message->subject = $this->params->type ? "Þú hefur skráð þig á viðburðinn: {$eventObject->subject}" : "Þú hefur afskráð þig af viðburðinum: {$eventObject->subject}";
     //MAIL
     //	now we want to send this to the user/quest via e-mail
     //	so we try to connect to Queue and send a message
     //	to mail_queue
     try {
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         $msg = new AMQPMessage($message->serialize(), ['delivery_mode' => 2]);
         $this->logger->info("{$userObject->email} is " . ($this->params->type ? '' : 'not ') . "attending {$eventObject->subject}");
         $channel->basic_publish($msg, '', 'mail_queue');
     } catch (\Exception $e) {
         throw new NotifyException($e->getMessage(), 0, $e);
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
         $eventObject = null;
         $userObject = null;
         $this->closeDataSourceDriver();
     }
     return $this;
 }
コード例 #3
0
ファイル: UserValidate.php プロジェクト: bix0r/Stjornvisi
 /**
  * @return $this
  * @throws NotifyException
  */
 public function send()
 {
     //USER
     //	get the user.
     $user = $this->getUser($this->params->user_id);
     $this->logger->debug("User validate [{$user->email}]");
     //VIEW
     //	create and configure view
     $child = new ViewModel(array('user' => $user, 'link' => $this->params->facebook));
     $child->setTemplate('script');
     $layout = new ViewModel();
     $layout->setTemplate('layout');
     $layout->addChild($child, 'content');
     $phpRenderer = new PhpRenderer();
     $phpRenderer->setCanRenderTrees(true);
     $resolver = new Resolver\TemplateMapResolver();
     $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'script' => __DIR__ . '/../../../view/email/user-validate.phtml'));
     $phpRenderer->setResolver($resolver);
     //MAIL
     //	now we want to send this to the user/quest via e-mail
     //	so we try to connect to Queue and send a message
     //	to mail_queue
     try {
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         foreach ($layout as $child) {
             $child->setOption('has_parent', true);
             $result = $phpRenderer->render($child);
             $child->setOption('has_parent', null);
             $capture = $child->captureTo();
             if (!empty($capture)) {
                 $layout->setVariable($capture, $result);
             }
         }
         $result = new Mail();
         $result->name = $user->name;
         $result->email = $user->email;
         $result->subject = "Stjórnvísi, staðfesting á aðgangi";
         $result->body = $phpRenderer->render($layout);
         $result->test = true;
         $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]);
         $this->logger->info("User validate email to [{$user->email}]");
         $channel->basic_publish($msg, '', 'mail_queue');
     } catch (\Exception $e) {
         throw new NotifyException($e->getMessage(), 0, $e);
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
         $user = null;
     }
     return $this;
 }
コード例 #4
0
ファイル: Submission.php プロジェクト: bix0r/Stjornvisi
 /**
  * @return $this
  * @throws NotifyException
  */
 public function send()
 {
     $groupObject = $this->getGroup($this->params->group_id);
     $userObject = $this->getUser($this->params->recipient);
     //VIEW
     //	create and configure view
     $child = new ViewModel(array('user' => $userObject, 'group' => $groupObject));
     $child->setTemplate($this->params->register ? 'group-register' : 'group-unregister');
     $layout = new ViewModel();
     $layout->setTemplate('layout');
     $layout->addChild($child, 'content');
     $phpRenderer = new PhpRenderer();
     $phpRenderer->setCanRenderTrees(true);
     $resolver = new Resolver\TemplateMapResolver();
     $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'group-register' => __DIR__ . '/../../../view/email/group-register.phtml', 'group-unregister' => __DIR__ . '/../../../view/email/group-unregister.phtml'));
     $phpRenderer->setResolver($resolver);
     foreach ($layout as $child) {
         $child->setOption('has_parent', true);
         $result = $phpRenderer->render($child);
         $child->setOption('has_parent', null);
         $capture = $child->captureTo();
         if (!empty($capture)) {
             $layout->setVariable($capture, $result);
         }
     }
     $result = new Mail();
     $result->name = $userObject->name;
     $result->email = $userObject->email;
     $result->subject = $this->params->register ? "Þú hefur skráð þig í hópinn: {$groupObject->name}" : "Þú hefur afskráð þig úr hópnum: {$groupObject->name}";
     $result->body = $phpRenderer->render($layout);
     $result->test = true;
     //MAIL
     //	now we want to send this to the user/quest via e-mail
     //	so we try to connect to Queue and send a message
     //	to mail_queue
     try {
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]);
         $this->logger->info(get_class($this) . ":send" . " {$userObject->email} is " . ($this->params->register ? '' : 'not ') . "joining group {$groupObject->name_short}");
         $channel->basic_publish($msg, '', 'mail_queue');
     } catch (\Exception $e) {
         throw new NotifyException($e->getMessage(), 0, $e);
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
         $userObject = null;
         $groupObject = null;
         $this->closeDataSourceDriver();
     }
     return $this;
 }
コード例 #5
0
ファイル: Password.php プロジェクト: bix0r/Stjornvisi
 /**
  * @return $this
  * @throws NotifyException
  */
 public function send()
 {
     //VIEW
     //	create and configure view
     $child = new ViewModel(array('user' => $this->params->recipients, 'password' => $this->params->password));
     $child->setTemplate('script');
     $layout = new ViewModel();
     $layout->setTemplate('layout');
     $layout->addChild($child, 'content');
     $phpRenderer = new PhpRenderer();
     $phpRenderer->setCanRenderTrees(true);
     $resolver = new Resolver\TemplateMapResolver();
     $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'script' => __DIR__ . '/../../../view/email/lost-password.phtml'));
     $phpRenderer->setResolver($resolver);
     foreach ($layout as $child) {
         $child->setOption('has_parent', true);
         $result = $phpRenderer->render($child);
         $child->setOption('has_parent', null);
         $capture = $child->captureTo();
         if (!empty($capture)) {
             $layout->setVariable($capture, $result);
         }
     }
     $result = new Mail();
     $result->name = $this->params->recipients->name;
     $result->email = $this->params->recipients->email;
     $result->subject = "Nýtt lykilorð";
     $result->body = $phpRenderer->render($layout);
     $result->type = 'Password';
     $result->test = true;
     //MAIL
     //	now we want to send this to the user/quest via e-mail
     //	so we try to connect to Queue and send a message
     //	to mail_queue
     try {
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]);
         $this->logger->info($this->params->recipients->name . " is requesting new password");
         $channel->basic_publish($msg, '', 'mail_queue');
     } catch (\Exception $e) {
         throw new NotifyException($e->getMessage(), 0, $e);
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
     }
     return $this;
 }
コード例 #6
0
ファイル: Digest.php プロジェクト: bix0r/Stjornvisi
 /**
  * @return $this
  * @throws NotifyException
  */
 public function send()
 {
     //ID
     //  create an ID for this digest
     $emailId = $this->getHash();
     //TIME RANGE
     //	calculate time range and create from and
     //	to date objects for the range.
     $from = new DateTime();
     $from->add(new DateInterval('P1D'));
     $to = new DateTime();
     $to->add(new DateInterval('P8D'));
     $this->logger->info("Queue Service says: Fetching upcoming events");
     //EVENTS
     //  fetch all events
     $events = $this->getEvents($from, $to);
     //NO EVENTS
     //	if there are no events to publish, then it's no need
     //	to keep on processing this script
     if (count($events) == 0) {
         $this->logger->info("Digest, no events registered, stop");
         return $this;
     } else {
         $this->logger->info("Digest, " . count($events) . " events registered.");
     }
     //USERS
     //	get all users who want to know
     //	about the upcoming events.
     $users = $this->getUsers();
     $this->logger->info("Digest, " . count($users) . " user will get email ");
     //VIEW
     //	create and configure view
     $child = new ViewModel(array('events' => $events, 'from' => $from, 'to' => $to));
     $child->setTemplate('news-digest');
     $layout = new ViewModel();
     $layout->setTemplate('layout');
     $layout->addChild($child, 'content');
     $phpRenderer = new PhpRenderer();
     $phpRenderer->setCanRenderTrees(true);
     $resolver = new TemplateMapResolver();
     $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'news-digest' => __DIR__ . '/../../../view/email/news-digest.phtml'));
     $phpRenderer->setResolver($resolver);
     //QUEUE
     //	try to connect to Queue and send messages to it.
     //	this will try to send messages to mail_queue, that will
     //	send them on it's way via a MailTransport
     try {
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         foreach ($users as $user) {
             $child->setVariable('user', $user);
             foreach ($layout as $child) {
                 $child->setOption('has_parent', true);
                 $result = $phpRenderer->render($child);
                 $child->setOption('has_parent', null);
                 $capture = $child->captureTo();
                 if (!empty($capture)) {
                     $layout->setVariable($capture, $result);
                 }
             }
             $result = new Mail();
             $result->name = $user->name;
             $result->email = $user->email;
             $result->subject = "Vikan framundan | {$from->format('j. n.')} - {$to->format('j. n. Y')}";
             $result->body = $phpRenderer->render($layout);
             $result->id = $emailId;
             $result->user_id = md5((string) $emailId . $user->email);
             $result->type = 'Digest';
             $result->parameters = 'allir';
             $result->test = false;
             $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]);
             $channel->basic_publish($msg, '', 'mail_queue');
             $this->logger->debug("Queue Service says: Fetching users who want upcoming events, {$user->email} in queue ");
         }
     } catch (\Exception $e) {
         throw new NotifyException($e->getMessage(), 0, $e);
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
         $this->closeDataSourceDriver();
     }
     return $this;
 }
コード例 #7
0
ファイル: Group.php プロジェクト: bix0r/Stjornvisi
 /**
  * Run the handler.
  *
  * @return $this
  * @throws NotifyException
  */
 public function send()
 {
     $emailId = $this->getHash();
     $users = $this->getUsers($this->params->recipients, $this->params->test, $this->params->sender_id, $this->params->group_id);
     $group = $this->getGroup($this->params->group_id);
     $this->logger->info("Group-email in " . ($this->params->test ? '' : 'none') . " test mode");
     //MAIL
     //	now we want to send this to the user/quest via e-mail
     //	so we try to connect to Queue and send a message
     //	to mail_queue
     try {
         //QUEUE
         //	create and configure queue
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         //VIEW
         //	create and configure view
         $child = new ViewModel(array('user' => null, 'group' => $group, 'body' => call_user_func(new Paragrapher(), $this->params->body)));
         $child->setTemplate('script');
         $layout = new ViewModel();
         $layout->setTemplate('layout');
         $layout->addChild($child, 'content');
         $phpRenderer = new PhpRenderer();
         $phpRenderer->setCanRenderTrees(true);
         $resolver = new Resolver\TemplateMapResolver();
         $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'script' => __DIR__ . '/../../../view/email/group-letter.phtml'));
         $phpRenderer->setResolver($resolver);
         //FOR EVERY USER
         //	for every user, render mail-template
         //	and send to mail-queue
         foreach ($users as $user) {
             $child->setVariable('user', $user);
             foreach ($layout as $child) {
                 $child->setOption('has_parent', true);
                 $result = $phpRenderer->render($child);
                 $child->setOption('has_parent', null);
                 $capture = $child->captureTo();
                 if (!empty($capture)) {
                     $layout->setVariable($capture, $result);
                 }
             }
             $result = new Mail();
             $result->name = $user->name;
             $result->email = $user->email;
             $result->subject = $this->params->subject;
             $result->body = $phpRenderer->render($layout);
             $result->user_id = md5((string) $emailId . $user->email);
             $result->id = $emailId;
             $result->type = 'Event';
             $result->entity_id = $group->id;
             $result->parameters = $this->params->recipients;
             $result->test = $this->params->test;
             $msg = new AMQPMessage($result->serialize(), ['delivery_mode' => 2]);
             $this->logger->info("Groupmail to user:{$user->email}, group:{$group->name_short}");
             $channel->basic_publish($msg, '', 'mail_queue');
         }
     } catch (\Exception $e) {
         throw new NotifyException($e->getMessage(), 0, $e);
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
         $users = null;
         $group = null;
         $this->closeDataSourceDriver();
     }
     return $this;
 }
コード例 #8
0
ファイル: Event.php プロジェクト: bix0r/Stjornvisi
 /**
  * @return $this
  * @throws NotifyException
  */
 public function send()
 {
     $emailId = $this->getEmailId();
     $event = $this->getEvent($this->params->event_id);
     $users = $this->getUser($this->getGroupsFromEvent($event), $event->id, $this->params->user_id, $this->params->recipients, $this->params->test);
     $this->logger->info(count($users) . " user will get an email" . "in connection with event {$event->subject}:{$event->id}");
     //VIEW
     //	create and configure view
     $child = new ViewModel(array('user' => null, 'event' => $event, 'body' => call_user_func(new Paragrapher(), $this->params->body)));
     $child->setTemplate('event');
     $layout = new ViewModel();
     $layout->setTemplate('layout');
     $layout->addChild($child, 'content');
     $phpRenderer = new PhpRenderer();
     $phpRenderer->setCanRenderTrees(true);
     $resolver = new Resolver\TemplateMapResolver();
     $resolver->setMap(array('layout' => __DIR__ . '/../../../view/layout/email.phtml', 'event' => __DIR__ . '/../../../view/email/event.phtml'));
     $phpRenderer->setResolver($resolver);
     //CONNECT TO QUEUE
     //	try to connect to RabbitMQ
     try {
         $connection = $this->queueFactory->createConnection();
         $channel = $connection->channel();
         $channel->queue_declare('mail_queue', false, true, false, false);
         //FOR EVER USER
         //	for every user: render email template, create message object and
         //	send to mail-queue
         foreach ($users as $user) {
             $child->setVariable('user', $user);
             foreach ($layout as $child) {
                 $child->setOption('has_parent', true);
                 $result = $phpRenderer->render($child);
                 $child->setOption('has_parent', null);
                 $capture = $child->captureTo();
                 if (!empty($capture)) {
                     $layout->setVariable($capture, $result);
                 }
             }
             $message = new Mail();
             $message->name = $user->name;
             $message->email = $user->email;
             $message->subject = $this->params->subject;
             $message->body = $phpRenderer->render($layout);
             $message->id = $emailId;
             $message->user_id = md5((string) $emailId . $user->email);
             $message->type = 'Event';
             $message->entity_id = $event->id;
             $message->parameters = $this->params->recipients;
             $message->test = $this->params->test;
             $msg = new AMQPMessage($message->serialize(), ['delivery_mode' => 2]);
             $this->logger->info(" {$user->email} will get an email" . "in connection with event {$event->subject}:{$event->id}");
             $channel->basic_publish($msg, '', 'mail_queue');
         }
     } catch (\Exception $e) {
         throw new NotifyException($e->getMessage(), 0, $e);
     } finally {
         if (isset($channel) && $channel) {
             $channel->close();
         }
         if (isset($connection) && $connection) {
             $connection->close();
         }
         $users = null;
         $event = null;
         $this->closeDataSourceDriver();
     }
     return $this;
 }