Skip to content

Commit 16e2c48

Browse files
authored
Merge pull request #34 from txthinking/modernize
Some maintenance
2 parents 98f8a57 + bdcfcdd commit 16e2c48

File tree

11 files changed

+285
-113
lines changed

11 files changed

+285
-113
lines changed

.github/workflows/php-tests.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: PHP Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
name: PHP ${{ matrix.php-version }}
10+
runs-on: ubuntu-latest
11+
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
12+
13+
strategy:
14+
matrix:
15+
php-version: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
16+
max-parallel: 1
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ matrix.php-version }}
25+
26+
- name: Install Dependencies
27+
run: composer install
28+
29+
- name: Run Tests
30+
run: ./vendor/bin/phpunit --configuration phpunit.xml.dist --verbose

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ composer.lock
33
.idea/
44
docs/
55
phpunit.xml
6+
.phpunit.result.cache

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=5.3.2",
21+
"php": ">=5.4",
2222
"psr/log": "~1.0"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "~5.0",
25+
"phpunit/phpunit": "~8.5",
2626
"monolog/monolog": "~1.13"
2727
},
2828
"autoload": {
@@ -31,8 +31,8 @@
3131
}
3232
},
3333
"autoload-dev": {
34-
"classmap": [
35-
"tests/TestCase.php"
36-
]
34+
"psr-4": {
35+
"Tx\\Mailer\\Tests\\": "tests/"
36+
}
3737
}
3838
}

phpunit.xml.dist

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd"
55
backupGlobals="false"
66
backupStaticAttributes="false"
77
bootstrap="./vendor/autoload.php"
@@ -18,16 +18,16 @@
1818
</testsuite>
1919
</testsuites>
2020
<filter>
21-
<blacklist>
22-
<directory suffix=".php">vendor</directory>
23-
</blacklist>
21+
<whitelist>
22+
<exclude>
23+
<directory suffix=".php">vendor</directory>
24+
</exclude>
25+
</whitelist>
2426
</filter>
2527
<logging>
26-
<log
27-
type="coverage-html"
28-
target="docs/report"
29-
lowUpperBound="35"
30-
highLowerBound="70"
31-
/>
28+
<log type="coverage-html"
29+
target="docs/report"
30+
lowUpperBound="35"
31+
highLowerBound="70"/>
3232
</logging>
3333
</phpunit>

src/Mailer/SMTP.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,16 @@ public function __construct(LoggerInterface $logger=null)
108108
* set server and port
109109
* @param string $host server
110110
* @param int $port port
111-
* @param string $secure ssl tls tlsv1.0 tlsv1.1 tlsv1.2
111+
* @param string $secure ssl tls
112+
* @param bool $allowInsecure skip certificate verification?
112113
* @return $this
113114
*/
114115
public function setServer($host, $port, $secure=null, $allowInsecure=null)
115116
{
116117
$this->host = $host;
117118
$this->port = $port;
118119
$this->secure = $secure;
119-
$this->allowInsecure = $allowInsecure;
120+
$this->allowInsecure = (bool) $allowInsecure;
120121
if(!$this->ehlo) $this->ehlo = $host;
121122
$this->logger && $this->logger->debug("Set: the server");
122123
return $this;
@@ -204,7 +205,7 @@ protected function connect()
204205
$this->logger && $this->logger->debug("Connecting to {$this->host} at {$this->port}");
205206
$host = ($this->secure == 'ssl') ? 'ssl://' . $this->host : $this->host;
206207
// Create connection
207-
$context = null;
208+
$context = stream_context_create([]);
208209
if ($this->allowInsecure) {
209210
$context = stream_context_create([
210211
'ssl' => [
@@ -214,18 +215,16 @@ protected function connect()
214215
]
215216
]);
216217
}
217-
$this->smtp = stream_socket_client(
218+
$this->smtp = @stream_socket_client(
218219
$host.':'.$this->port,
219220
$error_code,
220221
$error_message,
221222
ini_get('default_socket_timeout'),
222223
STREAM_CLIENT_CONNECT,
223224
$context
224225
);
225-
//set block mode
226-
// stream_set_blocking($this->smtp, 1);
227226
if (!$this->smtp){
228-
throw new SMTPException("Could not open SMTP Port.");
227+
throw new SMTPException("Could not open SMTP Port to $host:{$this->port}");
229228
}
230229
$code = $this->getCode();
231230
if ($code !== '220'){
@@ -254,24 +253,13 @@ protected function starttls()
254253
throw new CryptoException('Crypto type expected PHP 5.6 or greater');
255254
}
256255

257-
switch ($this->secure) {
258-
case 'tlsv1.0':
259-
$crypto_type = STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT;
260-
break;
261-
case 'tlsv1.1':
262-
$crypto_type = STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
263-
break;
264-
case 'tlsv1.2':
265-
$crypto_type = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
266-
break;
267-
default:
268-
$crypto_type = STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT |
269-
STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT |
270-
STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
271-
break;
256+
if ($this->allowInsecure) {
257+
stream_context_set_option($this->smtp, 'ssl', 'verify_peer', false);
258+
stream_context_set_option($this->smtp, 'ssl', 'verify_peer_name', false);
259+
stream_context_set_option($this->smtp, 'ssl', 'allow_self_signed', true);
272260
}
273261

274-
if(!\stream_socket_enable_crypto($this->smtp, true, $crypto_type)) {
262+
if(!\stream_socket_enable_crypto($this->smtp, true, STREAM_CRYPTO_METHOD_ANY_CLIENT)) {
275263
throw new CryptoException("Start TLS failed to enable crypto");
276264
}
277265
return $this;

tests/MailerTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
<?php
22

3-
use \Tx\Mailer;
4-
use \Tx\Mailer\SMTP;
5-
use \Tx\Mailer\Message;
6-
use \Tx\Mailer\Exceptions\SMTPException;
7-
use \Monolog\Logger;
3+
namespace Tx\Mailer\Tests;
4+
5+
use Tx\Mailer;
6+
use Monolog\Logger;
87

98
class MailerTest extends TestCase
109
{
1110

12-
public function setup()
13-
{
14-
}
15-
1611
public function testSend()
1712
{
1813
$mail = new Mailer(new Logger('Mailer'));
@@ -23,7 +18,7 @@ public function testSend()
2318
->addTo(self::TO_NAME, self::TO_EMAIL)
2419
->addCc(self::CC_NAME, self::CC_EMAIL)
2520
->addBcc(self::BCC_NAME, self::BCC_EMAIL)
26-
->setSubject('Test Mailer '. time())
21+
->setSubject('Test Mailer ' . time())
2722
->setBody('Hi, boy')
2823
->addAttachment('test', __FILE__)
2924
->send();

0 commit comments

Comments
 (0)