I'm creating a custom module that uses PHPMailer, which has been installed in the module's vendor folder via composer.

This is the error I get:

Error: Class 'PHPMailer' not found in Drupal\multisend\Service\MailerService->__construct()

PHPMailer is being used in a service class and a PHPMailer object is being created in the service classes constructor. Here is the code:


namespace Drupal\multisend\Service;

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

class MailerService
    private $mailer;
    private $config;
    private $attorney_data;

    public function __construct($attorney_data)
        $this->mailer = new PHPMailer(true);
        $this->config = \Drupal::config('multisend.settings');
        $this->attorney_data = $attorney_data;


I've even tried require() on the PHPMailer class but no luck. If anyone could give any suggestions, I would appreciate it greatly.

  • Are those the right paths for the use statement? Also, strive to use dependency injection for your classes (no new statement or global container). require should never be needed in 99% of cases. How did you install the package? It states that it’s in the modules vendor folder, but it should really be in the root vendor folder. – Kevin Jan 9 at 5:20
  • Thanks @Kevin, I initially installed the package into the module's directory but found that the only time my code would work is if I used a Drupal namespace. So... I added it to the root vendor folder and tried the dependency injection approach but still nothing. What is proper protocol when using a package in a Drupal module? – Jake Stewart Jan 9 at 5:53
  • sooo ... composer require phpmailer/phpmailer installs the package in the root vendor folder like @keven ^^ says should just be "use PHPMailer\PHPMailer\PHPMailer" if not perhas "use phpmailer\PHPMailer\PHPMailer" ??! – Taggart Jensen Jan 9 at 7:02
  • External packages have their own namespaces, using Drupal on them won’t work. I suggest using an IDE like PHPStorm, it will reduce the guessing on namespaces and paths and dependency injection. – Kevin Jan 9 at 14:13
  • The namespace that I'm using is the only one that PHPStorm suggests. – Jake Stewart Jan 9 at 14:31

Yes this is right the onlything is never try to composer require X in a modules directory even though you can include a composer.json file in the module its self always make sure you are in the site root. then the paths will be correct.

  • But then how does one install a module without having to install each dependency manually in the root director if a module does not include its dependencies? – Jake Stewart Jan 9 at 14:24
  • Simple. look at drupal.org/project/address as an example If you download it and open the folder you will see it includes a composer.json file, in the module which defines the 'dependencies' so when you run "composer require 'drupal/address:^1.4'" from the root folder it (composer/drupal) checks for module dependencies and installs them automatically ... see drupal.org/docs/develop/using-composer/… but because you are making your own module , you must first add dependencies manually – Taggart Jensen Jan 9 at 22:20
  • The dependencies are listed in the .info file of the module a d composer.json of that module - but you don’t run composer install from the modules directory. You have to require your dependencies at the root. – Kevin Jan 11 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.