/** * @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; }
/** * @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; }
/** * @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; }
/** * @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; }
/** * This is the actual Mail Queue. * * This is the guy who sends out e-mail. He is listening * for messages sent to the RabbitMQ:mail_queue and he will * relay them to the SMTP protocol. * * @throws \RuntimeException */ public function mailAction() { $request = $this->getRequest(); // Make sure that we are running in a console and the user has not tricked our // application into running this action from a public web server. if (!$request instanceof ConsoleRequest) { throw new \RuntimeException('You can only use this action from a console!'); } $sm = $this->getServiceLocator(); $logger = $sm->get('Logger'); /** @var $logger \Zend\Log\Logger */ try { $connectionFactory = $sm->get('Stjornvisi\\Lib\\QueueConnectionFactory'); $connection = $connectionFactory->createConnection(); $channel = $connection->channel(); $channel->queue_declare('mail_queue', false, true, false, false); $logger->info("Mail Queue started, Waiting for messages. To exit press CTRL+C"); //THE MAGIC // here is where everything happens. the rest of the code // is just connect and deconnect to RabbitMQ $callback = function ($msg) use($logger, $sm) { $messageObject = new NotifyMailMessage(); $messageObject->unserialize($msg->body); if (!filter_var($messageObject->email, FILTER_VALIDATE_EMAIL)) { $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); $logger->error("Main mailer: Invalid mail address [{$messageObject->email}]"); return; } $logger->debug("Send e-mail to [{$messageObject->email}, {$messageObject->name}]"); //CREATE / SEND // create a mail message and actually send it $message = new Message(); $message->addTo($messageObject->email, $messageObject->name)->addFrom('*****@*****.**', "Stjórnvísi")->setSubject($messageObject->subject)->setBody($messageObject->body)->setEncoding("UTF-8"); //ATTACHER // you can read all about what this does in \Stjornvisi\Mail\Attacher // but basically what this does is: convert a simple html string into a // multy-part mime object with embedded attachments. $trackerId = $messageObject->user_id ? $messageObject->user_id : ''; //TODO why do I need this? $attacher = new Attacher($message); $message = $attacher->parse("http://tracker.stjornvisi.is/spacer.gif?id={$trackerId}"); //DEBUG MODE // process started with --debug flag if ($this->getRequest()->getParam('debug', false)) { $logger->debug("Mailer:mailAction " . print_r($messageObject, true)); //TRACE MODE // process started with --trace flag } elseif ($this->getRequest()->getParam('trace', false)) { $logger->debug("Mailer:mailAction " . $message->toString()); //NORMAL MODE // process started in normal mode. e-mail will be sent } else { try { $transport = $sm->get('MailTransport'); /** @var $transport \Zend\Mail\Transport\Smtp */ if (method_exists($transport, 'getConnection') && ($protocol = $transport->getConnection())) { if ($protocol->hasSession()) { $transport->send($message); } else { $protocol->connect(); $protocol->helo('localhost.localdomain'); $protocol->rset(); $transport->send($message); } $protocol->quit(); $protocol->disconnect(); } else { $transport->send($message); } $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); if ($messageObject->test) { $logger->debug("This is just a test e-mail notofcation -- we will ignore it"); } else { try { $emailService = $sm->get('Stjornvisi\\Service\\Email'); /** @var $emailService \Stjornvisi\Service\Email */ $emailService->create(array('subject' => $messageObject->subject, 'body' => $messageObject->body, 'hash' => $messageObject->id, 'user_hash' => $messageObject->user_id, 'type' => $messageObject->type, 'entity_id' => $messageObject->entity_id, 'params' => $messageObject->parameters)); $emailService = null; } catch (\Exception $e) { while ($e) { $logger->critical($e->getMessage(), $e->getTrace()); $e = $e->getPrevious(); } } } } catch (\Exception $e) { while ($e) { $logger->critical($e->getMessage(), $e->getTrace()); $e = $e->getPrevious(); } } } }; // end of - MAGIC $channel->basic_qos(null, 1, null); $channel->basic_consume('mail_queue', '', false, false, false, false, $callback); while (count($channel->callbacks)) { $channel->wait(); } $channel->close(); $connection->close(); } catch (\PhpAmqpLib\Exception\AMQPOutOfBoundsException $e) { $logger->alert("Can't start MailQueue: " . get_class($e) . ": {$e->getMessage()}", $e->getTrace()); exit(1); } catch (\PhpAmqpLib\Exception\AMQPProtocolException $e) { $logger->alert("Can't start MailQueue: " . get_class($e) . ": {$e->getMessage()}", $e->getTrace()); exit(1); } catch (\PhpAmqpLib\Exception\AMQPRuntimeException $e) { $logger->alert("Can't start MailQueue: " . get_class($e) . ": {$e->getMessage()}", $e->getTrace()); exit(1); } catch (\PhpAmqpLib\Exception\AMQPConnectionException $e) { $logger->alert("Can't start MailQueue: " . get_class($e) . ": {$e->getMessage()}", $e->getTrace()); exit(1); } catch (\PhpAmqpLib\Exception\AMQPChannelException $e) { $logger->alert("Can't start MailQueue: " . get_class($e) . ": {$e->getMessage()}", $e->getTrace()); exit(1); } catch (\PhpAmqpLib\Exception\AMQPTimeoutException $e) { $logger->alert("Can't start MailQueue: " . get_class($e) . ": {$e->getMessage()}", $e->getTrace()); exit(1); } catch (\PhpAmqpLib\Exception\AMQPException $e) { $logger->alert("Can't start MailQueue: " . get_class($e) . ": {$e->getMessage()}", $e->getTrace()); exit(1); } catch (\Exception $e) { while ($e) { $logger->critical("Exception in MailQueue: {$e->getMessage()}", $e->getTrace()); $e = $e->getPrevious(); } } }
/** * @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; }
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(); } } }
/** * 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; }
/** * @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; }