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

如何用CodeIgniter发送Bootstrap HTML邮件?PHP mail函数发收据的正确方法

嘿,我来帮你搞定这个问题!不管是用CodeIgniter框架还是原生PHP的mail()函数,发送Bootstrap样式的HTML邮件其实都有章可循,关键是要处理好邮件的HTML结构和样式兼容性——毕竟邮件客户端对CSS的支持和浏览器可是差不少呢。


一、用CodeIgniter发送Bootstrap格式的HTML邮件

CodeIgniter自带的Email类封装了很多细节,用它来处理邮件会更省心,步骤如下:

1. 配置Email类

你可以在application/config/email.php里全局配置,也可以在控制器里动态设置。推荐用SMTP协议(比原生mail()更可靠,垃圾邮件概率更低),示例配置如下:

$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.example.com'; // 比如smtp.gmail.com
$config['smtp_port'] = 587;
$config['smtp_user'] = 'your-email@domain.com';
$config['smtp_pass'] = 'your-email-password';
$config['smtp_crypto'] = 'tls';
$config['mailtype'] = 'html'; // 必须设为html,否则会发送纯文本
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n"; // 强制用这个换行符,避免邮件格式错乱

然后加载并初始化Email类:

$this->load->library('email');
$this->email->initialize($config);

2. 生成Bootstrap风格的HTML邮件内容

重点提醒:几乎所有邮件客户端都不支持外部CSS文件,所以必须把Bootstrap样式内联到HTML元素里,或者用内嵌的<style>标签(内联样式兼容性最好)。你可以手动写内联样式,也可以用工具把Bootstrap类转成内联代码。

假设你从数据库取出了订单数据,生成收据的HTML模板示例:

// 从数据库获取订单数据
$order = $this->db->get_where('orders', ['id' => $order_id])->row();

// 构建HTML内容
$html_receipt = '
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>您的订单收据</title>
    <style>
        .container { max-width: 800px; margin: 0 auto; padding: 20px; }
        .card { border: 1px solid #eee; border-radius: 8px; padding: 25px; margin-bottom: 20px; }
        .table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        .table th, .table td { padding: 12px; border-bottom: 1px solid #eee; text-align: left; }
        .text-center { text-align: center; }
        .text-success { color: #28a745; font-weight: bold; }
        .footer { margin-top: 30px; font-size: 14px; color: #666; }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <h2 class="text-center">感谢您的订单!</h2>
            <p>尊敬的用户,以下是您的订单详情:</p>
            <table class="table">
                <tr>
                    <th>订单编号</th>
                    <td>' . html_escape($order->order_no) . '</td>
                </tr>
                <tr>
                    <th>下单时间</th>
                    <td>' . date('Y-m-d H:i:s', strtotime($order->created_at)) . '</td>
                </tr>
                <tr>
                    <th>订单总金额</th>
                    <td class="text-success">¥' . number_format($order->total, 2) . '</td>
                </tr>
            </table>
            <div class="footer text-center">
                <p>如有疑问,请联系我们的客服团队</p>
            </div>
        </div>
    </div>
</body>
</html>';

记得用html_escape()转义数据库数据,避免HTML注入问题。

3. 发送邮件

设置收件人、主题和内容,然后执行发送:

$this->email->from('your-email@domain.com', '你的店铺名称');
$this->email->to($order->customer_email);
$this->email->subject('您的订单收据已生成');
$this->email->message($html_receipt);

// 发送并检查结果
if ($this->email->send()) {
    echo '收据邮件已成功发送!';
} else {
    // 调试时可以打印错误信息
    echo '发送失败:' . $this->email->print_debugger();
}

二、用原生PHP mail()函数发送Bootstrap格式收据

如果不用框架,直接用mail()函数,核心是要正确设置邮件头,确保识别为HTML格式:

1. 配置邮件头

必须指定Content-Type为text/html,同时设置字符编码:

$to = $order->customer_email;
$subject = '您的订单收据已生成';
$from = 'your-email@domain.com';

// 构建邮件头
$headers = "From: 你的店铺名称 <$from>\r\n";
$headers .= "Reply-To: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

2. 生成HTML收据内容

和CodeIgniter的示例类似,同样要处理样式兼容性,转义数据:

// 假设用PDO从数据库取数据
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'db_user', 'db_pass');
$stmt = $pdo->prepare("SELECT * FROM orders WHERE id = ?");
$stmt->execute([$order_id]);
$order = $stmt->fetch(PDO::FETCH_OBJ);

$html_receipt = '
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>您的订单收据</title>
    <style>
        .container { max-width: 800px; margin: 0 auto; padding: 20px; }
        .card { border: 1px solid #eee; border-radius: 8px; padding: 25px; margin-bottom: 20px; }
        .table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        .table th, .table td { padding: 12px; border-bottom: 1px solid #eee; text-align: left; }
        .text-center { text-align: center; }
        .text-success { color: #28a745; font-weight: bold; }
        .footer { margin-top: 30px; font-size: 14px; color: #666; }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <h2 class="text-center">感谢您的订单!</h2>
            <p>尊敬的用户,以下是您的订单详情:</p>
            <table class="table">
                <tr>
                    <th>订单编号</th>
                    <td>' . htmlspecialchars($order->order_no) . '</td>
                </tr>
                <tr>
                    <th>下单时间</th>
                    <td>' . date('Y-m-d H:i:s', strtotime($order->created_at)) . '</td>
                </tr>
                <tr>
                    <th>订单总金额</th>
                    <td class="text-success">¥' . number_format($order->total, 2) . '</td>
                </tr>
            </table>
            <div class="footer text-center">
                <p>如有疑问,请联系我们的客服团队</p>
            </div>
        </div>
    </div>
</body>
</html>';

3. 发送邮件

调用mail()函数即可:

if (mail($to, $subject, $html_receipt, $headers)) {
    echo '收据邮件已成功发送!';
} else {
    echo '邮件发送失败,请检查服务器的邮件配置(比如sendmail是否正常)';
}

关键注意事项
  1. 样式兼容性:Outlook等老邮件客户端不支持复杂CSS选择器,尽量用基础样式,把关键样式直接内联到元素上(比如<td style="color:#28a745;">)。
  2. 测试邮件:发送前用Mailtrap之类的工具预览渲染效果,避免在客户邮箱里显示错乱。
  3. 送达率:原生mail()依赖服务器sendmail配置,容易被标记为垃圾邮件;优先用SMTP服务(比如SendGrid、Mailgun)。
  4. 数据安全:所有从数据库或用户输入的内容都要转义,防止HTML注入。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:05:04