You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何保存生成的HTML代码?求PHP及Laravel 9实现方案

解决方案:生成纯HTML并通过Sendy API发送

针对你需要生成无PHP脚本的纯HTML并通过Sendy API发送的需求,以下是原生PHP和Laravel 9两种实现方案:


原生PHP实现(输出缓冲用法)

输出缓冲的核心是在输出HTML前启动缓冲,渲染完数据后捕获缓冲内容,得到纯HTML字符串。

步骤与示例代码

<?php
// 1. 先处理数据库逻辑,获取数据
$username = "";
$password = "";
$hostname = "";
$database = "";
$conn = new mysqli($hostname, $username, $password, $database);
$sql = "SELECT * FROM your_target_table";
$result = $conn->query($sql);
$data = [];
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}
$conn->close();

// 2. 启动输出缓冲:后续所有输出都会暂存到缓冲区
ob_start();
?>
<!DOCTYPE html>
<html lang="en" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
    <title>订阅内容更新</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">* {
            box-sizing: border-box;
        }
        body {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body style="background-color: transparent; margin: 0; padding: 0; -webkit-text-size-adjust: none; text-size-adjust: none;">
<table border="0" cellpadding="0" cellspacing="0" class="nl-container" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: transparent;" width="100%">
    <tbody>
        <tr>
            <td>
                <table align="center" border="0" cellpadding="0" cellspacing="0" class="row row-1" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;" width="100%">
                    <tbody>
                        <?php foreach($data as $item): ?>
                        <tr>
                            <td><?= htmlspecialchars($item['your_column_name']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>
</body>
</html>
<?php
// 3. 捕获缓冲内容并关闭缓冲:$htmlContent即为纯HTML字符串
$htmlContent = ob_get_clean();

// 4. 调用Sendy API发送(根据官方文档调整参数)
$sendyUrl = 'https://your-sendy-domain.com/api';
$postData = [
    'api_key' => 'your-sendy-api-key',
    'to' => 'subscriber@example.com',
    'from' => 'sender@your-domain.com',
    'subject' => '最新订阅内容',
    'html' => $htmlContent,
    // 其他必填参数如列表ID等
];

$ch = curl_init($sendyUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
?>

关键说明

  • ob_start():开启输出缓冲,后续所有HTML/echo内容都会被暂存,不会直接输出到浏览器。
  • ob_get_clean():返回缓冲区的所有内容,并关闭缓冲,此时得到的$htmlContent完全是渲染后的纯HTML,无任何PHP脚本。

Laravel 9实现(更简洁高效)

Laravel的视图系统原生支持将Blade模板渲染为纯HTML字符串,无需手动操作输出缓冲。

步骤1:创建邮件视图

在resources/views/emails/subscription.blade.php中编写HTML模板,用Blade语法渲染数据:

<!DOCTYPE html>
<html lang="en" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
    <title>订阅内容更新</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">* {
            box-sizing: border-box;
        }
        body {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body style="background-color: transparent; margin: 0; padding: 0; -webkit-text-size-adjust: none; text-size-adjust: none;">
<table border="0" cellpadding="0" cellspacing="0" class="nl-container" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: transparent;" width="100%">
    <tbody>
        <tr>
            <td>
                <table align="center" border="0" cellpadding="0" cellspacing="0" class="row row-1" role="presentation" style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;" width="100%">
                    <tbody>
                        @foreach($data as $item)
                        <tr>
                            <td>{{ htmlspecialchars($item->your_column_name) }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>
</body>
</html>

步骤2:渲染视图并调用Sendy API

在控制器或命令类中实现逻辑:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;

class SubscriptionEmailController extends Controller
{
    public function send()
    {
        // 1. 从数据库获取数据
        $data = DB::table('your_target_table')->get();

        // 2. 渲染视图为纯HTML字符串
        $htmlContent = view('emails.subscription', ['data' => $data])->render();

        // 3. 调用Sendy API发送
        $response = Http::post('https://your-sendy-domain.com/api', [
            'api_key' => config('services.sendy.api_key'),
            'to' => 'subscriber@example.com',
            'from' => config('mail.from.address'),
            'subject' => '最新订阅内容',
            'html' => $htmlContent,
            // 其他必填参数如列表ID等
        ]);

        // 处理响应
        return $response->successful() 
            ? response()->json(['message' => '邮件发送成功'])
            : response()->json(['message' => '邮件发送失败'], 500);
    }
}

关键说明

  • view()->render():自动编译Blade模板,将数据渲染为纯HTML字符串,完全不含PHP代码,直接可用于Sendy API的html参数。

内容的提问来源于stack exchange,提问作者Ayoub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 02:55:21