0

This post has evolved from an initial problem where I had copied in code that caused an error due to formatting. I am now struggling with a new error. The inital problem has changed to a redundant one. I will check other posts for a potential fix. I have been following along with a tutorial about how to send email using PHPmailer and SMTP. I am using Xampp localhost. I have installed PHPmailer using composer. I am receiving the error: SMTP Error: Could not authenticate. SMTP Error: Could not authenticate. message could not be sentSMTP Error: Could not authenticate.

Help would be appreciated.

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

require_once 'vendor/autoload.php';

$m = new PHPMailer(true);


try{
$m->SMTPDebug = 2;
$m->isSMTP();
$m->Host = 'smtp.gmail.com';
$m->SMTPAuth = true;


$m->Username = 'munsonatl';
$m->Password = ' ';

$m->SMTPSecure = 'SSL';
$m->Port = 465;

$m->setFrom('munsonatl@gmail.com', 'Mailer');
$m->addAddress('munsonatl@gmail.com', 'Matt Macy');
$m->addReplyto('reply@mattmacy.com', 'replyAdress');


$m->Subject = "Here is an Email";
$m->Body = "This is the body of the email";
$m->AltBody = "This is the body of an email";
$m->send();
echo "message has been sent";

} catch (Exception $e){
echo "message could not be sent", $m->ErrorInfo;
}

?> enter image description here

munson
  • 61
  • 2
  • 10
  • Place your `require_once` line below the `use` lines – brombeer Feb 24 '18 at 19:24
  • Then I get another error on require_once: Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE) – munson Feb 24 '18 at 19:42
  • I even try using parenthesis around require_once: require_once ('vendor/autoload.php'); and get error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE) – munson Feb 24 '18 at 19:48
  • Did you use `composer` to install [PHPMailer](https://github.com/PHPMailer/PHPMailer)? – brombeer Feb 24 '18 at 20:01
  • Yes I used composer tp install PHPmailer. – munson Feb 24 '18 at 20:04
  • Are you running an old PHP version? – Synchro Feb 24 '18 at 20:11
  • No I am not just updated to 7.2.2 yesterday – munson Feb 24 '18 at 20:12
  • Tired changing uses to phpmailer lowercase and still receive error – munson Feb 24 '18 at 20:13
  • Make a file that contains nothing but the use and require statements, see if you still get the error. If you do, do you perhaps have a preloaded file in your PHP config that is interfering? – Synchro Feb 24 '18 at 20:23
  • I made the file and it still gives me the error – munson Feb 24 '18 at 20:27
  • I just installed complete new version of xampp yesterday. It was clean install. Did the very straight forward install through composer. I don't believe my file organization in composer is faulty but will double check – munson Feb 24 '18 at 20:29
  • When I comment out the use statement require "vendor/autoload.php" doesnt raise error. I didnt get the use statements from the tutorial. they were in the comments. something is wrong with them – munson Feb 24 '18 at 20:31
  • I tired to make changes to the use statements but dont entirely grasp their operation. when commented out of the file you told me to make I receive no errors. – munson Feb 24 '18 at 20:32
  • I comment out use PHPMailer\PHPMailer\Exception and I now I have an error on the process: Invalid address: (From): root@localhost bool(false) – munson Feb 24 '18 at 20:41
  • What version of PHPMailer? What tutorial did you follow? If all fails, start a new project and follow the installation instructions on the PHPMailer github https://github.com/PHPMailer/PHPMailer – brombeer Feb 24 '18 at 20:49
  • I followed this its very good:https://www.youtube.com/watch?v=mQ3TXOxj7cs&t=5s I downloaded from packagist everything should be fine. I commented out use... \Exception and now get: SMTP NOTICE: EOF caught while checking if connected The following From address failed: munsonatl@gmail.com – munson Feb 24 '18 at 20:57
  • I have the suspession that I might be receiving an error because of what I commented out – munson Feb 24 '18 at 20:58
  • I had to change the app access to allow less secure access: https://stackoverflow.com/questions/3949824/smtp-error-could-not-authenticate-in-phpmailer – munson Feb 24 '18 at 22:45

1 Answers1

1

Either lose the parentheses or the space after the function name.

require_once ‘vendor/autoload.php’;

Or

require_once(‘vendor/autoload.php’);
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Still receiving error with require_once: require_once 'vendor/autoload.php'; – munson Feb 24 '18 at 20:03
  • I noticed the uses dont mention the src folder that contains those files. added it and have this: use PHPMailer\PHPMailer\src\PHPMailer; use PHPMailer\PHPMailer\src\Exception; – munson Feb 24 '18 at 20:18
  • That’s wrong. `use` statements are not file paths; they are name aliases. – Synchro Feb 24 '18 at 20:20
  • I still get the error with: use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; – munson Feb 24 '18 at 20:23