Search code examples
smtpgmailphpmailer

phpmailer seems not working with gmail


when I try to send email using phpmailer with gmail stp, seems doesn't working. I have tested all solutions proposed but still this issue:

2017-01-27 19:29:33 Invalid address:  (addAnAddress Reply-To): jgh
2017-01-27 19:29:33 SERVER -> CLIENT: 220-khadija.genious.net ESMTP Exim 4.87 #1 Fri, 27 Jan 2017 19
:29:33 +0000 
                                      220-We do not authorize the use of this system to transport unsolicited
, 
                                      220 and/or bulk e-mail.
2017-01-27 19:29:33 CLIENT -> SERVER: EHLO www.mywebsite.ma
2017-01-27 19:29:33 SERVER -> CLIENT: 250-khadija.genious.net Hello khadija.genious.net [41.77.119.226
]
                                      250-SIZE 52428800
                                      250-8BITMIME
                                      250-PIPELINING
                                      250-AUTH PLAIN LOGIN
                                      250-STARTTLS
                                      250 HELP
2017-01-27 19:29:33 CLIENT -> SERVER: STARTTLS
2017-01-27 19:29:33 SERVER -> CLIENT: 220 TLS go ahead
2017-01-27 19:29:33 CLIENT -> SERVER: EHLO www.mywebsite.ma
2017-01-27 19:29:33 SERVER -> CLIENT: 250-khadija.genious.net Hello khadija.genious.net [41.77.119.226
]
                                      250-SIZE 52428800
                                      250-8BITMIME
                                      250-PIPELINING
                                      250-AUTH PLAIN LOGIN
                                      250 HELP
2017-01-27 19:29:33 CLIENT -> SERVER: AUTH LOGIN
2017-01-27 19:29:33 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-01-27 19:29:33 CLIENT -> SERVER: xxx==
2017-01-27 19:29:33 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-01-27 19:29:33 CLIENT -> SERVER: xxx
2017-01-27 19:29:35 SERVER -> CLIENT: 535 Incorrect authentication data
2017-01-27 19:29:35 SMTP ERROR: Password command failed: 535 Incorrect authentication data
2017-01-27 19:29:35 SMTP Error: Could not authenticate.
2017-01-27 19:29:35 CLIENT -> SERVER: QUIT
2017-01-27 19:29:35 SERVER -> CLIENT: 221 khadija.genious.net closing connection
2017-01-27 19:29:35 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Here is my php page:

require 'class.phpmailer.php';
require 'class.smtp.php';

header('Content-Type: application/json');

$mail = new PHPMailer;

$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "[email protected]";
$mail->Password = "XXX";

$email_recipient = '[email protected]';

$mail->setFrom($email_recipient);
$mail->addAddress($email_recipient);  
$mail->addReplyTo($_POST['email']);

$mail->isHTML(true);

$mail->Subject = 'Subject';
$mail->Body    = "Message";

if(!$mail->send()) {
    echo json_encode(array('result' => false));
} else {
    echo json_encode(array('result' => true));
}

I have already enabled lesssecureapps : https://www.google.com/settings/security/lesssecureapps

Any ideas?

Thanks


Solution

  • Your ISP is redirecting your SMTP connection to their own mail server. You are asking to connect to smtp.gmail.com, but you are ending up on khadija.genious.net, and of course your credentials do not work on there.

    I would guess you are running PHP older than 5.6; this is a good lesson in why not verifying TLS certificates is a bad idea - you have effectively given away you Gmail credentials to an MITM attacker. This is covered in the troubleshooting guide.