나는 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를 사용하는 방법을 찾는 데 도움이 시겠어요?
Google 계정에서 "보안 수준이 낮은 앱 허용 : 사용 안함"설정을 확인해보십시오. PHP 코드로 Gmail을 사용하려면이 기능을 켜야 할 수도 있습니다. 이것은 일반적인 웹 앱이 덜 안전하다고 간주되기 때문입니다. –