isSMTP(); $mailer->Host = $host; $mailer->Port = $port; // SMTPSecure 映射:ssl→隐式 SMTPS;tls→STARTTLS;none/未知→明文。 // 明文场景必须显式关闭 SMTPAutoTLS:PHPMailer 默认会对明文会话自动尝试 // STARTTLS 升级,Mailpit 等纯明文本地服务会被误判为失败。 if ($secure === 'ssl') { $mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; } elseif ($secure === 'tls') { $mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; } else { $mailer->SMTPSecure = ''; $mailer->SMTPAutoTLS = false; } // SMTP 认证仅在配置了用户名时启用(Mailpit 等本地无认证服务不设置) if ($user !== '') { $mailer->SMTPAuth = true; $mailer->Username = $user; $mailer->Password = $pass; } $mailer->CharSet = 'UTF-8'; $mailer->isHTML(true); $mailer->setFrom($from, $fromName); $mailer->addAddress((string) ($target['target_addr'] ?? '')); $mailer->Subject = $title; $mailer->Body = '
' . '

' . $title . '

' . '
' . nl2br($content) . '
' . '
'; $mailer->send(); } catch (PHPMailerException $e) { // send() 抛出的 Exception 消息即 $mailer->ErrorInfo(setError 已拼接 // SMTP lastError/detail/smtp_code),异常链保留原始异常供上游追溯 throw new RuntimeException('邮件发送失败: ' . $e->getMessage(), 0, $e); } return true; } }