diff --git a/http-server/Abstracts/Callback.php b/http-server/Abstracts/Callback.php index c9336e97..925205e8 100644 --- a/http-server/Abstracts/Callback.php +++ b/http-server/Abstracts/Callback.php @@ -8,8 +8,10 @@ use Exception; use HttpServer\Application; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; +use Snowflake\Config; use Snowflake\Error\Logger; use Snowflake\Event; +use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; use Swoole\Timer; @@ -81,42 +83,48 @@ abstract class Callback extends Application /** - * @param $email - * @param $nickname + * @return PHPMailer + * @throws \PHPMailer\PHPMailer\Exception + * @throws ConfigException + */ + private function createEmail() + { + $mail = new PHPMailer(true); + $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output + $mail->isSMTP(); // Send using SMTP + $mail->Host = Config::get('email.host'); // Set the SMTP server to send through + $mail->SMTPAuth = true; // Enable SMTP authentication + $mail->Username = Config::get('email.username'); // SMTP username + $mail->Password = Config::get('email.password'); // SMTP password + $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged + $mail->Port = Config::get('email.port'); // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above + $mail->setFrom(Config::get('email.send.address'), Config::get('email.send.nickname')); + return $mail; + } + + + /** * @param $message * @throws */ - protected function system_mail($email, $nickname, $message) + protected function system_mail($message) { - $mail = new PHPMailer(true); try { - //Server settings - $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output - $mail->isSMTP(); // Send using SMTP - $mail->Host = 'smtp1.example.com'; // Set the SMTP server to send through - $mail->SMTPAuth = true; // Enable SMTP authentication - $mail->Username = 'user@example.com'; // SMTP username - $mail->Password = 'secret'; // SMTP password - $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged - $mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above - - //Recipients - $mail->setFrom('system@example.com', '系统管理员'); - $mail->addAddress($email, $nickname); // Add a recipient - - // Attachments -// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments -// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name - - // Content - $mail->isHTML(true); // Set email format to HTML - $mail->Subject = 'Here is the subject'; + $mail = $this->createEmail(); + $receives = Config::get('email.receives'); + if (empty($receives) || !is_array($receives)) { + throw new Exception('接收人信息错误'); + } + foreach ($receives as $receive) { + $mail->addAddress($receive['address'], $receive['nickname']); // Add a recipient + } + $mail->isHTML(true); // Set email format to HTML + $mail->Subject = 'service error'; $mail->Body = $message; $mail->AltBody = $message; - $mail->send(); } catch (Exception $e) { - $this->addError($e->getMessage(),'email'); + $this->addError($e->getMessage(), 'email'); } } diff --git a/http-server/Events/OnShutdown.php b/http-server/Events/OnShutdown.php index 130b57a8..3b10f124 100644 --- a/http-server/Events/OnShutdown.php +++ b/http-server/Events/OnShutdown.php @@ -27,13 +27,12 @@ class OnShutdown extends Callback /** * @param Server $server - * @throws ConfigException|ComponentException + * @throws ComponentException * @throws Exception */ public function onHandler(Server $server) { - $this->system_mail(Config::get('email'), Config::get('nickname'), 'server shutdown~'); - + $this->system_mail('server shutdown~'); $event = Snowflake::get()->getEvent(); if (!$event->exists(Event::SERVER_SHUTDOWN)) { return; diff --git a/http-server/Events/OnWorkerError.php b/http-server/Events/OnWorkerError.php index 661f2b60..e85d9ddf 100644 --- a/http-server/Events/OnWorkerError.php +++ b/http-server/Events/OnWorkerError.php @@ -19,7 +19,7 @@ class OnWorkerError extends Callback * @param int $worker_pid * @param int $exit_code * @param int $signal - * @throws ConfigException + * @throws Exception */ public function onHandler(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) { @@ -27,9 +27,7 @@ class OnWorkerError extends Callback if (!Config::has('email')) { return; } - $email = Config::get('email'); - $name = Config::get('nickname', false, 'Admin'); - $this->system_mail($email, $name, print_r([ + $this->system_mail(print_r([ '$worker_pid' => $worker_pid, '$worker_id' => $worker_id, '$exit_code' => $exit_code,