Skip to content

Commit cf3af43

Browse files
authored
Merge pull request #19 from joshtronic/feat_expand-tls
feat(tls): expand tls options
2 parents 648ca5a + c375f9f commit cf3af43

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Mailer/SMTP.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ public function send(Message $message)
169169
$this->connect()
170170
->ehlo();
171171

172-
if ($this->secure === 'tls'){
172+
if (substr($this->secure, 0, 3) === 'tls') {
173173
$this->starttls()
174174
->ehlo();
175175
}
176+
176177
if ($this->username !== null || $this->password !== null) {
177178
$this->authLogin();
178179
} elseif ($this->oauthToken !== null) {
@@ -224,7 +225,27 @@ protected function starttls()
224225
if ($code !== '220'){
225226
throw new CodeException('220', $code, array_pop($this->resultStack));
226227
}
227-
if(!\stream_socket_enable_crypto($this->smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
228+
229+
if ($this->secure !== 'tls' && version_compare(phpversion(), '5.6.0', '<')) {
230+
throw new CryptoException('Crypto type expected PHP 5.6 or greater');
231+
}
232+
233+
switch ($this->secure) {
234+
case 'tlsv1.0':
235+
$crypto_type = STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT;
236+
break;
237+
case 'tlsv1.1':
238+
$crypto_type = STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
239+
break;
240+
case 'tlsv1.2':
241+
$crypto_type = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
242+
break;
243+
default:
244+
$crypto_type = STREAM_CRYPTO_METHOD_TLS_CLIENT;
245+
break;
246+
}
247+
248+
if(!\stream_socket_enable_crypto($this->smtp, true, $crypto_type)) {
228249
throw new CryptoException("Start TLS failed to enable crypto");
229250
}
230251
return $this;

tests/SMTPTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ public function testTLSMessage()
8585
usleep(self::DELAY);
8686
}
8787

88+
public function testTLSv10Message()
89+
{
90+
$this->smtp->setServer(self::SERVER, self::PORT, 'tlsv1.0')
91+
->setAuth(self::USER, self::PASS);
92+
93+
$status = $this->smtp->send($this->message);
94+
$this->assertTrue($status);
95+
usleep(self::DELAY);
96+
}
97+
98+
public function testTLSv11Message()
99+
{
100+
$this->smtp->setServer(self::SERVER, self::PORT, 'tlsv1.1')
101+
->setAuth(self::USER, self::PASS);
102+
103+
$status = $this->smtp->send($this->message);
104+
$this->assertTrue($status);
105+
usleep(self::DELAY);
106+
}
107+
108+
public function testTLSv12Message()
109+
{
110+
$this->smtp->setServer(self::SERVER, self::PORT, 'tlsv1.2')
111+
->setAuth(self::USER, self::PASS);
112+
113+
$status = $this->smtp->send($this->message);
114+
$this->assertTrue($status);
115+
usleep(self::DELAY);
116+
}
117+
88118
/**
89119
* @expectedException \Tx\Mailer\Exceptions\SMTPException
90120
*/

0 commit comments

Comments
 (0)