27
  • I tried :include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');

Fatal error: Class 'PHPMailer' not found in C:\Inetpub\wwwroot\php\index.php on line 151

I place the PHPMailerAutoload.php in the same directory as my script.

Can someone help me with this ?

iori
  • 2,708
  • 11
  • 36
  • 72
  • 1
    Where is your `PHPMailerAutoload.php` file located? Also do you include this code which you show here into another file? – Rizier123 Mar 06 '15 at 19:49
  • in here `C:\Inetpub\wwwroot\php\` same path as my **index.php** – iori Mar 06 '15 at 19:50
  • 1
    based on it's name alone, it sounds like the PHPMailerAutoload.php file is trying to locate the actual class file for PHPMailer and is not able to do so. I would look in that file to see where it's trying to look and verify that the actual class file exists in one of those locations – Drew Mar 06 '15 at 19:52
  • @Rizier123 : Should I do this then ? `require_once('C:\Inetpub\wwwroot\php\PHPMailerAutoload.php');` – iori Mar 06 '15 at 19:53
  • @iori Try: `reuqire_once(__DIR__ . "/PHPMailerAutoload.php");` – Rizier123 Mar 06 '15 at 19:54
  • I use this class in my projects and just include the actual class file, include_once('phpmailer/class.phpmailer.php'); – Drew Mar 06 '15 at 19:59
  • @Rizier123 : I got the same error. – iori Mar 06 '15 at 20:05
  • @Drew : I'll let you know. – iori Mar 06 '15 at 20:05
  • @iori Do you include the code which you show us here also into another file? – Rizier123 Mar 06 '15 at 20:05
  • Yes.I place them in the bottom section of my index.php. – iori Mar 06 '15 at 20:06
  • So which file do you show us here? And how do you include it into index.php? – Rizier123 Mar 06 '15 at 20:08
  • There are three files that need to be in the same directory: class.phpmailer.php, class.pop3.php, and class.smtp.php. There is also a directory called 'language' that needs to be sitting at the same level as the 3 files I just mentioned. This directory contains a couple dozen php files for various languages and really the only ones that are required would be for the language you are using. – Drew Mar 06 '15 at 20:12
  • Ohh I see. I thought I only need 1 file. That's I could get this to work. Thanks. I am working on it now. – iori Mar 06 '15 at 20:16
  • 1
    @Drew : After move the whole PHPMaier folder to the same directory as my php file , and include it like this `include_once('\PHPMailer\PHPMailerAutoload.php');` I think it works now. :D Thanks man – iori Mar 06 '15 at 20:21
  • This isn't any great mystery - it's all covered in the readme, so you really didn't need to ask! – Synchro Mar 06 '15 at 20:25
  • You should add your solution as an answer and mark it as solved. – EmilCataranciuc Apr 08 '18 at 00:27
  • Does this answer your question? [Sending email with PHP from an SMTP server](https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server) – Abu Shoeb Jun 07 '20 at 11:06
  • There is no 'PHPMailerAutoload.php' in https://github.com/PHPMailer/PHPMailer/tree/master/src What am I missing? – Luther Jun 24 '20 at 14:03

11 Answers11

103

all answers are outdated now. Most current version (as of Feb 2018) does not have autoload anymore, and PHPMailer should be initialized as follows:

<?php

  require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");
  require("/home/site/libs/PHPMailer-master/src/SMTP.php");

    $mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->IsSMTP(); // enable SMTP

    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "xxxxxx";
    $mail->Password = "xxxx";
    $mail->SetFrom("xxxxxx@xxxxx.com");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("xxxxxx@xxxxx.com");

     if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message has been sent";
     }
?>
avs099
  • 10,000
  • 5
  • 53
  • 105
  • 1
    Are there any further requirements? This Snippet is not working for me with 6.0.5 ---> Fatal error: Class 'PHPMailer\PHPMailer\Exception' not found in PHPMailer.php on line 1411. PHPStorm warns for unhandled Exceptions too. – aProgger Apr 13 '18 at 15:49
  • 1
    @aProgger use following instead https://github.com/PHPMailer/PHPMailer/blob/master/UPGRADING.md */ use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; /* get_sendmail PHPMailer.php istenildigi zaman uzerine kaydedilmek sureti ile guncellenebilir. */ require_once('yourfolder/PHPMailer.php'); //Create a new PHPMailer instance $mail = new PHPMailer; – Deniz Porsuk Apr 17 '18 at 10:28
  • this really works , the trick is to put that actual path to the files or folders i.e starting from the home/site/path – kinsley kajiva Sep 09 '18 at 05:59
  • 3
    `require("/home/site/libs/PHPMailer-master/src/Exception.php");` needs to be first line – Mr Heelis Nov 22 '18 at 22:49
  • 1
    Try with the additional slash in front of the name space like this `$mail = new \PHPMailer\PHPMailer\PHPMailer();` for latest version. – Akeel ahamed Nov 02 '19 at 10:15
  • rason # 99 to hate object oriented and use namespace crap – user889030 Sep 23 '20 at 17:01
14

This answers in an extension to what avs099 has given above, for those who are still having problems:

1.Makesure that you have php_openssl.dll installed(else find it online and install it);

2.Go to your php.ini; find extension=php_openssl.dll enable it/uncomment

3.Go to github and downland the latetest version :6.0 at this time.

4.Extract the master copy into the path that works better for you(I recommend the same directory as the calling file)

Now copy this code into your foo-mailer.php and render it with your gmail stmp authentications.

    require("/PHPMailer-master/src/PHPMailer.php");
    require("/PHPMailer-master/src/SMTP.php");
    require("/PHPMailer-master/src/Exception.php");


    $mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->IsSMTP(); 

    $mail->CharSet="UTF-8";
    $mail->Host = "smtp.gmail.com";
    $mail->SMTPDebug = 1; 
    $mail->Port = 465 ; //465 or 587

     $mail->SMTPSecure = 'ssl';  
    $mail->SMTPAuth = true; 
    $mail->IsHTML(true);

    //Authentication
    $mail->Username = "foo@gmail.com";
    $mail->Password = "*******";

    //Set Params
    $mail->SetFrom("foo@gmail.com");
    $mail->AddAddress("bar@gmail.com");
    $mail->Subject = "Test";
    $mail->Body = "hello";


     if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message has been sent";
     }

Disclaimer:The original owner of the code above is avs099 with just my little input.

Take note of the additional:

a) (PHPMailer\PHPMailer) namespace:needed for name conflict resolution.

b) The (require("/PHPMailer-master/src/Exception.php");):It was missing in avs099's code thus the problem encountered by aProgger,you need that line to tell the mailer class where the Exception class is located.

RickShaw
  • 171
  • 1
  • 3
11

Doesn't sound like all the files needed to use that class are present. I would start over:

  1. Download the package from https://github.com/PHPMailer/PHPMailer by clicking on the "Download ZIP" button on the far lower right of the page.
  2. extract the zip file
  3. upload the language folder, class.phpmailer.php, class.pop3.php, class.smtp.php, and PHPMailerAutoload.php all into the same directory on your server, I like to create a directory on the server called phpmailer to place all of these into.
  4. Include the class in your PHP project: require_once('phpmailer/PHPMailerAutoload.php');
Synchro
  • 29,823
  • 14
  • 69
  • 85
Drew
  • 3,647
  • 3
  • 20
  • 37
  • I include it like this`include_once('\PHPMailer\PHPMailerAutoload.php');` , and it works also. – iori Mar 06 '15 at 20:23
  • Just to explain my edit - As of 5.2.9 (the current release), PHPMailer does not try to load the SMTP class, nor the autoloader, so if you only load the PHPMailer class and try to send using `isSMTP`, it will break. This configuration makes it play nicely with things like composer and makes it easier to work with your own subclasses as it no longer imposes a hard dependency. – Synchro Mar 06 '15 at 20:30
8

This is just namespacing. Look at the examples for reference - you need to either use the namespaced class or reference it absolutely, for example:

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

//Load composer's autoloader
require 'vendor/autoload.php';
  • 1
    This does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Filnor Dec 04 '17 at 12:49
4

I suggest you look into getting composer. https://getcomposer.org Composer makes getting third-party libraries a LOT easier and using a single autoloader for all of them. It also standardizes on where all your dependencies are located, along with some automatization capabilities.

Download https://getcomposer.org/composer.phar to C:\Inetpub\wwwroot\php

Delete your C:\Inetpub\wwwroot\php\PHPMailer\ directory.

Use composer.phar to get the phpmailer package using the command line to execute

cd C:\Inetpub\wwwroot\php
php composer.phar require phpmailer/phpmailer

After it is finished it will create a C:\Inetpub\wwwroot\php\vendor directory along with all of the phpmailer files and generate an autoloader.

Next in your main project configuration file you need to include the autoload file.

require_once 'C:\Inetpub\wwwroot\php\vendor\autoload.php';

The vendor\autoload.php will include the information for you to use $mail = new \PHPMailer;

Additional information on the PHPMailer package can be found at https://packagist.org/packages/phpmailer/phpmailer

Will B.
  • 14,243
  • 4
  • 56
  • 62
  • it also destroys your existing auto_loading, destroys your need to understand code, produces ENORMOUS project bloat, (I've seen 900 times too large projects) , infantalises your understanding of true server structure, is incredibly incompatible with any pre-existing architecture .. this `PHPMailer.php` php is a perfect example - you need three small php classes not all that bloat.. - avoid composer – Mr Heelis Nov 22 '18 at 22:50
  • @mrheelis while your assertions can be true under certain circumstances. Avoiding a dependency manager is not practical advice. In doing so you risk other aspects such as security vulnerabilities, not receiving patches of malfunctioning functions, or not including dependencies for the library you use. None-the-less your comment in general is very opinionated and subjective to your personal preference, rather than based on actual fact. In this circumstance composer would provide the autoloader and only include the required PHPMailer. – Will B. Nov 23 '18 at 00:27
  • it's not an opinion, dependencies are not an enemy - all servers have them let alone software platforms - and you don't get rid of them by using composer you hide that you use them (a big difference) it's much better to document what you have installed - instead of packages simply saying download here "use these 3 files and include these dependencies" there is a load of bloat.. it's not hard to provide organised stuff for sharing I don't know why people opt not to.. the truth is composer is (like a lot of bad ideas) messianic in it's control.. it's a rod for your back rather than a friend – Mr Heelis Nov 23 '18 at 09:48
  • @MrHeelis composer is a PSR-0/4 dependency manager, just like `yum`, `apt-get` or `npm`. You exclaim that composer causes "bloat", but do not elaborate as to how or provide sources to back the claim. Bloat implies that composer causes apps to run slower or use more memory, which is **untrue**. While libraries can require unneeded dependencies. As such due diligence is required when using ANY 3rd party library, with or without composer. You are entitled to your "opinion", but composer is a widely renowned and utilized tool. If it did as you claim, devs would not use it in their projects. – Will B. Nov 26 '18 at 02:21
1

I was with the same problem except with a slight difference, the version of PHPMailer 6.0, by the good friend avs099 I know that the new version of PHPMailer since February 2018 does not support the autoload, and had a serious problem to instantiate the libraries with the namespace in MVC, I leave the code for those who need it.

//Controller
    protected function getLibraryWNS($libreria) {
    $rutaLibreria = ROOT . 'libs' . DS . $libreria . '.php';

    if(is_readable($rutaLibreria)){
        require_once $rutaLibreria;
        echo $rutaLibreria . '<br/>';
    }
    else{
        throw new Exception('Error de libreria');
    }
}

//loginController
    public function enviarEmail($email, $nombre, $asunto, $cuerpo){
    //Import the PHPMailer class into the global namespace
            $this->getLibraryWNS('PHPMailer');
            $this->getLibraryWNS('SMTP');
    //Create a new PHPMailer instance
            $mail = new \PHPMailer\PHPMailer\PHPMailer();
    //Tell PHPMailer to use SMTP
            $mail->isSMTP();
    //Enable SMTP debugging
    //      $mail->SMTPDebug = 0;                                       // 0 = off (for production use),  1 = client messages, 2 = client and server messages  Godaddy POR CONFIRMAR
            $mail->SMTPDebug = 1;                                       // debugging: 1 = errors and messages, 2 = messages only
    //Whether to use SMTP authentication
            $mail->SMTPAuth = true;                                     // authentication enabled
    //Set the encryption system to use - ssl (deprecated) or tls
            $mail->SMTPSecure = 'ssl';                                  //Seguridad Correo Gmail
    //Set the hostname of the mail server
            $mail->Host = "smtp.gmail.com";                             //Host Correo Gmail
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
            $mail->Port = 465;      //587;
    //Verifica si el servidor acepta envios en HTML
            $mail->IsHTML(true);
    //Username to use for SMTP authentication - use full email address for gmail
            $mail->Username = 'tumail@gmail.com';
    //Password to use for SMTP authentication
            $mail->Password = 'tucontraseña';
    //Set who the message is to be sent from
            $mail->setFrom('tumail@gmail.com','Creador de Páginas Web');
            $mail->Subject = $asunto;
            $mail->Body = $cuerpo;
    //Set who the message is to be sent to
            $mail->addAddress($email, $nombre);
    //Send the message, check for errors
    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
        return false;
    } else {
        echo "Message has been sent";
        return true;
    }
0

Just from reading what you have written, you will need to add the file class.phpmailer.php to your directory as well.

Calvin K
  • 396
  • 2
  • 6
0

PHPMailerAutoload needs to be in the same folder as class.phpmailer.php

This is the PHPMailerAutoload code that I assume this:

 $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
Leandro Papasidero
  • 3,624
  • 1
  • 15
  • 33
0
Just download composer and install phpMailler autoloader.php

https://github.com/PHPMailer/PHPMailer/blob/master/composer.json

once composer is loaded use below code:

    require_once("phpMailer/class.phpmailer.php");
    require_once("phpMailer/PHPMailerAutoload.php");

    $mail = new PHPMailer(true); 
            $mail->SMTPDebug = true;
            $mail->SMTPSecure = "tls";
            $mail->SMTPAuth   = true;
            $mail->Username   = 'youremail id';
            $mail->Password   = 'youremail password';
            $mail_from        = "youremail id";
            $subject          = "Your Subject";
            $body             = "email body";
            $mail_to          = "receiver_email";
            $mail->IsSMTP(); 
            try {
                  $mail->Host= "smtp.your.com";
                  $mail->Port = "Your SMTP Port No";// ssl port :465, 
                  $mail->Debugoutput = 'html';
                  $mail->AddAddress($mail_to, "receiver_name");
                  $mail->SetFrom($mail_from,'AmpleChat Team'); 
                  $mail->Subject = $subject;
                  $mail->MsgHTML($body);
                  $mail->Send();
                 $emailreturn = 200;
                } catch (phpmailerException $e) {
                  $emailreturn = $e->errorMessage();             
                } catch (Exception $e) {
                 $emailreturn = $e->getMessage();
                }
    echo $emailreturn;

Hope this will work.

0

I had a number of errors similar to this. Make sure your setFrom email address is valid in $mail->setFrom()

Sanjoy
  • 19
  • 1
0

I resolved error copying the files class.phpmailer.php , class.smtp.php to the folder where the file is PHPMailerAutoload.php, of course there should be the file that we will use to send the email.