SwiftMailer测试用户名和密码

时间:2019-08-17 15:08:17

标签: php laravel email swiftmailer

我真的不知道这是关于SwiftMailer还是mailtrap.io的问题,但是问题是当我尝试建立与所述服务(mailtrap.io的SMTP连接时,我什么也没得到错误,即使我不使用任何用户名或密码也是如此。不使用任何用户名或密码时,我是否不应该获得任何身份验证错误?还是我做错了这个方法?

这是我用来在将动态连接存储到.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());
    }
}

1 个答案:

答案 0 :(得分:2)

我在 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".