Skip to content

Commit

Permalink
Email\Transport class
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Aug 24, 2024
1 parent 2b73a54 commit 6a6de04
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 45 deletions.
21 changes: 5 additions & 16 deletions src/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Email
protected Address|null $replyTo = null;
protected string $subject;
protected array $to;
protected array|null $transport;
protected Transport $transport;

/**
* Email constructor
Expand All @@ -58,7 +58,6 @@ public function __construct(array $props = [], bool $debug = false)
$props['body'] = ['text' => $props['body']];
}


$this->attachments = Attachment::factory($props['attachments'] ?? []);
$this->bcc = Address::factory($props['bcc'] ?? [], multiple: true);
$this->beforeSend = $props['beforeSend'] ?? null;
Expand All @@ -67,7 +66,7 @@ public function __construct(array $props = [], bool $debug = false)
$this->from = Address::factory([$props['from'] => $props['fromName'] ?? null]);
$this->subject = $props['subject'];
$this->to = Address::factory($props['to'], multiple: true);
$this->transport = $props['transport'] ?? null;
$this->transport = new Transport(...$props['transport'] ?? []);

if ($replyTo = $props['replyTo'] ?? null) {
$this->replyTo = Address::factory([$replyTo => $props['replyToName'] ?? null]);
Expand Down Expand Up @@ -126,16 +125,6 @@ public function cc(): array
return Address::resolve($this->cc);
}

/**
* Returns default transport settings
*/
protected function defaultTransport(): array
{
return [
'type' => 'mail'
];
}

/**
* Returns the "from" email address
*/
Expand Down Expand Up @@ -211,9 +200,9 @@ public function to(): array
/**
* Returns the email transports settings
*/
public function transport(): array
public function transport(): Transport
{
return $this->transport ?? $this->defaultTransport();
return $this->transport;
}

/**
Expand All @@ -235,7 +224,7 @@ public function toArray(): array
'replyToName' => $this->replyToName(),
'subject' => $this->subject(),
'to' => $this->to(),
'transport' => $this->transport()
'transport' => $this->transport()->toArray()
];
}
}
32 changes: 7 additions & 25 deletions src/Email/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,14 @@ public function send(bool $debug = false): bool
}

// smtp transport settings
if (($this->transport()['type'] ?? 'mail') === 'smtp') {
if ($this->transport()->type() === 'smtp') {
$mailer->isSMTP();
$mailer->Host = $this->transport()['host'] ?? null;
$mailer->SMTPAuth = $this->transport()['auth'] ?? false;
$mailer->Username = $this->transport()['username'] ?? null;
$mailer->Password = $this->transport()['password'] ?? null;
$mailer->SMTPSecure = $this->transport()['security'] ?? 'ssl';
$mailer->Port = $this->transport()['port'] ?? null;

if ($mailer->SMTPSecure === true) {
switch ($mailer->Port) {
case null:
case 587:
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
break;
case 465:
$mailer->SMTPSecure = 'ssl';
break;
default:
throw new InvalidArgumentException(
'Could not automatically detect the "security" protocol from the ' .
'"port" option, please set it explicitly to "tls" or "ssl".'
);
}
}
$mailer->Host = $this->transport()->host();
$mailer->SMTPAuth = $this->transport()->auth();
$mailer->Username = $this->transport()->username();
$mailer->Password = $this->transport()->password();
$mailer->SMTPSecure = $this->transport()->security();
$mailer->Port = $this->transport()->port();
}

// accessible phpMailer instance
Expand Down
104 changes: 104 additions & 0 deletions src/Email/Transport.php
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;
}
}
24 changes: 21 additions & 3 deletions tests/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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());

Expand All @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion tests/Email/PHPMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testSMTPTransportDefaults()
$phpunit->assertNull($mailer->Username);
$phpunit->assertNull($mailer->Password);
$phpunit->assertSame('ssl', $mailer->SMTPSecure);
$phpunit->assertNull($mailer->Port);
$phpunit->assertSame(465, $mailer->Port);

$beforeSend = true;
}
Expand Down

0 comments on commit 6a6de04

Please sign in to comment.