1

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:

<?php

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.

5
  • 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
    Commented Jan 9, 2019 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? Commented Jan 9, 2019 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" ??!
    – taggartJ
    Commented Jan 9, 2019 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
    Commented Jan 9, 2019 at 14:13
  • The namespace that I'm using is the only one that PHPStorm suggests. Commented Jan 9, 2019 at 14:31

1 Answer 1

0

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.

3
  • 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? Commented Jan 9, 2019 at 14:24
  • 1
    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
    – taggartJ
    Commented Jan 9, 2019 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
    Commented Jan 11, 2019 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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