Skip to content

[Mailer] Delivery randomly fails with Google SMTP Relay #32148

Closed
@bpolaszek

Description

@bpolaszek

Hello there,

TL;DR:

Using Google SMTP Relay with the new Mailer Component works in a random manner.


Background

I'm working on a new project for which I chose the new Mailer component over Swift Mailer with the same configuration of what I'm used to (i.e. using Google SMTP Relay).

This service accessible from Google Apps allows using Google's SMTP servers without authentication, on an IP whitelist basis.

The Google SMTP Relay DSN is the following one, and doesn't require any credentials:

smtp://smtp-relay.gmail.com:587?encryption=&auth_mode=

Issue

I've been using it with SwiftMailer for years without any issue, but when using Symfony Mailer, it is likely to result in the following exception:

{"exception":"[object] (Symfony\Component\Mailer\Exception\TransportException(code: 0): Expected response code "250" but got an empty response. at ~/builds/prod/releases/4/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php:236)","command":"app:test","message":"Expected response code "250" but got an empty response."}

The likely is the problem, because that exception doesn't occur every time.

When adding a dump($response); after this line:

$response = $this->getFullResponse();

There's a 3/5 chance the dumped response will be

"421 4.7.0 Try again later, closing connection. (EHLO) x12sm193387wrq.21 - gsmtp\r\n"
""

over

"""
250-smtp-relay.gmail.com at your service, [92.154.***.***]\r\n
250-SIZE 157286400\r\n
250-8BITMIME\r\n
250-STARTTLS\r\n
250-ENHANCEDSTATUSCODES\r\n
250-PIPELINING\r\n
250-CHUNKING\r\n
250 SMTPUTF8\r\n
"""
"250 2.1.0 OK f50sm190172eda.33 - gsmtp\r\n"
"250 2.1.5 OK f50sm190172eda.33 - gsmtp\r\n"
"354  Go ahead f50sm190172eda.33 - gsmtp\r\n"
"250 2.0.0 OK  1561367145 f50sm190172eda.33 - gsmtp\r\n"

You'll probably tell me the problem comes from Google, since it's a 421 response code from their servers. The problem is that the issue just never occurs with SwiftMailer. Does the new component does some more things (like some sort of handshaking or whatever - I have no knowledge of how this works) Google SMTP servers can't interpret properly?

How to reproduce

When I run the following command several times:

# .env
###> symfony/mailer ###
MAILER_DSN=smtp://smtp-relay.gmail.com:587?encryption=&auth_mode=
###< symfony/mailer ###
###> symfony/swiftmailer-bundle ###
MAILER_URL=smtp://smtp-relay.gmail.com:587?encryption=&auth_mode=
###< symfony/swiftmailer-bundle ###
namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface as SymfonyMailer;
use Symfony\Component\Mime\Email as SymfonyMailerEmail;
use Symfony\Component\Mime\Part\TextPart;

class TestCommand extends Command
{
    protected static $defaultName = 'app:test';

    /**
     * @var SymfonyMailer
     */
    private $symfonyMailer;
    
    /**
     * @var \Swift_Mailer
     */
    private $swiftMailer;

    public function __construct(SymfonyMailer $symfonyMailer, \Swift_Mailer $swiftMailer)
    {
        parent::__construct();
        $this->symfonyMailer = $symfonyMailer;
        $this->swiftMailer = $swiftMailer;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {

        $io = new SymfonyStyle($input, $output);

        try {
            $io->comment('Sending through Symfony Mailer...');
            $message = new SymfonyMailerEmail();
            $message->setBody(new TextPart(\date(('Y-m-d H:i:s'))));
            $message->subject('Symfony Mailer');
            $message->addFrom('helloworld@mycustomdomain.com');
            $message->addTo('me@mycustomdomain.com');
            $this->symfonyMailer->send($message);
            $io->success('Done.');
        } catch (TransportExceptionInterface $e) {
            $io->error($e->getMessage());
        }

        try {
            $io->comment('Sending throught Swift Mailer...');
            $message = new \Swift_Message('Swift Mailer', \date(('Y-m-d H:i:s')));
            $message->addFrom('helloworld@mycustomdomain.com');
            $message->addTo('me@mycustomdomain.com');
            $this->swiftMailer->send($message);
            $io->success('Done.');
        } catch (\Exception $e) {
            $io->error($e->getMessage());
        }
    }
}

SwiftMailer always succeeds, while the Mailer component succeeds sometimes.

Thank you,
Ben

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions