Skip to content

Commit

Permalink
Implement SMTP(s) configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
voronkovich committed Feb 20, 2023
1 parent 53442cc commit 52aebc5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/DSNConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private function applyConfig(PHPMailer $mailer, $config)
case 'smtp':
case 'smtps':
$mailer->isSMTP();
$this->configureSMTP($mailer, $config);
break;
default:
throw new Exception(
Expand All @@ -108,4 +109,41 @@ private function applyConfig(PHPMailer $mailer, $config)

return $mailer;
}

/**
* Configure SMTP.
*
* @param PHPMailer $mailer PHPMailer instance
* @param array $config Configuration
*
* @return PHPMailer
*/
private function configureSMTP($mailer, $config)
{
$isSMTPS = 'smtps' === $config['scheme'];

if ($isSMTPS) {
$mailer->SMTPSecure = 'tls';
}

$mailer->Host = $config['host'];

if (isset($config['port'])) {
$mailer->Port = $config['port'];
} elseif ($isSMTPS) {
$mailer->Port = SMTP::DEFAULT_SECURE_PORT;
}

$mailer->SMTPAuth = isset($config['user']) || isset($config['pass']);

if (isset($config['user'])) {
$mailer->Username = $config['user'];
}

if (isset($config['pass'])) {
$mailer->Password = $config['pass'];
}

return $mailer;
}
}
7 changes: 7 additions & 0 deletions src/SMTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class SMTP
*/
const DEFAULT_PORT = 25;

/**
* The SMTPs port to use if one is not specified.
*
* @var int
*/
const DEFAULT_SECURE_PORT = 587;

/**
* The maximum line length allowed by RFC 5321 section 4.5.3.1.6,
* *excluding* a trailing CRLF break.
Expand Down
33 changes: 32 additions & 1 deletion test/PHPMailer/DSNConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use PHPMailer\PHPMailer\DSNConfigurator;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\Test\TestCase;

final class DSNConfiguratorTest extends TestCase
Expand Down Expand Up @@ -48,12 +49,42 @@ public function testConfigureSendmail()
$this->assertEquals($this->Mail->Mailer, 'sendmail');
}

public function testConfigureSmtp()
public function testConfigureSmtpWithoutAuthentication()
{
$configurator = new DSNConfigurator();

$configurator->configure($this->Mail, 'smtp://localhost');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertEquals($this->Mail->Host, 'localhost');
$this->assertFalse($this->Mail->SMTPAuth);
}

public function testConfigureSmtpWithAuthentication()
{
$configurator = new DSNConfigurator();

$configurator->configure($this->Mail, 'smtp://user:pass@remotehost');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertTrue($this->Mail->SMTPAuth);
$this->assertEquals($this->Mail->Host, 'remotehost');
$this->assertEquals($this->Mail->Username, 'user');
$this->assertEquals($this->Mail->Password, 'pass');
}

public function testConfigureSmtpsWithoutPort()
{
$configurator = new DSNConfigurator();

$configurator->configure($this->Mail, 'smtps://user:pass@remotehost');

$this->assertEquals($this->Mail->Mailer, 'smtp');
$this->assertEquals($this->Mail->SMTPSecure, 'tls');
$this->assertTrue($this->Mail->SMTPAuth);
$this->assertEquals($this->Mail->Host, 'remotehost');
$this->assertEquals($this->Mail->Username, 'user');
$this->assertEquals($this->Mail->Password, 'pass');
$this->assertEquals($this->Mail->Port, SMTP::DEFAULT_SECURE_PORT);
}
}

0 comments on commit 52aebc5

Please sign in to comment.