Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftMailer test username and password

I really don't know if this is about SwiftMailer or mailtrap.io but the problem is when I try to establish a SMTP connection to said service (mailtrap.io) I never get any errors, even if I don't use any username or password. Am I not supposed to get any authentication errors when not using any username or password? Or am I doing this the wrong way?

This is the method I use to test if the dynamic connection can be made before I store it in the .env file.

/**
 * 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());
    }
}
like image 285
Kaizokupuffball Avatar asked Oct 30 '25 22:10

Kaizokupuffball


1 Answers

I tested a connection to Gmail with SwiftMailer 6 on Symfony 4.4 and the code below (it's similar to yours) works as expected for me.

So if you want to check if the connection is established without a message sending use the next code:

public function checkConnection()
{
    try {
        // Create the Transport
        $transport = (new \Swift_SmtpTransport(
            'smtp.gmail.com',
            '587',
            'tls'
        ))
            ->setUsername('[email protected]')
            ->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();
    }
}

otherwise, it throws an exception:

Swift_TransportException:
Failed to authenticate on SMTP server with username "[email protected]" 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".
like image 83
Serhii Popov Avatar answered Nov 01 '25 12:11

Serhii Popov