2016-07-29 35 views
3

Ich bin neu in PHP und versuche, eine Website mit Registrierungsformular zu erstellen. Benutzer sollten eine E-Mail mit einem Bestätigungslink erhalten (da ich einen kostenlosen Hosting-Server habe, versuche ich den Google Mail-Server zu verwenden). Die Sache ist, ich kämpfe mit dem E-Mail-PHP-Code. Ich versuche PHPMailer Funktionen wie folgt zu verwenden:Ich kann keine E-Mails mit PHP über Google Mail senden - Autorisierungsfehler

<?php 
 
/** 
 
* This example shows settings to use when sending via Google's Gmail servers. 
 
*/ 
 

 
//SMTP needs accurate times, and the PHP time zone MUST be set 
 
//This should be done in your php.ini, but this is how to do it if you don't have access to that 
 
date_default_timezone_set('Etc/UTC'); 
 
require 'PHPMailer/PHPMailerAutoload.php'; 
 

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

 
//Tell PHPMailer to use SMTP 
 
$mail->isSMTP(); 
 

 
//Enable SMTP debugging 
 
// 0 = off (for production use) 
 
// 1 = client messages 
 
// 2 = client and server messages 
 
$mail->SMTPDebug = 2; 
 

 
//Ask for HTML-friendly debug output 
 
$mail->Debugoutput = 'html'; 
 

 
//Set the hostname of the mail server 
 
$mail->Host = 'smtp.gmail.com'; 
 
// use 
 
// $mail->Host = gethostbyname('smtp.gmail.com'); 
 
// if your network does not support SMTP over IPv6 
 

 
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
 
$mail->Port = 587; 
 
//Set the encryption system to use - ssl (deprecated) or tls 
 
$mail->SMTPSecure = 'tls'; 
 

 
//Whether to use SMTP authentication 
 
$mail->SMTPAuth = true; 
 

 
//Username to use for SMTP authentication - use full email address for gmail 
 
$mail->Username = "[email protected]"; 
 

 
//Password to use for SMTP authentication 
 
$mail->Password = "MYPASSWORD"; 
 

 
//Set who the message is to be sent from 
 
$mail->setFrom('[email protected]', 'Name S'); 
 

 
//Set an alternative reply-to address 
 
$mail->addReplyTo('[email protected]', 'Name S'); 
 

 
//Set who the message is to be sent to 
 
$mail->addAddress('[email protected]', 'John Doe'); 
 

 
//Set the subject line 
 
$mail->Subject = 'PHPMailer GMail SMTP test'; 
 

 
//Read an HTML message body from an external file, convert referenced images to embedded, 
 
//convert HTML into a basic plain-text alternative body 
 
$mail->msgHTML("HELLo"); 
 

 
//Replace the plain text body with one created manually 
 
//$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!"; 
 
} 
 
?>

Ich habe versucht, beide Ports 587 und 465. Auch sowohl SSL- und TLS versucht. Jedes Mal, wenn ich versuche, den Code, den ich die folgende Fehlermeldung auszuführen:

SERVER -> CLIENT: 220 smtp.gmail.com ESMTP gw4sm17090153wjc.45 - gsmtp CLIENT -> SERVER: EHLO spec.dr-manny.co.uk SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.36]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 CLIENT -> SERVER: AUTH LOGIN SERVER -> CLIENT: 334 VXNlcm5hbWU6 CLIENT -> SERVER: bWFubnlzYWVkaUBnbWFpbC5jb20= SERVER -> CLIENT: 334 UGFzc3dvcmQ6 CLIENT -> SERVER: a2luZ29uZW1vaDk5 SERVER -> CLIENT: 534-5.7.14 Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 - gsmtp SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 - gsmtp SMTP Error: Could not authenticate. CLIENT -> SERVER: QUIT SERVER -> CLIENT: 221 2.0.0 closing connection gw4sm17090153wjc.45 - gsmtp SMTP connect() failed. Error: SMTP connect() failed.

Auch ich bekam eine E-Mail von Google Mail mit dem Betreff „Jemand hat vergessen“. Ich habe die E-Mail geöffnet und gefunden "
Hallo Manny, . Jemand hat gerade mit Ihrem Passwort versucht, sich in Ihrem Google-Konto [email protected] mit einer Anwendung wie einem E-Mail-Client oder einem Mobilgerät anzumelden." Ich habe diese E-Mail ca. 15 Minuten nach dem Ausführen der PHP-Seite erhalten.

Ich habe die "Bestätigung in zwei Schritten" deaktiviert und die Option "Weniger sichere Apps zulassen" deaktiviert. Nichts scheint zu helfen. Wer könnte mir bitte helfen?

Antwort

2

Ich hatte das gleiche Problem im vergangenen Jahr: der Code (VB.Net) war in Ordnung, also wo die Anmeldeinformationen. Das Problem war, dass Google Mail die App in einem weit entfernten Web-Server (mein Hosting) nicht mochte, wo Sie versuchen, denselben Benutzernamen zu verwenden.

ich es fest (in zwei verschiedenen Gelegenheiten) von:

Danach testen Sie Ihren Code erneut.

+0

Vielen Dank! Ich habe die zweite Möglichkeit ausprobiert. Es funktionierte! Du hast mich gerettet! Danke :) –

+0

Gern geschehen! Der schwierige Teil davon ist, dass die Fehlermeldung nicht wirklich sagt, was das Problem ist: p – Roimer

+0

Danke Mann - es hat auch für mich funktioniert –