猿问

SwiftMailer 测试用户名和密码

我真的不知道这是否是关于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());

    }

}


宝慕林4294392
浏览 229回答 1
1回答

慕尼黑的夜晚无繁华

我在 Symfony 4.4 上使用SwiftMailer 6测试了与 Gmail 的连接,下面的代码(与您的类似)对我来说按预期工作。因此,如果您想检查是否在没有消息发送的情况下建立了连接,请使用下一个代码:public function checkConnection(){&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; // Create the Transport&nbsp; &nbsp; &nbsp; &nbsp; $transport = (new \Swift_SmtpTransport(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'smtp.gmail.com',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '587',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'tls'&nbsp; &nbsp; &nbsp; &nbsp; ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setUsername('your.username@gmail.com')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setPassword('yourSuperPassword');&nbsp; &nbsp; &nbsp; &nbsp; // Create the Mailer using your created Transport&nbsp; &nbsp; &nbsp; &nbsp; $mailer = new \Swift_Mailer($transport);&nbsp; &nbsp; &nbsp; &nbsp; $mailer->getTransport()->start();&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; } catch (\Swift_TransportException $e) {&nbsp; &nbsp; &nbsp; &nbsp; return $e->getMessage();&nbsp; &nbsp; } catch (\Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; return $e->getMessage();&nbsp; &nbsp; }}否则,它会抛出异常: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 at535 5.7.8&nbsp; https://support.google.com/mail/?p=BadCredentials z7sm3020898ljj.98 - gsmtp".&nbsp;Authenticator PLAIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8&nbsp; https://support.google.com/mail/?p=BadCredentials z7sm3020898ljj.98 - gsmtp".&nbsp;Authenticator XOAUTH2 returned Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8&nbsp; https://support.google.com/mail/?p=BadCredentials z7sm3020898ljj.98 - gsmtp".
随时随地看视频慕课网APP
我要回答