Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class 'PHPMailer\PHPMailer\PHPMailer' not found #1703

Closed
Ruaidri opened this issue Apr 3, 2019 · 33 comments
Closed

Class 'PHPMailer\PHPMailer\PHPMailer' not found #1703

Ruaidri opened this issue Apr 3, 2019 · 33 comments

Comments

@Ruaidri
Copy link

Ruaidri commented Apr 3, 2019

No description provided.

@Synchro
Copy link
Member

Synchro commented Apr 4, 2019

Read the readme file that tells you how to load PHPMailer.

@Synchro Synchro closed this as completed Apr 4, 2019
@Ruaidri
Copy link
Author

Ruaidri commented Apr 4, 2019 via email

@Synchro
Copy link
Member

Synchro commented Apr 4, 2019

I'd really recommend not using the old version. 6.0.x should work fine, you just need to understand namespaces. Either way, I can't tell what you're doing wrong if you don't post your code.

@Ruaidri
Copy link
Author

Ruaidri commented Apr 4, 2019 via email

@Synchro
Copy link
Member

Synchro commented Apr 4, 2019

Looks like your image upload didn't work - but don't post code as images if that's what you did. Post your code directly in here.

@Ruaidri
Copy link
Author

Ruaidri commented Apr 4, 2019

 <?php

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

  require 'vendor/autoload.php';
  $mail = new PHPMailer(true);

  try {
  //Server settings
  // $mail->SMTPDebug = 2;
  $mail->IsSMTP();                                      // Set mailer to use SMTP
  $mail->Host = 'mail.welred.ie';                 // Specify main and backup server
  $mail->Port = 465;                                    // Set the SMTP port
  $mail->SMTPAuth = true;                               // Enable SMTP authentication
  $mail->Username = '[email protected]';                // SMTP username
  $mail->Password = 'Password';                  // SMTP password
  $mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted

  $mail->From = '[email protected]';
  $mail->FromName = 'Wel Red';
  $mail->AddAddress($email, $f_name);  // Add a recipient

@Synchro
Copy link
Member

Synchro commented Apr 4, 2019

That code is correct, which must mean that your composer setup isn't right. Is there a phpmailer folder inside your vendor folder? What happens if you run composer install in your project directory?

@Ruaidri
Copy link
Author

Ruaidri commented Apr 4, 2019

image
Hopefully this image works. I have run composer install inside my project directory.

@Synchro
Copy link
Member

Synchro commented Apr 4, 2019

Hm. That all looks correct too. In vendor/composer/autoload_psr4.php, there should be a line like this:

'PHPMailer\\PHPMailer\\' => array($vendorDir . '/phpmailer/phpmailer/src'),

Perhaps try composer dump-autoload to make it rebuild in the autoloader.

@Ruaidri
Copy link
Author

Ruaidri commented Apr 4, 2019 via email

@cyberwizard-dev
Copy link

hello, im facing same issue.

@NiklasBr
Copy link
Contributor

NiklasBr commented Feb 7, 2023

Have you been following the installation instructions in the README at https://github.com/PHPMailer/PHPMailer#installation--loading?

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@Synchro
Copy link
Member

Synchro commented Feb 7, 2023

Do you see a PHPMailer folder inside your vendor folder? Is there a class mapping line pointing at it (as described in an earlier comment)? Is your vendor folder at the top level of your app?

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@Synchro
Copy link
Member

Synchro commented Feb 7, 2023

Can you show your folder structure?

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@Synchro
Copy link
Member

Synchro commented Feb 7, 2023

I mean a screen shot or something showing how the folders are arranged in your app. I'm trying to establish whether things are in the right place, or whether you have done something like clone PHPMailer instead of installing it. Have you done a composer dump-autoload?

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@Synchro
Copy link
Member

Synchro commented Feb 7, 2023

I don't think image attachments work via email in GitHub, so there is nothing visible. You need to use the web interface.

@cyberwizard-dev
Copy link

vendor

png

@Synchro
Copy link
Member

Synchro commented Feb 7, 2023

That all looks correct. Can you also show your instance creation line, $mail = ....

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@Synchro
Copy link
Member

Synchro commented Feb 7, 2023

That looks ok (ignoring wrong direction slashes in second example). I can only think that something has gone astray in your composer setup. What happens if you start a new project, simply:

  1. mkdir thing
  2. cd thing
  3. composer require phpmailer/phpmailer
  4. php -r "use PHPMailer\PHPMailer\PHPMailer; require 'vendor/autoload.php'; new PHPMailer(); echo 'ok';"

If that succeeds (should print ok), you know your problem is elsewhere in your app's config.

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 7, 2023 via email

@cyberwizard-dev
Copy link

That looks ok (ignoring wrong direction slashes in second example). I can only think that something has gone astray in your composer setup. What happens if you start a new project, simply:

1. `mkdir thing`

2. `cd thing`

3. `composer require phpmailer/phpmailer`

4. `php -r "use PHPMailer\PHPMailer\PHPMailer; require 'vendor/autoload.php'; new PHPMailer(); echo 'ok';"`

If that succeeds (should print ok), you know your problem is elsewhere in your app's config.

Did this and it worked, with no error whatsoever. Now I am confused...

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 9, 2023

`
use PHPMailer\PHPMailer\PHPMailer;
require "vendor/autoload.php";

function sendEmail($email, $message, $subject, $name, $html): bool{
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = SMTP_HOST;
$mail->Port = SMTP_PORT; // typically 587
$mail->SMTPSecure = 'tls'; // ssl is obsolete
$mail->SMTPAuth = true;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
$mail->setFrom(FROM_EMAIL_ADDRESS, APP_NAME);
$mail->addAddress($email, $name);
$mail->Subject = $subject;
if ($html) {
$mail->msgHTML($message); // remove if you do not want to send HTML email
} else {
$mail->AltBody = $message;
}
#$mail->addAttachment('docs/brochure.pdf'); //Attachment, can be skipped
return $mail->send();
}

`

this is inside the functions file and I have the code that sends the mail inside the add_user.php file.

That is the function to sends the mail.

if ($isUser) { try { $emailRes = sendEmail($email, $welcomeText, $emailSubject, $firstName, true); /* $emailSubject = "Welcome to " . APP_NAME; $headers = "From:" . FROM_EMAIL_ADDRESS . "\r\n"; $welcomeText = getWelcomeEmailText($email, $unHashedPassword); mail($email, $emailSubject, $welcomeText, $headers);*/ } catch (\PHPMailer\PHPMailer\Exception $e) { } }

@cyberwizard-dev
Copy link

cyberwizard-dev commented Feb 9, 2023 via email

@manojdashmca
Copy link

manojdashmca commented Feb 20, 2023

I Am facing a issue that Class "PHPMailer" not found, I installed PHPMailer through composer. There is no issue in the local environment. but i am facing the issue in production server.

Below is the code I am using.

require_once APPPATH . '../vendor/phpmailer/phpmailer/src/Exception.php';
require_once APPPATH . '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
require_once APPPATH . '../vendor/phpmailer/phpmailer/src/SMTP.php';

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

function sendEmail($data) {

    $template = $data['template'];
    $tomailid = $data['to'];
    array_key_exists("cc", $data) ? $cc = $data['cc'] : $cc = '';
    array_key_exists("bcc", $data) ? $bcc = $data['bcc'] : $bcc = '';
    array_key_exists("heading", $data) ? $heading = $data['heading'] : $heading = '';
    array_key_exists("attachment", $data) ? $attachment = $data['attachment'] : $attachment = '';
    $subject = $data['subject'];

    $status = false;

    $mail = new \PHPMailer(true);
    $mail->IsSMTP();                               // Set mailer to use SMTP
    $mail->Host = 'smtp.zoho.in';              // Specify main and backup server
    $mail->Port = 587;              // Set the SMTP port
    $mail->SMTPAuth = true;                        // Enable SMTP authentication
    $mail->AuthType = 'LOGIN';
    $mail->Username = 'XXXXXXXXX';          // SMTP username
    $mail->Password = 'XXXXXXX';      // SMTP password
    $mail->SMTPSecure = 'tls';        // Enable encryption, 'ssl' also accepted

    $mail->From = 'XXXXXXXX';
    $mail->FromName = 'XXXXXXX';
    $mail->AddAddress($tomailid);  // Add a recipient
    $mail->IsHTML(true);           // Set email format to HTML
    if (!empty($attachment)) {
        if (is_array($attachment)) {
            for ($x = 0; $x < count($attachment); $x++)
                $mail->AddAttachment($attachment[$x]);
        } else {
            $mail->AddAttachment($attachment);
        }
    }
    $mail->MailerDebug = true;
    $mail->SMTPDebug = 4;
    $mail->Subject = $subject; //'Here is the subject';
    $mail->Body = $template; //'This is the HTML message body <strong>in bold!</strong>';
    $mail->AltBody = '';

    try {
        if (!$mail->Send()) {
            echo 'Mailer Error: ' . $mail->ErrorInfo;
            $status = false;
            exit;
        } else {
            $status = true;
        }
    } catch (Exception $e) {
        $status = false;
    }

    return $status;
}

Any help is appriciated.

@Synchro
Copy link
Member

Synchro commented Feb 20, 2023

Um, you put a \ in front of PHPMailer, so it's looking for the class in the global namespace. Just delete that, and then go read up on how namespaces work!

@Synchro
Copy link
Member

Synchro commented Feb 20, 2023

Also there is no MailerDebug property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants