SOURCE DOWNLOAD

木漏茶室源码下载中心

整站 PHP 源码:可浏览文件、复制路径、下载单文件或打包 ZIP。

发布版本 v6.25.231 升级后自动生成独立源码包,下载中心只发送这份冻结包 已排除 config.php / 用户数据 / 上传文件 / 升级备份
komorebi-forum 183 个节点 · PHP 8.3.37 · 完整源码 ZIP 1.31 MB
下载完整源码 ZIP 源码包大小:1.31 MB 安装教程 作者主页 免费开源 免费下载 ↓
COMMUNITY DEPLOYMENTS

已经跑起来的木漏茶室

从源码下载中心下载、完成安装并回传验证的站外站点。每个站点只计 1 次部署下载量;已记录站点会实时读取名称、版本、Logo 与在线状态,本机、内网和证书异常站点无法完成回传。

实时检测
2 个站点已部署

已展示全部 2 个站点。

inc/mailer.php 13.8 KB · 发布包 v6.25.231
下载
<?php
/**
 * 极简 SMTP 发信(支持 SSL / STARTTLS),适配虚拟主机与云服务器
 */

class SmtpMailer
{
    private $host;
    private $port;
    private $user;
    private $pass;
    private $secure; // ssl | tls | none
    private $socket;
    private $timeout = 15;
    public $lastError = '';

    public function __construct($host, $port, $user, $pass, $secure = 'ssl')
    {
        $this->host = trim($host);
        $this->port = (int) $port;
        $this->user = $user;
        $this->pass = $pass;
        $this->secure = in_array($secure, ['ssl', 'tls', 'none'], true) ? $secure : 'ssl';
    }

    public static function fromSettings()
    {
        return new self(
            setting('smtp_host'),
            setting('smtp_port', '465'),
            setting('smtp_user'),
            setting('smtp_pass'),
            setting('smtp_secure', 'ssl')
        );
    }

    private function connect()
    {
        $prefix = $this->secure === 'ssl' ? 'ssl://' : '';
        $errno = $errstr = '';
        $this->socket = @stream_socket_client(
            $prefix . $this->host . ':' . $this->port,
            $errno,
            $errstr,
            $this->timeout,
            STREAM_CLIENT_CONNECT,
            stream_context_create(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]])
        );
        if (!$this->socket) {
            throw new Exception('无法连接 SMTP 服务器:' . $errstr . ' (' . $errno . ')');
        }
        stream_set_timeout($this->socket, $this->timeout);
        $this->expect(220);
    }

    private function read()
    {
        $data = '';
        while (($line = fgets($this->socket, 515)) !== false) {
            $data .= $line;
            if (isset($line[3]) && $line[3] === ' ') {
                break;
            }
        }
        return $data;
    }

    private function expect($code)
    {
        $resp = $this->read();
        if ((int) substr($resp, 0, 3) !== (int) $code) {
            throw new Exception('SMTP 响应异常:' . trim($resp));
        }
        return $resp;
    }

    private function write($cmd)
    {
        fwrite($this->socket, $cmd . "\r\n");
    }

    private $bounce = false;

    public function send($to, $subject, $htmlBody, $fromName = '')
    {
        try {
            $this->connect();
            $this->write('EHLO ' . ($_SERVER['HTTP_HOST'] ?? 'localhost'));
            $this->read();

            if ($this->secure === 'tls') {
                $this->write('STARTTLS');
                $this->expect(220);
                $crypto = STREAM_CRYPTO_METHOD_TLS_CLIENT;
                if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
                    $crypto |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
                }
                if (!stream_socket_enable_crypto($this->socket, true, $crypto)) {
                    throw new Exception('STARTTLS 加密握手失败');
                }
                $this->write('EHLO ' . ($_SERVER['HTTP_HOST'] ?? 'localhost'));
                $this->read();
            }

            if ($this->user !== '') {
                $this->write('AUTH LOGIN');
                $this->expect(334);
                $this->write(base64_encode($this->user));
                $this->expect(334);
                $this->write(base64_encode($this->pass));
                $this->expect(235);
            }

            $from = setting('smtp_from', $this->user);
            if ($from === '') {
                $from = $this->user;
            }
            $this->write('MAIL FROM:<' . $from . '>');
            $this->expect(250);
            $this->write('RCPT TO:<' . $to . '>');
            $this->expect(250);
            $this->write('DATA');
            $this->expect(354);

            $name = $fromName !== '' ? $fromName : setting('smtp_from_name', setting('site_name', '木漏茶室'));
            $headers = [
                'From: ' . $this->mimeHeader($name) . ' <' . $from . '>',
                'To: <' . $to . '>',
                'Subject: ' . $this->mimeHeader($subject),
                'MIME-Version: 1.0',
                'Content-Type: text/html; charset=UTF-8',
                'Content-Transfer-Encoding: base64',
                'Date: ' . date('r'),
                'Message-ID: <' . bin2hex(random_bytes(10)) . '@' . ($_SERVER['HTTP_HOST'] ?? 'localhost') . '>',
            ];
            $body = chunk_split(base64_encode($htmlBody), 76, "\r\n");
            fwrite($this->socket, implode("\r\n", $headers) . "\r\n\r\n" . $body . "\r\n.\r\n");
            $this->expect(250);
            $this->write('QUIT');
            @fclose($this->socket);
            return true;
        } catch (Exception $e) {
            $this->lastError = $e->getMessage();
            if ($this->socket) {
                @fclose($this->socket);
            }
            return false;
        }
    }

    private function mimeHeader($text)
    {
        return '=?UTF-8?B?' . base64_encode($text) . '?=';
    }
}


/**
 * 验证码邮件卡片主题。
 * 所有颜色都写成内联样式,兼容常见邮箱客户端。
 */
function mail_card_themes()
{
    return [
        'huangxiaoyun' => [
            'name' => '黄霄雲 · 星海来信',
            'desc' => '粉紫星海、柔光与音符,适合验证码邮件',
            'bg' => '#f4e9fb',
            'card' => '#fff9fe',
            'accent' => '#a85bd1',
            'accent2' => '#ff8bc4',
            'text' => '#3c2947',
            'muted' => '#8a7397',
            'code_bg' => '#fff0f9',
            'code_color' => '#8e3fb5',
            'badge_bg' => '#f6e4ff',
            'badge_color' => '#8a3caf',
            'motif' => '♪',
            'kicker' => '星海来信 · HUANG XIAOYUN',
        ],
        'sakura' => [
            'name' => '樱雪 · 春日来信',
            'desc' => '樱花粉与奶油白,轻盈温柔',
            'bg' => '#fdf1f3',
            'card' => '#fffafb',
            'accent' => '#e58aa5',
            'accent2' => '#f7b7c8',
            'text' => '#4a3037',
            'muted' => '#9a7c84',
            'code_bg' => '#fff2f5',
            'code_color' => '#c25b79',
            'badge_bg' => '#ffe8ee',
            'badge_color' => '#b34e6d',
            'motif' => '❀',
            'kicker' => '樱雪来信 · SAKURA',
        ],
        'tea' => [
            'name' => '茶室 · 一室茶香',
            'desc' => '青绿与米白,安静克制',
            'bg' => '#edf6ef',
            'card' => '#fbfdf9',
            'accent' => '#5c9b77',
            'accent2' => '#a9c9a2',
            'text' => '#2f4438',
            'muted' => '#738b7d',
            'code_bg' => '#f1f8f1',
            'code_color' => '#3f7a59',
            'badge_bg' => '#e5f3e8',
            'badge_color' => '#2f6b4d',
            'motif' => '茶',
            'kicker' => '木漏茶室 · TEA LETTER',
        ],
        'starry' => [
            'name' => '星夜 · 深空来信',
            'desc' => '深蓝夜空与金色星光,对比清晰',
            'bg' => '#0f172a',
            'card' => '#17213b',
            'accent' => '#8ab4ff',
            'accent2' => '#f2c66d',
            'text' => '#edf3ff',
            'muted' => '#a9b8d6',
            'code_bg' => '#0c1426',
            'code_color' => '#ffd98a',
            'badge_bg' => '#263757',
            'badge_color' => '#ffe3a6',
            'motif' => '✦',
            'kicker' => '星夜来信 · NIGHT SKY',
        ],
        'paper' => [
            'name' => '素笺 · 简洁信纸',
            'desc' => '白纸与蓝灰线条,正式耐看',
            'bg' => '#f3f6f8',
            'card' => '#ffffff',
            'accent' => '#5d7f9a',
            'accent2' => '#a6bfd1',
            'text' => '#334b5c',
            'muted' => '#7f95a5',
            'code_bg' => '#f3f8fc',
            'code_color' => '#3f6b8a',
            'badge_bg' => '#eaf2f7',
            'badge_color' => '#3d6481',
            'motif' => '笺',
            'kicker' => '木漏茶室 · SIMPLE NOTE',
        ],
    ];
}

