This commit is contained in:
2020-09-02 19:09:32 +08:00
parent d97e45e201
commit 4e881c628b
3 changed files with 39 additions and 34 deletions
+35 -27
View File
@@ -8,8 +8,10 @@ use Exception;
use HttpServer\Application; use HttpServer\Application;
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\SMTP;
use Snowflake\Config;
use Snowflake\Error\Logger; use Snowflake\Error\Logger;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Timer; use Swoole\Timer;
@@ -81,42 +83,48 @@ abstract class Callback extends Application
/** /**
* @param $email * @return PHPMailer
* @param $nickname * @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 * @param $message
* @throws * @throws
*/ */
protected function system_mail($email, $nickname, $message) protected function system_mail($message)
{ {
$mail = new PHPMailer(true);
try { try {
//Server settings $mail = $this->createEmail();
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output $receives = Config::get('email.receives');
$mail->isSMTP(); // Send using SMTP if (empty($receives) || !is_array($receives)) {
$mail->Host = 'smtp1.example.com'; // Set the SMTP server to send through throw new Exception('接收人信息错误');
$mail->SMTPAuth = true; // Enable SMTP authentication }
$mail->Username = 'user@example.com'; // SMTP username foreach ($receives as $receive) {
$mail->Password = 'secret'; // SMTP password $mail->addAddress($receive['address'], $receive['nickname']); // Add a recipient
$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 $mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'service error';
//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->Body = $message; $mail->Body = $message;
$mail->AltBody = $message; $mail->AltBody = $message;
$mail->send(); $mail->send();
} catch (Exception $e) { } catch (Exception $e) {
$this->addError($e->getMessage(),'email'); $this->addError($e->getMessage(), 'email');
} }
} }
+2 -3
View File
@@ -27,13 +27,12 @@ class OnShutdown extends Callback
/** /**
* @param Server $server * @param Server $server
* @throws ConfigException|ComponentException * @throws ComponentException
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server) 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(); $event = Snowflake::get()->getEvent();
if (!$event->exists(Event::SERVER_SHUTDOWN)) { if (!$event->exists(Event::SERVER_SHUTDOWN)) {
return; return;
+2 -4
View File
@@ -19,7 +19,7 @@ class OnWorkerError extends Callback
* @param int $worker_pid * @param int $worker_pid
* @param int $exit_code * @param int $exit_code
* @param int $signal * @param int $signal
* @throws ConfigException * @throws Exception
*/ */
public function onHandler(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) 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')) { if (!Config::has('email')) {
return; return;
} }
$email = Config::get('email'); $this->system_mail(print_r([
$name = Config::get('nickname', false, 'Admin');
$this->system_mail($email, $name, print_r([
'$worker_pid' => $worker_pid, '$worker_pid' => $worker_pid,
'$worker_id' => $worker_id, '$worker_id' => $worker_id,
'$exit_code' => $exit_code, '$exit_code' => $exit_code,