【发布时间】:2019-12-23 12:50:49
【问题描述】:
我真的不知道这是关于 SwiftMailer 还是 mailtrap.io 但问题是当我尝试建立与所述服务的 SMTP 连接时 (mailtrap.io) 我从来没有收到任何错误,即使我没有'不要使用任何用户名或密码。不使用任何用户名或密码时,我不应该出现任何身份验证错误吗?还是我这样做是错误的?
这是我在将动态连接存储到.env文件之前用来测试是否可以建立动态连接的方法。
/**
* E-mail configuration
*
* @param Request $request
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
public function postEmailConfiguration(Request $request)
{
# Try connecting to the SMTP
try {
$encryption = $request->input('encryption');
if ($encryption == 'null') {
$encryption = NULL;
}
$transport = new Swift_SmtpTransport($request->input('host'), $request->input('port'), $encryption);
$transport->setUsername($request->input('username'));
$transport->setPassword($request->input('password'));
$mailer = new Swift_Mailer($transport);
$mailer->getTransport()->start();
# Update .env file
$this->setEnvironmentValues([
'MAIL_HOST' => $request->input('host'),
'MAIL_PORT' => $request->input('port'),
'MAIL_USERNAME' => $request->input('username'),
'MAIL_PASSWORD' => $request->input('password'),
'MAIL_ENCRYPTION' => $request->input('encryption')
]);
return redirect()->back()->with('success', 'Connection to SMTP establised and saved!');
}
# Could not connect to SMTP
catch (Swift_TransportException $e) {
return redirect()->back()->with('error', 'Could not connect to SMTP server.<br>Error: ' . $e->getMessage());
}
# Could not connect to SMTP
catch (Exception $e) {
return redirect()->back()->with('error', 'Could not connect to SMTP server.<br>Error: ' . $e->getMessage());
}
}
【问题讨论】:
-
来自 SwiftMailer SMTP_Transport 文档:
A connection to the SMTP server will be established upon the first call to send(): -
好的,所以基本上我得发个测试邮件才能知道认证是否成功?我会尝试一下,看看是否有帮助。添加它作为答案,我会检查它
-
好吧,文档说的更进一步,您可以使用
start()方法来检查身份验证。对我来说一切正常。尝试进行身份验证给了我这个:Fatal error: Uncaught Swift_TransportException: Failed to authenticate on SMTP server with username "user@example.com" using 3 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "535", with message "535 5.7.8 Error: authentication failed: Invalid user or password!".所以也许 mailtrap.io 出了点问题
标签: php laravel email swiftmailer