当函数在文件中并包含文件时,PHPMailer 不工作

PHPMailer not working when function is within and included file

有点奇怪。我使用 PHPMailer 中的代码创建了一个基本测试文件。

# Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'includes/PHPMailer/PHPMailer.php';
require 'includes/PHPMailer/SMTP.php';
require 'includes/PHPMailer/Exception.php';

function sendCampaignEmail ($email, $firstname, $lastname)
    {
    $mail = new PHPMailer(true); // Create a new PHPMailer instance. Passing `true` enables exceptions.
    try {
        //Server settings
        # $mail->SMTPDebug = SMTP::DEBUG_SERVER;       // Enable verbose debug output.  SMTP::DEBUG_SERVER = client and server messages
        # $mail->SMTPDebug = SMTP::DEBUG_CLIENT;       // SMTP::DEBUG_CLIENT = client messages
        $mail->SMTPDebug = SMTP::DEBUG_OFF;       // SMTP::DEBUG_OFF = off (for production use)
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = 'myhostxxxx.co.uk';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'oobaclub@xxxxxxxxx.co.uk';         // SMTP username
        $mail->Password   = 'xxxxxmyEmailPasswordxxxxx';        // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 465;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        //Recipients
        $mail->setFrom('myfromemail.co.uk', 'My Name');
        $mail->addAddress($email, $firstname . ' ' . $lastname);    // Add a recipient
        $mail->addReplyTo('myfromemail.co.uk', 'My Name');

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'Dear ' . $firstname . ' ' . $lastname . '<br/><br/>This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        echo '<br/>Message has been sent to ' . $email . ' (' . $firstname . ' ' . $lastname . ')';
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }


$names= array();

$id=0;

$names[$id]['email'] = "atestemail@gmail.com";
$names[$id]['first_name'] = "Bob";
$names[$id]['last_name'] = "gMail"; $id++;

$names[$id]['email'] = "anothertest@testingmyemail.co.uk";
$names[$id]['first_name'] = "Sid";
$names[$id]['last_name'] = "Smith"; $id++;



$count=0;
while ($count < count($names))
    {
    sendCampaignEmail ($names[$count]['email'], $names[$count]['first_name'], $names[$count]['last_name']);
    $count++;
    }

以上代码运行良好。

所以...然后,我将函数放入一个包含的文件中(我所有的函数都是:“globalfunctions.php”)....现在它显示“致命错误:未捕获错误:Class 'PHPMailer' 未找到

所以,现在,在我 index.php 的顶部,我有:

## PHP MAILER - # Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once 'includes/PHPMailer/PHPMailer.php';
require_once 'includes/PHPMailer/SMTP.php';
require_once 'includes/PHPMailer/Exception.php';

require_once 'includes/connect.php';
require_once 'includes/globalfunctions.php';

我很困惑,因为其他一切正常。我所有的其他功能都有效。我认为名称空间是全局的...但是,我尝试将“使用”代码添加到函数中...但是,正如预期的那样,这也不起作用....

我难住了。

PHP use declarations 是本地别名,仅 应用于它们出现的文件。您必须在 中添加 use 语句每个 使用该 class 的文件;您不能将所有声明放在一个文件中,然后从其他地方包含它。

现在可能是学习如何使用 composer 的好时机,因为它会处理很多事情。