1 回答
TA贡献1864条经验 获得超6个赞
我在 Symfony 4.4 上使用SwiftMailer 6测试了与 Gmail 的连接,下面的代码(与您的类似)对我来说按预期工作。
因此,如果您想检查是否在没有消息发送的情况下建立了连接,请使用下一个代码:
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 关注
- 228 浏览
添加回答
举报