commentMail === 'on' &&
$options->commentMailHost &&
$options->commentMailPort &&
$options->commentMailFromName &&
$options->commentMailAccount &&
$options->commentMailPassword &&
$options->commentSMTPSecure
) {
// 注册评论完成时的回调
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('Email', 'send');
}
class Email
{
/**
* 发送评论通知的主入口
*
* @param Widget_Feedback $comment 评论对象
*/
public static function send($comment)
{
try {
$params = array(
'title' => htmlspecialchars($comment->title),
'postlink' => preg_replace('/\/comment-page-\d+#comment-\d+/', '', $comment->permalink),
'permalink' => htmlspecialchars($comment->permalink),
'author' => htmlspecialchars($comment->author),
'text' => self::processCommentText($comment->text),
'mail' => $comment->mail,
);
// 作者评论处理(博主回复)
if ($comment->authorId == $comment->ownerId) {
self::handleAuthorComment($comment, $params);
} else {
self::handleGuestComment($comment, $params);
}
} catch (Exception $e) {
error_log('[Email Plugin] Error: ' . $e->getMessage());
}
}
/**
* 初始化邮件发送器
* 新增SMTP主机连通性检测
*/
private static function initMailer()
{
try {
$options = Helper::options();
$host = $options->commentMailHost;
$port = $options->commentMailPort;
$timeout = 3;
$connection = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (! $connection) {
throw new Exception("SMTP主机不可达: {$host}:{$port} - {$errstr} ({$errno})");
}
fclose($connection);
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->CharSet = 'UTF-8';
$mail->SMTPSecure = $options->commentSMTPSecure;
$mail->Host = $host;
$mail->Port = $port;
$mail->FromName = $options->commentMailFromName;
$mail->Username = $options->commentMailAccount;
$mail->From = $options->commentMailAccount;
$mail->Password = $options->commentMailPassword;
$mail->isHTML(true);
return $mail;
} catch (Exception $e) {
error_log('[Email Plugin] SMTP初始化失败: ' . $e->getMessage());
throw $e;
}
}
/**
* 处理评论内容
* - 转换图片标签为响应式展示
* - 处理表情符号
*
* @param string $content 原始评论内容
* @return string 处理后的HTML内容
*/
private static function processCommentText($content)
{
$content = preg_replace_callback(
'/!\[(.*?)\]\((.*?)\)/',
function ($matches) {
$alt = htmlspecialchars($matches[1]);
$siteUrl = Helper::options()->siteUrl;
$src = rtrim($siteUrl, '/') . '/' . ltrim($matches[2], '/');
return '';
},
$content
);
if (function_exists('formatEmoji')) {
$content = formatEmoji($content, false);
}
return $content;
}
/**
* 构建邮件HTML正文
* 新增原评论内容展示区域
*
* @param string $title 邮件标题
* @param string $subtitle 邮件副标题
* @param string $content 当前评论内容
* @param string $originalContent 原评论内容(新增)
* @param string $permalink 评论链接
* @return string 完整的HTML邮件正文
*/
private static function buildEmailBody($title, $subtitle, $content, $originalContent, $permalink)
{
$options = Helper::options();
$siteUrl = rtrim($options->siteUrl, '/');
$favicon = $options->favicon;
if ($favicon && ! parse_url($favicon, PHP_URL_SCHEME)) {
$favicon = $siteUrl . '/' . ltrim($favicon, '/');
}
$originalSection = '';
if (! empty($originalContent)) {
$originalSection = <<