2017-10-20 7 views
0

나는 Composer를 사용하여 PHPMailer를 설치했으며 Google XOAuth2 인증을 사용하기 위해 모든 작성기 의존성을 추가했습니다. 그런 다음 Gmail API를 사용하여 새로 고침 토큰을 얻으려면 자습서를 따라야합니다.
모든 것이 잘 작동해야합니다. 나는 모든 종이 작업을 올바르게했다. 권리?! 으로부터 직선xampp + PHPMailer + Gmail + XOAuth2 = SMTP 오류 : SMTP 호스트에 연결할 수 없습니다.

2017-10-20 18:01:45 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP c17sm2715728wrg.26 - gsmtp 
2017-10-20 18:01:45 CLIENT -> SERVER: EHLO localhost 
2017-10-20 18:01:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [151.61.40.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 
2017-10-20 18:01:45 CLIENT -> SERVER: STARTTLS 
2017-10-20 18:01:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 
SMTP Error: Could not connect to SMTP host. 
2017-10-20 18:01:45 CLIENT -> SERVER: QUIT 
2017-10-20 18:01:45 
2017-10-20 18:01:45 
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 


내 코드는 다음과 같습니다 최선 의도와 모든 문서에도 불구하고

, 난이 오류가

을 smtp.gmail.com으로하는 SMTP 연결을 설정할 수 아니에요 PHPMailer Gmail XOAUTH 예제.

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\OAuth; 

use League\OAuth2\Client\Provider\Google; 

date_default_timezone_set('Europe/Rome'); 

require 'lib/php/vendor/autoload.php'; 

$mail = new PHPMailer; 

$mail->isSMTP(); 
$mail->SMTPDebug = 2; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 587; 
$mail->SMTPSecure = 'tls'; 
$mail->SMTPAuth = true; 
$mail->AuthType = 'XOAUTH2'; 

$email = 'seckrit-stuff'; 
$clientId = 'seckrit-stuff'; 
$clientSecret = 'seckrit-stuff'; 
$refreshToken = 'seckrit-stuff'; 

$provider = new Google(
    [ 
     'clientId' => $clientId, 
     'clientSecret' => $clientSecret, 
    ] 
); 
$mail->setOAuth(
    new OAuth(
     [ 
      'provider' => $provider, 
      'clientId' => $clientId, 
      'clientSecret' => $clientSecret, 
      'refreshToken' => $refreshToken, 
      'userName' => $email, 
     ] 
    ) 
); 

$mail->setFrom($email, 'Matteo Bini'); 
$mail->addAddress('seckrit-stuff', 'Matteo Bini'); 
$mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test'; 
$mail->CharSet = 'utf-8'; 
$mail->msgHTML('<strong>HTML</strong> message!'); 
$mail->AltBody = 'This is a plain-text message body'; 


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



당신은 나 로컬 호스트 (XAMPP)에서 Gmail을 PHPMailer를 사용하는 방법을 찾는 데 도움이 시겠어요?

+0

Google 계정에서 "보안 수준이 낮은 앱 허용 : 사용 안함"설정을 확인해보십시오. PHP 코드로 Gmail을 사용하려면이 기능을 켜야 할 수도 있습니다. 이것은 일반적인 웹 앱이 덜 안전하다고 간주되기 때문입니다. –

답변

2

OAuth와는 아무 관련이 없습니다. TLS 레벨에서 훨씬 이전에 문제가 발생했습니다. openssl 확장자가 누락되었거나 잘못 구성되었거나 CA 인증서가 오래되었습니다.

이 문제는 오류 링크와 관련된 문제 해결 안내서에서 설명합니다. - openssl을 사용하여 확인하십시오.

+0

질문을 게시하기 전에 문제 해결 안내서를 읽었으며 xampp php.ini 파일에서도 openssl 확장을 활성화했습니다. 문제 해결 가이드 팁을 사용하여 echo (extension_loaded ('openssl')? 'SSL loaded': 'SSL not loaded')와 함께 확장 프로그램을 테스트했습니다. "\ n"; SSL이로드되었습니다. 내가 더 많이했거나 다르게해야할까요? – matteobin

+0

글쎄,하지만 가이드에서 openssl 명령 줄 예제를 사용해보십시오. 또한 SMTPDebug = 3을 시도하십시오. – Synchro

+0

나는 내 문제를 해결했다. 고맙습니다! SMTPDebug = 3을 사용했는데 오류가 무엇인지 알아 냈고이 코드 줄을 php.ini openssl.cafile = C : \ xampp \ php \ extras \ ssl \ cacert.pem에 추가했습니다. 분명히 https://curl.haxx.se/docs/caextract.html에서 cacert.pem을 다운로드 한 후 – matteobin