Skip to content
Advertisement

How to include PHPmailer in functions.php properly (WordPress)

I’m trying to include PHPmailer in functions.php

my code:

JavaScript

function test_mailer () {

JavaScript

}

I also tried to put require_once out of the try catch still the same error here is the snippet about the error

“PHP Fatal error: Uncaught Error: Class ‘PHPMailer’ not found”

I use betheme template and I stored the files PHPmailer in betheme/includes/mail.

Advertisement

Answer

As BA_Webimax pointed out, you should be using WordPress’ built-in email functions, though due to WP’s reliance on outdated PHP versions, you will end up using a very old version of PHPMailer with it.

Back to your current problem: It’s not your require_once statements that are failing, it’s that you have not imported the namespaced PHPMailer classes into your namespace. Add these at the top of your script:

JavaScript

Alternatively, use the FQCN when creating your instance:

JavaScript

Note that this applies to the Exception class too, so you’d need to say:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement