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

PHP新手求助:使用PHPMailer发HTML邮件无反应且无报错

PHP邮件发送问题排查与PHPMailer正确使用指导

问题描述

我是PHP纯新手,完全不清楚PHPMailer的使用及实现方式。目前已从GitHub下载PHPMailer代码片段并上传至Cpanel,尝试通过以下PHP脚本发送HTML邮件,但既无报错信息也未收到邮件,恳请告知问题所在并提供解决指导。

我使用的代码:

// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

问题核心分析

  1. 未使用PHPMailer:你当前调用的是PHP原生mail()函数,和你下载的PHPMailer完全无关。原生mail()依赖服务器邮件服务(如Sendmail)配置,Cpanel主机常因未启用、权限不足或配置问题导致邮件静默失败。
  2. 重复收件人设置:代码中同时通过$to参数和headers里的To:字段指定收件人,可能触发邮件服务器的过滤规则。
  3. 无效发件人地址:birthday@example.com是测试域名地址,绝大多数邮件服务商直接拦截这类非真实域名的邮件。

解决步骤

一、先调试原生mail()函数(临时方案)

  • 开启错误提示:在脚本最顶部添加以下代码,显示隐藏的错误信息:
error_reporting(E_ALL);
ini_set('display_errors', 1);
  • 修正收件人设置:删除headers里的To:行,只保留$to参数的收件人,避免重复。
  • 使用真实邮箱:将发件人地址换成你在Cpanel中创建的真实邮箱(如birthday@yourdomain.com),收件人也改用真实邮箱,避免测试地址被拦截。
  • 检查主机配置:登录Cpanel查看邮件服务状态,或联系主机商确认是否启用了邮件发送权限。

二、正确使用PHPMailer(推荐方案)

PHPMailer比原生mail()更稳定,支持SMTP,适配绝大多数主机环境。

  1. 确认文件结构:确保上传到Cpanel的PHPMailer文件完整,包含PHPMailer.php、SMTP.php、Exception.php这三个核心文件(通常在PHPMailer目录下)。
  2. 使用示例脚本:替换以下代码中的真实信息后运行:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 引入PHPMailer核心文件
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// 初始化PHPMailer实例
$mail = new PHPMailer(true);

try {
    // 配置SMTP服务器(使用Cpanel提供的SMTP信息)
    $mail->SMTPDebug = SMTP::DEBUG_OFF; // 调试时可改为SMTP::DEBUG_SERVER查看详细日志
    $mail->isSMTP();
    $mail->Host = 'smtp.yourdomain.com'; // 你的Cpanel主机SMTP地址,一般是smtp+你的域名
    $mail->SMTPAuth = true;
    $mail->Username = 'birthday@yourdomain.com'; // Cpanel创建的真实邮箱账号
    $mail->Password = 'your_email_password'; // 邮箱登录密码
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // 加密方式,选STARTTLS或SSL
    $mail->Port = 587; // SMTP端口,587是常用端口,SSL用465

    // 设置收件人、发件人
    $mail->setFrom('birthday@yourdomain.com', 'Birthday Reminder');
    $mail->addAddress('aidan@example.com', 'Aidan'); // 主收件人
    $mail->addAddress('wez@example.com'); // 另一个收件人
    $mail->addCC('birthdayarchive@example.com'); // CC收件人
    $mail->addBCC('birthdaycheck@example.com'); // BCC收件人

    // 设置邮件内容
    $mail->isHTML(true); // 启用HTML格式
    $mail->Subject = 'Birthday Reminders for August';
    $mail->Body = '
    <html>
    <head>
      <title>Birthday Reminders for August</title>
    </head>
    <body>
      <p>Here are the birthdays upcoming in August!</p>
      <table>
        <tr>
          <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
        </tr>
        <tr>
          <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
        </tr>
        <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
        </tr>
      </table>
    </body>
    </html>
    ';
    $mail->AltBody = 'Here are the birthdays upcoming in August!\n\nPerson | Day | Month | Year\nJoe | 3rd | August | 1970\nSally | 17th | August | 1973'; // 纯文本备用内容,供不支持HTML的邮箱查看

    $mail->send();
    echo '邮件发送成功';
} catch (Exception $e) {
    echo "邮件发送失败: {$mail->ErrorInfo}";
}
?>
  1. 关键注意事项:
  • 替换yourdomain.com、邮箱账号、密码为你自己的真实信息。
  • 调试时将SMTPDebug设为SMTP::DEBUG_SERVER,可查看SMTP连接和发送的详细日志,快速定位问题。
  • 如果Cpanel自带SMTP无法使用,可尝试第三方SMTP服务(如QQ邮箱、Gmail),但需提前开启对应邮箱的SMTP权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 03:28:11