diff --git a/app/api/controller/Ems.php b/app/api/controller/Ems.php index 00afd372490a53d20777620023c31391f578c7eb..5ec168a08ef26e6c224b129cf4e2b1fa39b96637 100644 --- a/app/api/controller/Ems.php +++ b/app/api/controller/Ems.php @@ -77,7 +77,7 @@ class Ems extends Api if ($ret) { $this->success(__('发送成功')); } else { - $this->error(__('发送失败')); + $this->error(__('发送失败').' '.Emslib::getLastErrorMessage()); } } diff --git a/app/common/library/Ems.php b/app/common/library/Ems.php index cf33f471d1bd750822849544c86c06fdd531558f..614a3e358862183dd3507e7ff44915a4ab25a59d 100644 --- a/app/common/library/Ems.php +++ b/app/common/library/Ems.php @@ -34,6 +34,21 @@ class Ems */ protected static $maxCheckNums = 10; + + /** + * 邮件发送异常的消息回执 + * @var string + */ + protected static $errorMessage = ""; + + private static function setErrorMessage($message) + { + self::$errorMessage = $message; + } + public static function getLastErrorMessage() + { + return self::$errorMessage; + } /** * 获取最后一次邮箱发送的数据 * @@ -80,11 +95,12 @@ class Ems ->send(); } catch (PHPMailerException $e) { $result = false; + self::setErrorMessage($e->getMessage()); } return $result; }); } - $result = Event::trigger('ems_send', $ems); + $result = Event::trigger('ems_send', $ems, true); if (!$result) { $ems->delete(); return false; diff --git a/extend/badou/Email.php b/extend/badou/Email.php index 903e25b1228f63566c4fc2700397d392616bed51..ccd3aec12c78a15a9afb18628cd1b3d1b66c865a 100644 --- a/extend/badou/Email.php +++ b/extend/badou/Email.php @@ -5,6 +5,7 @@ namespace badou; use Throwable; use think\facade\Lang; use PHPMailer\PHPMailer\PHPMailer; +use PHPMailer\PHPMailer\Exception as PHPMailerException; /** * 邮件类 @@ -54,16 +55,17 @@ class Email extends PHPMailer $this->configured = false; } } - if ($this->configured) { - $this->Host = $sysMailConfig['smtp_server']; - $this->SMTPAuth = true; - $this->Username = $sysMailConfig['smtp_user']; - $this->Password = $sysMailConfig['smtp_pass']; - $this->SMTPSecure = $sysMailConfig['smtp_verification'] == 'SSL' ? self::ENCRYPTION_SMTPS : self::ENCRYPTION_STARTTLS; - $this->Port = $sysMailConfig['smtp_port']; - - $this->setFrom($sysMailConfig['smtp_sender_mail'], $sysMailConfig['smtp_user']); + if (!$this->configured) { + throw new PHPMailerException("邮箱配置信息邮件不完整,请先在管理后台配置好邮件服务"); } + $this->Host = $sysMailConfig['smtp_server']; + $this->SMTPAuth = true; + $this->Username = $sysMailConfig['smtp_user']; + $this->Password = $sysMailConfig['smtp_pass']; + $this->SMTPSecure = $sysMailConfig['smtp_verification'] == 'SSL' ? self::ENCRYPTION_SMTPS : self::ENCRYPTION_STARTTLS; + $this->Port = $sysMailConfig['smtp_port']; + + $this->setFrom($sysMailConfig['smtp_sender_mail'], $sysMailConfig['smtp_user']); } /**