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

使用PHPMailer在Mollie支付成功后发送邮件失败求助

解决命名空间导致PHPMailer无法实例化的问题

问题分析

当前在webhook.php中声明了命名空间namespace _PhpScoper08ed470ac589;,支付成功后尝试实例化Mail类发送邮件时,PHP会默认在当前命名空间下查找该类,导致报错Class '_PhpScoper08ed470ac589\classes' not found。直接使用new classes/phpmailer/Mail();的写法不符合PHP类实例化语法,因此无法解决问题。

解决方案

1. 全局命名空间实例化(适用于Mail类无自定义命名空间的情况)

如果mail.php中的Mail类没有声明自己的命名空间(属于全局命名空间),在实例化时添加全局命名空间前缀\,明确告诉PHP去全局空间查找该类:

if ($payment->isPaid() && !$payment->hasRefunds() && !$payment->hasChargebacks()) {
    include_once('/home/xxx/domains/xxx.nl/public_html/classes/phpmailer/mail.php');
    
    // 添加全局命名空间前缀
    $mail = new \Mail();
    ...
    $mail->send();
}

2. 使用完整命名空间实例化(适用于Mail类有自定义命名空间的情况)

先查看mail.php文件中Mail类的命名空间声明,例如如果类定义如下:

namespace classes\phpmailer;

class Mail {
    // 类实现代码
}

则需要使用完整的命名空间路径实例化:

if ($payment->isPaid() && !$payment->hasRefunds() && !$payment->hasChargebacks()) {
    include_once('/home/xxx/domains/xxx.nl/public_html/classes/phpmailer/mail.php');
    
    // 使用完整命名空间
    $mail = new \classes\phpmailer\Mail();
    ...
    $mail->send();
}

或者提前导入命名空间,简化代码:

// 在文件顶部(命名空间声明之后)添加use语句
use classes\phpmailer\Mail;

if ($payment->isPaid() && !$payment->hasRefunds() && !$payment->hasChargebacks()) {
    include_once('/home/xxx/domains/xxx.nl/public_html/classes/phpmailer/mail.php');
    
    $mail = new Mail();
    ...
    $mail->send();
}

3. 验证文件加载路径

确保include_once的路径正确,避免因文件未加载导致类不存在的问题,可以增加文件存在性检查:

if ($payment->isPaid() && !$payment->hasRefunds() && !$payment->hasChargebacks()) {
    $mailFilePath = '/home/xxx/domains/xxx.nl/public_html/classes/phpmailer/mail.php';
    if (file_exists($mailFilePath)) {
        include_once($mailFilePath);
        $mail = new \Mail(); // 或对应命名空间的写法
        ...
        $mail->send();
    } else {
        error_log('PHPMailer文件未找到:' . $mailFilePath);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 23:13:15