Skip to content

Instantly share code, notes, and snippets.

@marcguyer
Last active November 15, 2023 01:18
Show Gist options
  • Save marcguyer/559cfd4908d181cd354ef108c72df972 to your computer and use it in GitHub Desktop.
Save marcguyer/559cfd4908d181cd354ef108c72df972 to your computer and use it in GitHub Desktop.
Zend Framework V1 fix for TLS1.2 using Zend_Mail_Transport_Smtp
<?php
class My_Mail_Protocol_Smtp_Auth_Login extends My_Mail_Protocol_Smtp
{
/**
* LOGIN username
*
* @var string
*/
protected $_username;
/**
* LOGIN password
*
* @var string
*/
protected $_password;
/**
* Constructor.
*
* @param string $host (Default: 127.0.0.1)
* @param int $port (Default: null)
* @param array $config Auth-specific parameters
* @return void
*/
public function __construct($host = '127.0.0.1', $port = null, $config = null)
{
if (is_array($config)) {
if (isset($config['username'])) {
$this->_username = $config['username'];
}
if (isset($config['password'])) {
$this->_password = $config['password'];
}
}
parent::__construct($host, $port, $config);
}
/**
* Perform LOGIN authentication with supplied credentials
*
* @return void
*/
public function auth()
{
// Ensure AUTH has not already been initiated.
parent::auth();
$this->_send('AUTH LOGIN');
$this->_expect(334);
$this->_send(base64_encode($this->_username));
$this->_expect(334);
$this->_send(base64_encode($this->_password));
$this->_expect(235);
$this->_auth = true;
}
}
<?php
class My_Mail_Protocol_Smtp extends Zend_Mail_Protocol_Smtp
{
/**
* Initiate HELO/EHLO sequence and set flag to indicate valid smtp session
*
* @param string $host The client hostname or IP address (default: 127.0.0.1)
* @throws Zend_Mail_Protocol_Exception
* @return void
*/
public function helo($host = '127.0.0.1')
{
// Respect RFC 2821 and disallow HELO attempts if session is
// already initiated.
if ($this->_sess === true) {
throw new Zend_Mail_Protocol_Exception(
'Cannot issue HELO to existing session'
);
}
// Validate client hostname
if (!$this->_validHost->isValid($host)) {
throw new Zend_Mail_Protocol_Exception(
join(', ', $this->_validHost->getMessages())
);
}
// Initiate helo sequence
$this->_expect(220, 300); // Timeout set for 5 minutes as per RFC 2821 4.5.3.2
$this->_ehlo($host);
// If a TLS session is required, commence negotiation
if ($this->_secure == 'tls') {
$this->_send('STARTTLS');
$this->_expect(220, 180);
if (
true !== $enableCrypto = stream_socket_enable_crypto(
$this->_socket,
true,
// here's our fix TLS 1.2
STREAM_CRYPTO_METHOD_ANY_CLIENT
)
) {
throw new Zend_Mail_Protocol_Exception(
'Unable to connect via TLS'
);
}
$this->_ehlo($host);
}
$this->_startSession();
$this->auth();
}
}
<?php
$config = [
'auth' => 'login',
'username' => 'your username',
'password' => 'your password',
'ssl' => 'tls'
'port' => 'smtp port',
];
$transport = new Zend_Mail_Transport_Smtp(
'smtp.example.com'
$config
);
$conn = new My_Mail_Protocol_Smtp_Auth_Login(
'smtp.example.com',
'your smtp port',
$config
);
$conn->connect();
$conn->helo('example.com');
$transport->setConnection($conn);
// instanciate Zend_Mail and config normally
$mail->send($transport);
@vkost
Copy link

vkost commented Feb 22, 2022

I've just fixed one ancient Magento 1.9 install using this - THANK YOU SO MUCH!

@Niels-Snakenborg
Copy link

Thankyou! works perfectly.

@Paulsky
Copy link

Paulsky commented Mar 7, 2022

Thank you very much 🙏

@snow73
Copy link

snow73 commented May 10, 2023

Thank you!

@tasha1d4
Copy link

Thank you! It works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment