1 回答

TA貢獻(xiàn)1864條經(jīng)驗 獲得超6個贊
我在 Symfony 4.4 上使用SwiftMailer 6測試了與 Gmail 的連接,下面的代碼(與您的類似)對我來說按預(yù)期工作。
因此,如果您想檢查是否在沒有消息發(fā)送的情況下建立了連接,請使用下一個代碼:
public function checkConnection()
{
try {
// Create the Transport
$transport = (new \Swift_SmtpTransport(
'smtp.gmail.com',
'587',
'tls'
))
->setUsername('your.username@gmail.com')
->setPassword('yourSuperPassword');
// Create the Mailer using your created Transport
$mailer = new \Swift_Mailer($transport);
$mailer->getTransport()->start();
return true;
} catch (\Swift_TransportException $e) {
return $e->getMessage();
} catch (\Exception $e) {
return $e->getMessage();
}
}
否則,它會拋出異常:
Swift_TransportException:
Failed to authenticate on SMTP server with username "your.username@gmail.com" using 3 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials z7sm3020898ljj.98 - gsmtp".
Authenticator PLAIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials z7sm3020898ljj.98 - gsmtp".
Authenticator XOAUTH2 returned Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials z7sm3020898ljj.98 - gsmtp".
- 1 回答
- 0 關(guān)注
- 254 瀏覽
添加回答
舉報