public function testTrue() { $message = new Message(); $message->setSubject('Hundur')->setBody(file_get_contents(__DIR__ . '/mail.test.01.txt'))->addFrom('*****@*****.**', 'ble')->addTo('*****@*****.**', 'hundur'); $attacher = new Attacher($message); $result = $attacher->parse(); }
/** * 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(); } } }