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

使用PHPMailer发送订单邮件时遭遇SMTP类未找到错误的技术求助

Fixing the "SMTP class not found" Error in PHPMailer

Hey there! Let's break down why you're hitting this error and how to fix it quickly.

The Root Cause

Looking at your code, you're using the SMTP::DEBUG_SERVER constant to enable debug output, but you haven't added the necessary use statement for the SMTP class. Even though you've required the SMTP.php file, PHP doesn't know where to find the SMTP class without the namespace declaration.

Step-by-Step Fixes

1. Add the SMTP Class Use Statement

At the top of your code, right after your existing use declarations, add this line:

use PHPMailer\PHPMailer\SMTP;

Your complete use block should look like this:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP; // This line was missing!

This tells PHP exactly where to locate the SMTP class and its constants like DEBUG_SERVER.

2. Verify File Paths

Double-check that the path to SMTP.php in your require statement is correct:

require 'PHPMailer-master/src/SMTP.php';

Make sure the PHPMailer-master/src/ directory exists in your project, and watch out for case sensitivity (especially if you're running this on a Linux server—filenames like smtp.php won't work if the file is actually named SMTP.php).

3. Optional: Use Composer Autoload (Cleaner Approach)

If you installed PHPMailer via Composer, you can skip manually requiring each file by using Composer's autoloader. Replace your three require lines with just this:

require 'vendor/autoload.php';

This will automatically load all PHPMailer classes when they're needed, so you don't have to worry about missing require statements.

Quick Refactoring Tip

You're creating two separate PHPMailer instances in your function (one for the merchant, one for Shoply). To avoid repeating code, you could extract the SMTP setup into a helper function, like this:

function createMailerInstance() {
    $mail = new PHPMailer(true);
    try {
        // Reusable server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;
        $mail->isSMTP();
        $mail->Host = 'mail.shoply.ng';
        $mail->SMTPAuth = true;
        $mail->Username = 'myusername';
        $mail->Password = 'mypassword';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port = 465;
        $mail->setFrom('orders@shoply.ng', 'SHOPLY');
        return $mail;
    } catch (Exception $e) {
        return false;
    }
}

Then you can reuse this instance for both emails, making your code cleaner and easier to update later.

内容的提问来源于stack exchange,提问作者Eluan David Harold

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 19:27:49