【问题标题】:PHPMailer not working when function is within and included file当函数包含在文件中并包含文件时,PHPMailer 不起作用
【发布时间】:2022-01-19 14:49:23
【问题描述】:

有些东西很奇怪。我用 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”)...。现在它显示“致命错误:未捕获的错误:类”未找到 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';

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

我被难住了。

【问题讨论】:

    标签: phpmailer


    【解决方案1】:

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

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

    【讨论】:

    • 只是要明确这一点。我从 index.php 文件中删除了 USE 和 INCLUDE 代码,并将其添加到我的包含文件(globalfunctions.php)的顶部......即在函数的上方和外部。这现在有效。谢谢你。 (不喜欢使用 composer。我了解它的好处,但我的小项目不需要它)
    • 很好。请将此答案标记为正确!
    猜你喜欢
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 2016-11-11
    相关资源
    最近更新 更多