/** 当前邮件卡片主题 key。 */
function mail_card_theme_key($key = null)
{
    $themes = mail_card_themes();
    $key = $key === null ? (string) setting('mail_card_theme', 'huangxiaoyun') : (string) $key;
    return array_key_exists($key, $themes) ? $key : 'huangxiaoyun';
}

/** 生成验证码邮件卡片。 */
function mail_verification_card(array $data)
{
    $themes = mail_card_themes();
    $key = mail_card_theme_key($data['theme'] ?? null);
    $theme = $themes[$key];
    $site = trim((string) ($data['site'] ?? setting('site_name', '木漏茶室')));
    $title = trim((string) ($data['title'] ?? '验证码'));
    $intro = trim((string) ($data['intro'] ?? '你正在验证账号。'));
    $code = preg_replace('/\D/', '', (string) ($data['code'] ?? ''));
    $note = trim((string) ($data['note'] ?? '验证码 10 分钟内有效。若非本人操作,请忽略本邮件。'));
    $badge = trim((string) ($data['badge'] ?? 'SECURITY CODE'));
    $link = trim((string) ($data['link'] ?? ''));
    $linkLabel = trim((string) ($data['link_label'] ?? '返回木漏茶室'));

    $h = static function ($v) { return htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); };
    $bg = $theme['bg']; $card = $theme['card']; $accent = $theme['accent'];
    $accent2 = $theme['accent2']; $text = $theme['text']; $muted = $theme['muted'];
    $codeBg = $theme['code_bg']; $codeColor = $theme['code_color'];
    $badgeBg = $theme['badge_bg']; $badgeColor = $theme['badge_color'];

    $linkHtml = '';
    if ($link !== '') {
        $linkHtml = '<table role="presentation" cellpadding="0" cellspacing="0" style="margin:22px 0 0"><tr><td style="border-radius:999px;background:' . $h($accent) . '">'
            . '<a href="' . $h($link) . '" style="display:inline-block;padding:11px 22px;color:#ffffff;text-decoration:none;font-size:13px;font-weight:600;letter-spacing:.04em">'
            . $h($linkLabel) . '</a></td></tr></table>';
    }

    return '<!doctype html><html lang="zh-CN"><head><meta charset="utf-8">'
        . '<meta name="viewport" content="width=device-width,initial-scale=1">'
        . '<meta name="color-scheme" content="light only"><title>' . $h($title) . ' · ' . $h($site) . '</title></head>'
        . '<body style="margin:0;padding:0;background:' . $h($bg) . ';font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',\'PingFang SC\',\'Microsoft YaHei\',sans-serif">'
        . '<div style="display:none;max-height:0;overflow:hidden;opacity:0;color:transparent">验证码 ' . $h($code) . ',10 分钟内有效。</div>'
        . '<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:' . $h($bg) . '"><tr><td align="center" style="padding:30px 14px">'
        . '<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="max-width:560px;width:100%;background:' . $h($card) . ';border:1px solid ' . $h($accent2) . ';border-radius:24px;overflow:hidden">'
        . '<tr><td style="height:7px;background:' . $h($accent) . ';background-image:linear-gradient(90deg,' . $h($accent) . ',' . $h($accent2) . ');font-size:0;line-height:0">&nbsp;</td></tr>'
        . '<tr><td style="padding:26px 28px 0">'
        . '<table role="presentation" width="100%" cellpadding="0" cellspacing="0"><tr>'
        . '<td style="vertical-align:top"><div style="font-size:11px;letter-spacing:.2em;color:' . $h($muted) . ';text-transform:uppercase">' . $h($theme['kicker']) . '</div>'
        . '<div style="margin-top:8px;font-size:24px;line-height:1.35;font-weight:700;color:' . $h($text) . '">' . $h($title) . '</div></td>'
        . '<td align="right" style="vertical-align:top"><span style="display:inline-block;width:44px;height:44px;line-height:44px;border-radius:14px;text-align:center;background:' . $h($badgeBg) . ';color:' . $h($badgeColor) . ';font-size:23px">' . $h($theme['motif']) . '</span></td>'
        . '</tr></table></td></tr>'
        . '<tr><td style="padding:14px 28px 0"><p style="margin:0;font-size:14px;line-height:1.8;color:' . $h($muted) . '">' . $h($intro) . '</p>'
        . '<div style="margin-top:18px;padding:22px 16px;border-radius:18px;background:' . $h($codeBg) . ';border:1px solid ' . $h($accent2) . ';text-align:center">'
        . '<div style="font-size:11px;letter-spacing:.24em;color:' . $h($muted) . '">' . $h($badge) . '</div>'
        . '<div style="margin-top:8px;font-size:38px;line-height:1.2;letter-spacing:10px;font-weight:700;color:' . $h($codeColor) . ';font-family:\'SFMono-Regular\',Consolas,\'Liberation Mono\',monospace">' . $h($code) . '</div>'
        . '</div>'
        . '<p style="margin:16px 0 0;font-size:13px;line-height:1.8;color:' . $h($muted) . '">' . $h($note) . '</p>'
        . $linkHtml
        . '</td></tr>'
        . '<tr><td style="padding:22px 28px 24px"><div style="height:1px;background:' . $h($accent2) . ';opacity:.55;font-size:0;line-height:0">&nbsp;</div>'
        . '<p style="margin:16px 0 0;font-size:11px;line-height:1.7;color:' . $h($muted) . '">此邮件由「' . $h($site) . '」系统自动发送,请勿回复。<br>如果这不是你本人的操作,请忽略本邮件,并检查账号安全。</p></td></tr>'
        . '</table><p style="margin:14px 0 0;font-size:11px;color:' . $h($muted) . '">© ' . $h($site) . ' · ' . $h($theme['name']) . '</p>'
        . '</td></tr></table></body></html>';
}

/**
 * 发送邮件:未配置 SMTP 时写入 data/mail.log,便于主机环境排查
 */
function send_mail($to, $subject, $htmlBody)
{
    $log = '[' . now() . '] TO: ' . $to . ' | SUBJECT: ' . $subject . "\n" . $htmlBody . "\n\n";
    @file_put_contents(APP_ROOT . '/data/mail.log', $log, FILE_APPEND);

    if (setting('smtp_enabled') !== '1' || setting('smtp_host') === '') {
        return [false, '管理员尚未启用 SMTP 邮件服务,邮件已写入 data/mail.log'];
    }
    $mailer = SmtpMailer::fromSettings();
    $ok = $mailer->send($to, $subject, $htmlBody);
    return [$ok, $ok ? '邮件已发送' : ('发送失败:' . $mailer->lastError)];
}
主题