【问题标题】:How to include PHPmailer in functions.php properly (Wordpress)如何在functions.php中正确包含PHPmailer(Wordpress)
【发布时间】:2019-07-25 19:12:31
【问题描述】:

我正在尝试将 PHPmailer 包含在 functions.php 中

我的代码:

add_action('wp_ajax_nopriv_test_mailer', 'test_mailer');

函数 test_mailer () {

try {

    require_once(get_template_directory('/includes/mail/PHPMailer.php'));
    require_once(get_template_directory('/includes/mail/Exception.php'));

    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions

    //Server settings
    $mail->SMTPDebug = 4;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'test@gmail.com';                 // SMTP username
    $mail->Password = 'dummypassword!';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('test@gmail.com', 'Mailer Test');
    $mail->addAddress('john.doe@gmail.com', 'John User');     // Add a recipient
    $mail->addReplyTo('test@gmail.com');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject testing';
    $mail->Body    = '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 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

wp_die();

}

我也尝试将 require_once 排除在 try catch 之外,仍然是同样的错误,这里是关于错误的 sn-p

“PHP 致命错误:未捕获的错误:找不到类 'PHPMailer'”

我使用 betheme 模板并将 PHPmailer 文件存储在 betheme/includes/mail 中。

【问题讨论】:

  • 您是否有不想使用内置 wp_mail 功能的原因?他们已经使用 PHPMailer。 codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init
  • 嘿 @BA_Webimax 很棒的信息欢呼我想做的是通过 ajax 发送用户数据,如果在 functions.php (my_function) 中成功,我会获取用户电子邮件并向用户发送确认电子邮件,然后将字段(用户输入)保存在我的表中,我不想在用户提交表单时重新加载整个页面。

标签: php wordpress email phpmailer


【解决方案1】:

正如 BA_Webimax 所指出的,您应该使用 Wordpress 的内置电子邮件功能,但由于 WP 依赖于过时的 PHP 版本,您最终将使用非常旧的 PHPMailer 版本。

回到您当前的问题:失败的不是您的require_once 语句,而是您没有将命名空间PHPMailer 类导入到您的命名空间。在脚本顶部添加这些:

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

或者,在创建实例时使用 FQCN:

$mail = new PHPMailer\PHPMailer\PHPMailer;

请注意,这也适用于 Exception 类,因此您需要说:

catch (PHPMailer\PHPMailer\Exception $e) {

【讨论】:

  • Hey Synchro 我尝试按照您的建议进行操作,但仍然收到错误 PHP 致命错误:未捕获的错误:找不到类 'PHPMailer\PHPMailer\PHPMailer'
  • 我试着把 $mail = new PHPMailer\PHPMailer\PHPMailer;内部和外部尝试捕获但仍然相同可能将PHPmailer放在此目录betheme/includes/mail中是错误的
  • 我只包含了 PHPMailer.php、Exception 和 SMTP.php,也许我错过了要包含的内容?
  • 如果您成功加载类文件并以一种或另一种方式声明 FQCN,我看不出这是如何失败的。也许您正在加载的文件实际上并不包含 PHPMailer 类?
【解决方案2】:

get_template_directory()返回主题的绝对路径,不带任何参数。

试试这个为你的包含:

require_once(get_template_directory().'/includes/mail/PHPMailer.php');
require_once(get_template_directory().'/includes/mail/Exception.php');

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2018-02-20
    相关资源
    最近更新 更多