0

I am about to explode! I have followed this step, step by step (as the picture below shows) and it still doesn't work for me!

enter image description here

require_once 'configurations/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Please note that I have copied and pasted the code from GitHub and not changed the username nor the password! If the code above works, I wouldn't get the fatal error of Uncaught Error: Class 'PHPMailer' not found in ....

I have done this exactly as it is in the picture and in the code above (with some minor changes, of course) on my other projects and I have no problems at all with this on those projects!

What have I missed? What is wrong?

Community
  • 1
  • 1
Airikr
  • 5,214
  • 12
  • 50
  • 100
  • whats the whole error message –  Apr 11 '16 at 00:30
  • @Dagon: I only show the important part of the error message. The rest is where the error is located in what file. Nothing more, but okej. Here you go: `Fatal error: Uncaught Error: Class 'PHPMailer' not found in Z:\Google Drive\Hemsidor\min-ekonomi-rebuild\index.php:283 Stack trace: #0 {main} thrown in Z:\Google Drive\Hemsidor\min-ekonomi-rebuild\index.php on line 283` – Airikr Apr 11 '16 at 00:32
  • 1
    Where is the `index.php` file and the line `283`? – Marco Aurélio Deleu Apr 11 '16 at 00:36
  • @MarcoAurélioDeleu `$mail = new PHPMailer;`. I thought you knew that :P And where the `index.php` file is? Well, in the root of the `min-ekonomi-rebuild` folder. – Airikr Apr 11 '16 at 00:37
  • 1
    If you're getting error on `283` as the class could not be found, but not on 282 when you actually require the loader file, it just seems like we don't have enough to help you or some sort of bad karma. Did you hurt anybody this week? – Marco Aurélio Deleu Apr 11 '16 at 00:48
  • well i'm not sure of it but i've worked with this class before and it worked well for me just extracted the PHPMailer-master.zip folder and let every thing as it is and i require it in my file by this way require_once '../inc/PHPMailer-master/PHPMailerAutoload.php'; just try to let all the files as it is and do not remove any one of the extracted folder – PacMan Apr 11 '16 at 00:55

2 Answers2

0

This is the autoloader function:

function PHPMailerAutoload($classname)
{
    //Can't use __DIR__ as it's only in PHP 5.3+
    $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
    if (is_readable($filename)) {
        require $filename;
    }
}

Stick a couple of echoes or breakpoints in there so you can check a) that it's being run and b) that it's trying to load the class. The only way that this function will not attempt to load the class (and thus cause a "class not found" error when you try to use it) is if the class is not both present and readable, so check your ownership and permissions.

Synchro
  • 29,823
  • 14
  • 69
  • 85
  • Thanks! I tried first to comment out the `is_readable()` line (line 5 and 7 in your code above). After that, I've got `Message could not be sent.Mailer Error: SMTP connect() failed` followed by the troubleshooting URL. The thing is that I'm on Windows (everything is readable by default, if you're not on `C:` which I'm not (see the image in my question)). If I go to my other projects that are using your function, they'll work! `echo $filename;` above the `if(is_reada...` prints the `class.phpmailer.php` file with its absolute path. After that, the error message in my question. – Airikr Apr 11 '16 at 08:46
  • 1
    I seem to recall someone having problems with `is_readable` on Windows if it's on a shared volume? The SMTP connection failure suggests that's what's happening to you, and commenting it out allows it to progress to the next problem - the troubleshooting guide has more info on how to diagnose that, and there are many questions on here that address it too. – Synchro Apr 11 '16 at 11:02
  • Yes. I have the project on a shared drive. It's on a NAS that I have connected to. Hopefully it's the only problem :) I'm more calm now. Many thanks for your help. – Airikr Apr 11 '16 at 11:41
0

Provide absolute path for PHPMailerAutoload *(require_once)

Spooky
  • 1,064
  • 13
  • 17
  • `Z:\Google Drive\Hemsidor\min-ekonomi-rebuild\configurations/phpmailer/PHPMailerAutoload.php`. Why do you need it? – Airikr Apr 11 '16 at 08:52
  • I meant that You should use absolute path with require_once, not that I need Your abs. path. :D – Spooky Apr 11 '16 at 12:00
  • Aha! ^^ Please say that next time, and not just say "provide absolute path". – Airikr Apr 11 '16 at 14:04
  • Why would I need Your absolute path? :D Logic is the number one weapon in this world. Doesn't matter now, is it working with absolute path? Does your server have access read/write permissions for that path where PHPmailerAutoload is ? – Spooky Apr 12 '16 at 10:06