/** * @inheritdoc */ public function disconnect() { if ($this->isConnected || $this->Resource) { try { if (is_resource($this->Resource)) { mysql_close($this->Resource); } } catch (Exception $Ex) { LoggerFactory::getRootLogger()->warn($Ex); } $this->Resource = null; $this->isConnected = false; } }
/** * Отправить все письма добавленные в список * Вернет массив с логом шагов отправки и временем выполнения (подключение, авторизация, отправка писем) * @param string $host * @param int $port * @param string $login * @param string $password * @throws RuntimeException * @throws MailException * @return array */ public function send($host, $port = 25, $login = null, $password = null) { if (empty($port)) { $port = 25; } if (empty($this->emails)) { return array(); } $resultLog = array(); $resultLog['total'] = microtime(1); $this->prepareSendout(); $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); socket_set_option($socket, SOL_SOCKET, SO_SNDBUF, 128 * 1024); socket_set_option($socket, SOL_SOCKET, TCP_NODELAY, 0); if ($socket < 0) { throw new RuntimeException('socket_create() failed: ' . socket_strerror(socket_last_error())); } $resultLog['server_name'] = $host . ':' . $port; $resultLog['connecting'] = microtime(1); try { $result = socket_connect($socket, $host, $port); } catch (Exception $e) { throw new MailException('socket_connect() failed (host=' . $host . ', port=' . $port . ')'); } if ($result === false) { throw new MailException('socket_connect() failed: ' . socket_strerror(socket_last_error())); } $resultLog['connecting'] = microtime(1) - $resultLog['connecting']; $this->readSmtpAnswer($socket); $this->writeSmtpResponse($socket, 'EHLO ' . (empty($login) ? 'web5.local' : gethostname())); $this->readSmtpAnswer($socket); if (!empty($login) && !empty($password)) { $resultLog['authentication'] = microtime(1); // @todo: разобраться почему не работает AUTH PLAIN //$this->writeSmtpResponse($socket, 'AUTH PLAIN '.base64_encode(chr(0).$login.chr(0).$password)); $this->writeSmtpResponse($socket, 'AUTH LOGIN'); $this->writeSmtpResponse($socket, base64_encode($login)); $this->writeSmtpResponse($socket, base64_encode($password)); $this->readSmtpAnswer($socket); $resultLog['authentication'] = microtime(1) - $resultLog['authentication']; } if (!empty($this->emailsReady)) { $resultLog['emails_sent'] = microtime(1); $resultLog['emails_count'] = 0; $resultLog['emails'] = array(); foreach ($this->emailsReady as $k => $email) { try { if ($email['TO'] == '*****@*****.**' || $email['TO'] == '*****@*****.**') { LoggerFactory::getRootLogger()->info('Mailer ERROR: ' . 'email=' . $email['TO'] . ', ' . 'host=' . $host); } //добавляем мыла $this->writeSmtpResponse($socket, 'MAIL FROM:' . $email['FROM']); $this->readSmtpAnswer($socket); $this->writeSmtpResponse($socket, 'RCPT TO:' . $email['TO']); $this->readSmtpAnswer($socket); $this->writeSmtpResponse($socket, 'DATA'); $this->readSmtpAnswer($socket); $this->writeSmtpResponse($socket, $email['DATA'] . $this->sep . '.'); $this->readSmtpAnswer($socket); $resultLog['emails'][] = $email['TO']; $resultLog['emails_count']++; } catch (Exception $Ex) { LoggerFactory::getRootLogger()->error($Ex); } unset($this->emailsReady[$k]); } $resultLog['emails_sent'] = microtime(1) - $resultLog['emails_sent']; // для дебага достаточно первых несколько адресов $resultLog['emails'] = array_slice($resultLog['emails'], 0, 2); } $this->writeSmtpResponse($socket, 'QUIT'); $this->readSmtpAnswer($socket); $this->emailsReady = array(); socket_close($socket); // общее время выполнения запроса $resultLog['total'] = microtime(1) - $resultLog['total']; return $resultLog; }
<?php /** * Запуск ответа на аякс запрос. * @author Andrey Mostovoy <*****@*****.**> */ use Core\Service\Exception\ServiceException; use Core\Service\AbstractServicePage; use stalk\Logger\LoggerFactory; include_once 'boot.php'; if (isset($_POST['_service'])) { $serviceName = str_replace('.', '\\', $_POST['_service']); $commandName = $_POST['_command']; /** * @var AbstractServicePage $Service */ $Service = new $serviceName(); $Service->setCommandName($commandName); try { $Service->run(); $Service->successResponse(); } catch (ServiceException $Ex) { LoggerFactory::getRootLogger()->error($Ex); $Service->errorResponse($Ex); } }
/** * {@inheritdoc} */ public function run() { LoggerFactory::getRootLogger()->info('HELLO from ' . APPLICATION_REVISION); }