-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b73a54
commit 6a6de04
Showing
5 changed files
with
138 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Kirby\Email; | ||
|
||
use Kirby\Exception\InvalidArgumentException; | ||
use SensitiveParameter; | ||
|
||
/** | ||
* Email transports settings for mailer | ||
* | ||
* @package Kirby Email | ||
* @author Nico Hoffmann <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://opensource.org/licenses/MIT | ||
* @since 5.0.0 | ||
*/ | ||
class Transport | ||
{ | ||
public function __construct( | ||
public string $type = 'mail', | ||
public string|null $host = null, | ||
public int|null $port = null, | ||
public string|bool $security = 'ssl', | ||
public bool $auth = false, | ||
#[SensitiveParameter] | ||
public string|null $username = null, | ||
#[SensitiveParameter] | ||
public string|null $password = null, | ||
) { | ||
} | ||
|
||
public function auth(): bool | ||
{ | ||
return $this->auth; | ||
} | ||
|
||
public function host(): string|null | ||
{ | ||
return $this->host; | ||
} | ||
|
||
public function password(): string|null | ||
{ | ||
return $this->password; | ||
} | ||
|
||
public function port(): int|null | ||
{ | ||
if ($this->type() === 'mail') { | ||
return null; | ||
} | ||
|
||
// fallback to match security option | ||
return $this->port ?? match ($this->security()) { | ||
'tls' => 587, | ||
'ssl' => 465, | ||
default => null | ||
}; | ||
} | ||
|
||
public function security(): string|null | ||
{ | ||
if ($this->type() === 'mail') { | ||
return null; | ||
} | ||
|
||
// automatic mode: try to set based on port | ||
if ($this->security === true) { | ||
return match ($this->port) { | ||
null, 587 => 'tls', | ||
465 => 'ssl', | ||
default => throw new InvalidArgumentException( | ||
'Could not automatically detect the "security" protocol from the "port" option, please set it explicitly to "tls" or "ssl".' | ||
) | ||
}; | ||
} | ||
|
||
return $this->security; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return array_filter([ | ||
'type' => $this->type(), | ||
'host' => $this->host(), | ||
'port' => $this->port(), | ||
'security' => $this->security(), | ||
'auth' => $this->auth(), | ||
'username' => $this->username(), | ||
'password' => $this->password(), | ||
]); | ||
} | ||
|
||
public function type(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function username(): string|null | ||
{ | ||
return $this->username; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ public function testProperties() | |
$this->assertSame('', $email->body()->html()); | ||
$this->assertFalse($email->isHtml()); | ||
|
||
$this->assertSame(['type' => 'mail'], $email->transport()); | ||
$this->assertSame('mail', $email->transport()->type()); | ||
} | ||
|
||
public function testToArray() | ||
|
@@ -202,7 +202,16 @@ public function testBeforeSend() | |
|
||
$mailer = new Mailer(); | ||
|
||
$this->assertSame($transport, $mail->transport()); | ||
$this->assertSame([ | ||
'type' => 'smtp', | ||
'host' => 'mail.getkirby.com', | ||
'port' => 465, | ||
'security' => 'ssl', | ||
'auth' => true, | ||
'username' => '[email protected]', | ||
'password' => 'randomString', | ||
], $mail->transport()->toArray()); | ||
|
||
$this->assertSame($mail->beforeSend(), $beforeSend); | ||
$this->assertInstanceOf('Closure', $mail->beforeSend()); | ||
|
||
|
@@ -224,7 +233,16 @@ public function testBeforeSend() | |
|
||
$mailer = new Mailer(); | ||
|
||
$this->assertSame($transport, $mail->transport()); | ||
$this->assertSame([ | ||
'type' => 'smtp', | ||
'host' => 'mail.getkirby.com', | ||
'port' => 465, | ||
'security' => 'ssl', | ||
'auth' => true, | ||
'username' => '[email protected]', | ||
'password' => 'randomString', | ||
], $mail->transport()->toArray()); | ||
|
||
$this->assertSame($mail->beforeSend(), $beforeSend); | ||
$this->assertInstanceOf('Closure', $mail->beforeSend()); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters