SmtpMailer – 503 5.5.2 Send hello first FIX
- Failips
- Member | 54
Hi, when sending mailes using SmtpMailer (from smtp.office365.com) I used to get exception “Can't start with STARTTLS – 503 5.5.2 Send hello first…”. I fixed it adding this line of code to SmtpMailer class on line 158 before setting tls in connect function.
$this->write("HELO $this->clientHost", 250);
Code now looks like this:
<?php
...
protected function connect(): void
{
$this->connection = @stream_socket_client(// @ is escalated to exception
($this->secure === 'ssl' ? 'ssl://' : '') . $this->host . ':' . $this->port,
$errno, $error, $this->timeout, STREAM_CLIENT_CONNECT, $this->context
);
if (!$this->connection) {
throw new SmtpException($error ?: error_get_last()['message'], $errno);
}
stream_set_timeout($this->connection, $this->timeout, 0);
$this->read(); // greeting
$this->write("HELO $this->clientHost", 250);
if ($this->secure === 'tls') {
$this->write('STARTTLS', 220);
if (!stream_socket_enable_crypto(
$this->connection,
true,
STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
)) {
throw new SmtpException('Unable to connect via TLS.');
}
$this->write("EHLO $this->clientHost", 250);
}
$this->write("EHLO $this->clientHost");
$ehloResponse = $this->read();
if ((int) $ehloResponse !== 250) {
$this->write("HELO $this->clientHost", 250);
}
if ($this->username != null && $this->password != null) {
$authMechanisms = [];
if (preg_match('~^250[ -]AUTH (.*)$~im', $ehloResponse, $matches)) {
$authMechanisms = explode(' ', trim($matches[1]));
}
if (in_array('PLAIN', $authMechanisms, true)) {
$credentials = $this->username . "\0" . $this->username . "\0" . $this->password;
$this->write('AUTH PLAIN ' . base64_encode($credentials), 235, 'PLAIN credentials');
} else {
$this->write('AUTH LOGIN', 334);
$this->write(base64_encode($this->username), 334, 'username');
$this->write(base64_encode($this->password), 235, 'password');
}
}
}
...
?>
- David Matějka
- Moderator | 6445
Hi,
recently there were several attempts to fix this:
Please try latest version of nette/mail (3.1.4)