-1

Im trying to create an email service with PHPMailer and im receiving and authentication error and the username and password are correct .

This is my code:

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = “myemail@gmail.com";
$mail->Password = “my gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘example@gmail.com', 'Nicolas El Mir');
//Set who the message is to be sent to
$mail->addAddress('example@gmail.com', 'Nicolas Mir');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'This is a plain-text message body';
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>

Thanks for your help !

user3316092
  • 43
  • 1
  • 8

3 Answers3

1

Your problem starts here:

$mail->Username = “myemail@gmail.com";
$mail->Password = “my gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘example@gmail.com', 'Nicolas El Mir');

You see those curly/smart quotes? “ ” ‘

  • “myemail...
  • “my gmail password”
  • ‘example...

They're choking your script.

$mail->Username = "myemail@gmail.com";
$mail->Password = "my gmail password";
//Set who the message is to be sent from
$mail->setFrom('example@gmail.com', 'Nicolas El Mir');

Had you error reporting set to catch/display, it would have told you about it.

I'm really hoping that is your actual code too, rather than just a bad Word Processor paste.

Plus, make sure you do have a Webserver/PHP installed and that mail is enabled on it.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Plus, pulled from this answer https://stackoverflow.com/a/16048485/ which may be the same problem for you, the port number, etc.

$mail = new PHPMailer(); // create a new object
$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 = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

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

This code above has been tested and worked for me.

It could be that you needed $mail->SMTPSecure = 'ssl';

Also make sure you don't have two step verification switched on for that account as that can cause problems also.

UPDATED

You could try changing $mail->SMTP to:

$mail->SMTPSecure = 'tls';

It's worth noting that some SMTP servers block connections. Some SMTP servers don't support SSL (or TLS) connections.

and pulled from another answer https://stackoverflow.com/a/31194724/ in that question:

The solution was that I had to remove this line:

$mail->isSMTP();
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

I think it comes from your gmail account. Your account doesn't allow less secure apps. You can turn it on through this link Google security sttings

Diamond
  • 7,428
  • 22
  • 37
-1

Try this copy libeay32.dll and ssleay32.dll to windows/system2. These two files you will find in php folder (main folder). With this you can enable php extensions. i.e curl etc. If this doesn't work, mention your error message. or check out some youtube videos! That would be suitable. Thank you.

Mat Shinoz
  • 36
  • 6
  • 2
    you sure you're in the right question? dll files??? – Funk Forty Niner Feb 03 '16 at 23:28
  • might be. those two extensions helps in enabling curl, gd and other other extensions in php. – Mat Shinoz Feb 03 '16 at 23:30
  • I didn't understood , im using mac – user3316092 Feb 03 '16 at 23:32
  • 1
    **Note to Windows Users that may pass this way** You should never copy `libeay32.dll` and `ssleay32.dll` to `windows/system32` If you need to do anything with them they should only need to be copied to the `apache/bin` folder but only if you have a poor Apache/PHP setup – RiggsFolly Feb 04 '16 at 00:17
  • my question is if you copy libeay2.dll and ssleay32.dll to apache/bin will the php.ini extensions get activated. i.e curl etc. Thank you! – Mat Shinoz Feb 04 '16 at 01:01