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

Loading PHPMailer 6.0 #959

Closed
ridera opened this issue Jan 11, 2017 · 28 comments
Closed

Loading PHPMailer 6.0 #959

ridera opened this issue Jan 11, 2017 · 28 comments

Comments

@ridera
Copy link

ridera commented Jan 11, 2017

Having problem with upgrading phpMailer 5.xx to 6.0. Used several versions of phpmailer for years, no problems like this. I prefer to not use composer.

From upgrade guide "Alternatively, if you're not using composer, copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each one manually." I prefer to not modify the include_path file on a shared host.

I used the PHPMailerAutoload.php for my 5.xx versions

I used this from the 6.0 package: "Upgrading from PHPMailer 5.2 to 6.0 If you're not using composer, you can still load the classes manually, depending on what you're using:" ```

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

require 'src/PHPMailer.php';
require 'src/SMTP.php';
// No errors reported on these

$mail = new PHPMailer(true);
Fatal error: Uncaught Error: Class 'PHPMailer' not found in ///path to my code

Tried $mail = new \PHPMailer(true);
Tried lower case file names for PHPMailer.php and SMTP.php
Tried class.PHPMailer.php and class.SMTP.php; and with lowercase filenames

@Synchro
Copy link
Member

Synchro commented Jan 11, 2017

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;
$mail = new PHPMailer(true);

or

$mail = new PHPMailer\PHPMailer\PHPMailer(true);

@Synchro Synchro closed this as completed Jan 11, 2017
@Synchro
Copy link
Member

Synchro commented Jan 11, 2017

Just to confirm, I altered the beginning of the gmail example to look like this (i.e. to not use composer), and it worked fine:

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

require '../src/PHPMailer.php';
require '../src/SMTP.php';
require '../src/Exception.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

@ridera
Copy link
Author

ridera commented Jan 11, 2017

No work I copied your lines to make certain of no typo errors,
These are in my config file, which is called before the functions file
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require MAILER_DIR .'/src/PHPMailer.php';
require MAILER_DIR .'/src/SMTP.php';
require MAILER_DIR .'/src/Exception.php';
No errors on these requires

This is in my functions file
$mail = new PHPMailer; I even took out the (test)
same error
Fatal error: Uncaught Error: Class 'PHPMailer' not found in .....
using PHP Ver: 7.1.0;

@ridera
Copy link
Author

ridera commented Jan 11, 2017 via email

@Synchro
Copy link
Member

Synchro commented Jan 11, 2017

Ah, well that's a PHP issue. Use statements only apply in the file in which they are defined. As the docs say:

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

If you move the use statements to your functions file, it will work.

@ridera
Copy link
Author

ridera commented Jan 11, 2017

Many thanks. That worked

Just so I'm clear. Does version 6 require the use of namespace? If so, would be good to make this point explicit in the upgrade [Upgrading from PHPMailer 5.2 to 6.0] and examples files.

@Synchro
Copy link
Member

Synchro commented Jan 11, 2017

Yes - The classes are namespaced, so any references you make to them must respect that, just like anything that uses a namespace. It's is covered pretty thoroughly in the upgrade guide, which is linked from the readme, though the link doesn't work because it's expecting it to be in master, which it's not, yet. It's already reflected in all the example files in the 6.0 branch, though obviously not in the 5.2/master branch.

@ridera
Copy link
Author

ridera commented Jan 12, 2017 via email

@Synchro
Copy link
Member

Synchro commented Jan 12, 2017

Unfortunately you can't have it both ways - it's all or nothing. Namespacing is pretty important for libraries - there have been quite a few issues caused by class name clashes with SMTP in particular, it's simply expected of any modern library, and it's probably the single most requested feature.

This issue isn't specific to PHPMailer, it's exactly the same with all namespaced libraries & frameworks, which now covers Symfony, Zend, Laravel, Drupal, and pretty much everything else now. In your case you were doing something that would break any namespaced library, not just PHPMailer. All this kind of messing about with loading classes and defining things in the right place manually is precisely why composer exists; if you're using composer, you can pretty much forget about ever needing to know where a class file is.

Anyway, I can make the wording a little stronger to make it unambiguous.

Synchro added a commit that referenced this issue Jan 12, 2017
@andrealansp
Copy link

Hello, I used the composer for install phpmailer, on my project, but when i call de autoload i receive the error Uncaught Error: Class 'PHPMailer' not found in C:\wamp64\www\php\tpl\email.php:24 Stack trace: #0 {main} thrown in C:\wamp64\www\php\tpl\email.php on line 24.

What do i can do ?

Thanks for help!

@Synchro
Copy link
Member

Synchro commented Dec 4, 2017

I can't tell from what you posted. Do what it says in the readme, base your code on the examples provided.

@sunil-tamang
Copy link

Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer' not found

used this $mail = new PHPMailer(true);

almost tried everythin help......

@Synchro
Copy link
Member

Synchro commented Feb 26, 2018

Read this thread, do what it says in the readme, base your code on the examples provided.

@sumiah4
Copy link

sumiah4 commented Jun 14, 2019

Hi synchro,

I have the same problem.And I am navigating to this page using ajax.I installed it manually unziped the file and just added to my folder.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require ('PHPMailer/src/PHPMailer.php');
require ('PHPMailer/src/Exception.php');

$mail = new \PHPMailer(true);
I did all the way i could still the error alerts like
Uncaught Error: Class 'PHPMailer'.Please help .What to do?

@Synchro
Copy link
Member

Synchro commented Jun 14, 2019

Do what it says in this thread. You have exactly the same problem and should use the same solution.

@sumiah4
Copy link

sumiah4 commented Jun 14, 2019

I tried everypart.
Now it says 'Uncaught PHPMailer\PHPMailer\Exception:' when i changed the script to
use PHPMailer\PHPMailer\Exception;
require ('include/PHPMailer/src/Exception.php');
require ('include/PHPMailer/src/PHPMailer.php');

$rid=$superCage->post->getEscaped('rid');
$mail = new PHPMailer\PHPMailer\PHPMailer(true);

Am I doing it right?

@Synchro
Copy link
Member

Synchro commented Jun 14, 2019

Yes, except that you have asked PHPMailer to throw exceptions (by passing true to the constructor), but then you are not catching the exceptions (hence the uncaught exception error). Look at the exceptions example for how to handle this.

@sumiah4
Copy link

sumiah4 commented Jun 14, 2019

When I tried exception error catch i git the error like 'syntax error, unexpected 'use' (T_USE)',I am not sure what to do?

@Synchro
Copy link
Member

Synchro commented Jun 14, 2019

Just like the examples say:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'include/PHPMailer/src/Exception.php';
require 'include/PHPMailer/src/PHPMailer.php';

$mail = new PHPMailer(true);
try {
    //The rest of your mail sending code goes here
} catch (Exception $e) {
    echo 'Error: ', $e->getMessage();
}

If your code fails on the first use statement, it means you are using a very old version of PHP, and you should upgrade.

@sumiah4
Copy link

sumiah4 commented Jun 14, 2019

ok.I checked and found it to be the latest version.that is PHP Version 7.1.29.This is the latest version.

@Synchro
Copy link
Member

Synchro commented Jun 14, 2019

Current PHP is 7.3.6, but 7.1.x is new enough to have support for use, so you must be doing something else in your code.

@sumiah4
Copy link

sumiah4 commented Jun 14, 2019

OK.Thanks for your response.Iwill recheck it

@HrDv
Copy link

HrDv commented Jul 23, 2019

Hi Synchro, Hi everybody. I'm a new member. Mojave OS X. MAMP php7.3.1. Google emails.

I created a folder named "email" inside htdocs. Inside this folder, I created email.php (directly from your simple example) and I placed manually the folder "PHPMailer-master" and "vendor". I've just created a new google account for my trials : "[email protected]" with his password "pwdAccount2". To send an email-test to my previous email "[email protected]".

The result : the mail is told sent. Was severely blocked by google, with about 20 lines of error from PHP mailer. Here is my code :

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
require 'PHPMailer-master/src/Exception.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

try {
	//Server settings
	$mail->SMTPDebug = 2;                                       // Enable verbose debug output
	$mail->isSMTP();                                            // Set mailer to use SMTP
	$mail->Host       = 'smtp.gmail.com';  						// Specify main and backup SMTP servers
	$mail->SMTPAuth   = true;                                   // Enable SMTP authentication
	$mail->Username   = '[email protected]';                   // SMTP username
	$mail->Password   = 'pwdAccount2';                          // SMTP password
	$mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
	$mail->Port       = 587;                                    // TCP port to connect to

	//Recipients
	$mail->setFrom('[email protected]', 'Mailer');
	$mail->addAddress('account1.com', 'Joe User');     // Add a recipient
//	$mail->addAddress('[email protected]');               // Name is optional
//	$mail->addReplyTo('[email protected]', 'Information');
//	$mail->addCC('[email protected]');
//	$mail->addBCC('[email protected]');
	
	// Attachments
//	$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//	$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
	
	// Content
	$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';
	
	$mail->send();
	echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

@PHPMailer PHPMailer deleted a comment from HrDv Jul 23, 2019
@PHPMailer PHPMailer deleted a comment from HrDv Jul 23, 2019
@Synchro
Copy link
Member

Synchro commented Jul 23, 2019

Welcome to Github. Please don't post comments on old, closed issues, especially when they are not related to your problem - it sends notifications to everyone that has ever been part of the thread.

It sounds like your script is working fine, but you have debug output enabled, so turn it off by setting SMTPDebug = 0.

@HrDv
Copy link

HrDv commented Jul 23, 2019

Sorry, 'didnt' see the "closed" mention at the top. I'll try your new setting, thank you

@HrDv
Copy link

HrDv commented Jul 24, 2019 via email

@Synchro
Copy link
Member

Synchro commented Jul 24, 2019

You said the email was sent? In that case set SMTPDebug = 2 again, read what the output says, and read the troubleshooting guide to find out what to do about it.

@Sweepr
Copy link

Sweepr commented Sep 8, 2020

Really annoying when they go bump in the night, did you rtfm?

Stop hijacking closed topics.

@PHPMailer PHPMailer deleted a comment from sadhviadigh Sep 8, 2020
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

7 participants