symfony: [Mailer] Delivery randomly fails with Google SMTP Relay
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:
https://github.com/symfony/symfony/blob/aa4385dc125213342c8292c9060693fa51cbad4d/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php#L142
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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 28 (24 by maintainers)
Commits related to this issue
- minor #32894 [Mailer] Change the order of authenticators (fabpot) This PR was merged into the 4.3 branch. Discussion ---------- [Mailer] Change the order of authenticators | Q | A | --... — committed to symfony/symfony by fabpot 5 years ago
- bug #33449 Fix gmail relay (Beno!t POLASZEK) This PR was merged into the 4.3 branch. Discussion ---------- Fix gmail relay | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix... — committed to symfony/symfony by fabpot 5 years ago
- Merge branch '4.3' into 4.4 * 4.3: maintain sender/recipient name in SMTP envelopes Fix #32148 TransportException was not thrown — committed to symfony/symfony by fabpot 5 years ago
- Merge branch '4.4' * 4.4: [Validator] Deprecated CacheInterface in favor of PSR-6. Fix wrong namespace [Mailer] Fix typo [Mailer] Fix an error message maintain sender/recipient name in SMTP... — committed to symfony/symfony by nicolas-grekas 5 years ago
The issue on Stackoverflow is different and easy to fix: the problem is that the
@in the username must be encoded (can someone answers the question there, I don’t have an account on SO).@tienvx use
MAILER_DSN=smtp://tien.xuan.vo%40gmail.com:thisisyourpassword@smtp.gmail.com:465?encryption=sslif you really want to use a DSN.@LuKePicci Can you please submit your issue as a new ticket? Only few people monitor bug reports that have been closed 1.5 years ago.
My problem is because of the wrong dsn. Use your dsn and it works. Sorry guys! my bad
I’ve just spotted a difference between Swiftmailer and Symfony Mailer. It should not make any difference, but we never know with random failures. @tienvx Can you try #32894? It won’t help with the general issue as whitelisted IP does not use authenticators at all.