mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 23:16:03 +00:00
mail sender params
This commit is contained in:
@@ -104,17 +104,19 @@ class EmailSender
|
||||
/**
|
||||
* With parameters.
|
||||
*
|
||||
* Available parameters: fromAddress, fromName, replyToAddress, replyToName.
|
||||
* @param SenderParams|array $params
|
||||
*/
|
||||
public function withParams(array $params = []): Sender
|
||||
public function withParams($params): Sender
|
||||
{
|
||||
return $this->createSender()->withParams($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* With specific SMTP parameters.
|
||||
*
|
||||
* @param SmtpParams|array $params
|
||||
*/
|
||||
public function withSmtpParams(array $params = []): Sender
|
||||
public function withSmtpParams($params): Sender
|
||||
{
|
||||
return $this->createSender()->withSmtpParams($params);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ use Espo\Core\{
|
||||
};
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Sends emails. Builds parameters for sending. Should not be used directly.
|
||||
@@ -139,10 +140,17 @@ class Sender
|
||||
/**
|
||||
* With parameters.
|
||||
*
|
||||
* Available parameters: fromAddress, fromName, replyToAddress, replyToName.
|
||||
* @param SenderParams|array $params
|
||||
*/
|
||||
public function withParams(array $params): self
|
||||
public function withParams($params): self
|
||||
{
|
||||
if ($params instanceof SenderParams) {
|
||||
$params = $params->toArray();
|
||||
}
|
||||
else if (!is_array($params)) {
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
|
||||
$paramList = [
|
||||
'fromAddress',
|
||||
'fromName',
|
||||
@@ -150,7 +158,7 @@ class Sender
|
||||
'replyToName',
|
||||
];
|
||||
|
||||
foreach ($params as $key => $value) {
|
||||
foreach (array_keys($params) as $key) {
|
||||
if (!in_array($key, $paramList)) {
|
||||
unset($params[$key]);
|
||||
}
|
||||
@@ -163,9 +171,18 @@ class Sender
|
||||
|
||||
/**
|
||||
* With specific SMTP parameters.
|
||||
*
|
||||
* @param SmtpParams|array $params
|
||||
*/
|
||||
public function withSmtpParams(array $params): self
|
||||
public function withSmtpParams($params): self
|
||||
{
|
||||
if ($params instanceof SmtpParams) {
|
||||
$params = $params->toArray();
|
||||
}
|
||||
else if (!is_array($params)) {
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
|
||||
return $this->useSmtp($params);
|
||||
}
|
||||
|
||||
|
||||
135
application/Espo/Core/Mail/SenderParams.php
Normal file
135
application/Espo/Core/Mail/SenderParams.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Mail;
|
||||
|
||||
class SenderParams
|
||||
{
|
||||
private $fromAddress = null;
|
||||
|
||||
private $fromName = null;
|
||||
|
||||
private $replyToAddress = null;
|
||||
|
||||
private $replyToName = null;
|
||||
|
||||
private $paramList = [
|
||||
'fromAddress',
|
||||
'fromName',
|
||||
'replyToAddress',
|
||||
'replyToName',
|
||||
];
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
$params = [];
|
||||
|
||||
foreach ($this->paramList as $name) {
|
||||
if ($this->$name !== null) {
|
||||
$params[$name] = $this->$name;
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public static function fromArray(array $params): self
|
||||
{
|
||||
$obj = new self();
|
||||
|
||||
foreach ($obj->paramList as $name) {
|
||||
if (array_key_exists($name, $params)) {
|
||||
$obj->$name = $params[$name];
|
||||
}
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function getFromAddress(): ?string
|
||||
{
|
||||
return $this->fromAddress;
|
||||
}
|
||||
|
||||
public function getFromName(): ?string
|
||||
{
|
||||
return $this->fromName;
|
||||
}
|
||||
|
||||
public function getReplyToAddress(): ?string
|
||||
{
|
||||
return $this->replyToAddress;
|
||||
}
|
||||
|
||||
public function getReplyToName(): ?string
|
||||
{
|
||||
return $this->replyToName;
|
||||
}
|
||||
|
||||
public function withFromAddress(?string $fromAddress): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->fromAddress = $fromAddress;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withFromName(?string $fromName): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->fromName = $fromName;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withReplyToAddress(?string $replyToAddress): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->replyToAddress = $replyToAddress;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withReplyToName(?string $replyToName): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->replyToName = $replyToName;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
262
application/Espo/Core/Mail/SmtpParams.php
Normal file
262
application/Espo/Core/Mail/SmtpParams.php
Normal file
@@ -0,0 +1,262 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Mail;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class SmtpParams
|
||||
{
|
||||
private $server = null;
|
||||
|
||||
private $port = null;
|
||||
|
||||
private $fromAddress = null;
|
||||
|
||||
private $fromName = null;
|
||||
|
||||
private $connectionOptions = null;
|
||||
|
||||
private $auth = false;
|
||||
|
||||
private $authMechanism = null;
|
||||
|
||||
private $authClassName = null;
|
||||
|
||||
private $username = null;
|
||||
|
||||
private $password = null;
|
||||
|
||||
private $security = null;
|
||||
|
||||
private $paramList = [
|
||||
'server',
|
||||
'port',
|
||||
'fromAddress',
|
||||
'fromName',
|
||||
'connectionOptions',
|
||||
'auth',
|
||||
'authMechanism',
|
||||
'authClassName',
|
||||
'username',
|
||||
'password',
|
||||
'security',
|
||||
];
|
||||
|
||||
public function __construct(string $server, int $port)
|
||||
{
|
||||
$this->server = $server;
|
||||
$this->port = $port;
|
||||
}
|
||||
|
||||
public static function create(string $server, int $port): self
|
||||
{
|
||||
return new self($server, $port);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
$params = [];
|
||||
|
||||
foreach ($this->paramList as $name) {
|
||||
if ($this->$name !== null) {
|
||||
$params[$name] = $this->$name;
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public static function fromArray(array $params): self
|
||||
{
|
||||
$server = $params['server'] ?? null;
|
||||
$port = $params['port'] ?? null;
|
||||
$auth = $params['auth'] ?? false;
|
||||
|
||||
if ($server === null) {
|
||||
throw new RuntimeException("Empty server.");
|
||||
}
|
||||
|
||||
if ($port === null) {
|
||||
throw new RuntimeException("Empty port.");
|
||||
}
|
||||
|
||||
$obj = new self($server, $port);
|
||||
|
||||
$obj->auth = $auth;
|
||||
|
||||
foreach ($obj->paramList as $name) {
|
||||
if ($obj->$name !== null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (array_key_exists($name, $params)) {
|
||||
$obj->$name = $params[$name];
|
||||
}
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function getServer(): string
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
public function getFromAddress(): ?string
|
||||
{
|
||||
return $this->fromAddress;
|
||||
}
|
||||
|
||||
public function getFromName(): ?string
|
||||
{
|
||||
return $this->fromName;
|
||||
}
|
||||
|
||||
public function getConnectionOptions(): ?array
|
||||
{
|
||||
return $this->connectionOptions;
|
||||
}
|
||||
|
||||
public function useAuth(): bool
|
||||
{
|
||||
return $this->auth;
|
||||
}
|
||||
|
||||
public function getAuthMechanism(): ?string
|
||||
{
|
||||
return $this->authMechanism;
|
||||
}
|
||||
|
||||
public function getAuthClassName(): ?string
|
||||
{
|
||||
return $this->authClassName;
|
||||
}
|
||||
|
||||
public function getUsername(): ?string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function getSecurity(): ?string
|
||||
{
|
||||
return $this->security;
|
||||
}
|
||||
|
||||
public function withFromAddress(?string $fromAddress): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->fromAddress = $fromAddress;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withFromName(?string $fromName): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->fromName = $fromName;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withConnectionOptions(?array $connectionOptions): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->connectionOptions = $connectionOptions;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withAuth(bool $auth = true): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->auth = $auth;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withAuthMechanism(?string $authMechanism): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->authMechanism = $authMechanism;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withAuthClassName(?string $authClassName): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->authClassName = $authClassName;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withUsername(?string $username): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->username = $username;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withPassword(?string $password): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->password = $password;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withSecurity(?string $security): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->security = $security;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
72
tests/unit/Espo/Core/Mail/SenderParamsTest.php
Normal file
72
tests/unit/Espo/Core/Mail/SenderParamsTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\Espo\Core\Mail;
|
||||
|
||||
use Espo\Core\Mail\SenderParams;
|
||||
|
||||
class SenderParamsTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testFromArray1(): void
|
||||
{
|
||||
$array = [
|
||||
'fromAddress' => 'test@test',
|
||||
'fromName' => 'name',
|
||||
'replyToAddress' => 'reply@test',
|
||||
'replyToName' => 'reply',
|
||||
];
|
||||
|
||||
$this->assertEquals($array, SenderParams::fromArray($array)->toArray());
|
||||
}
|
||||
|
||||
public function testFromArray2(): void
|
||||
{
|
||||
$array = [
|
||||
'fromAddress' => 'test@test',
|
||||
'fromName' => 'name',
|
||||
];
|
||||
|
||||
$this->assertEquals($array, SenderParams::fromArray($array)->toArray());
|
||||
}
|
||||
|
||||
public function testBuilding(): void
|
||||
{
|
||||
$params = SenderParams::create()
|
||||
->withFromAddress('test@test')
|
||||
->withFromName('name')
|
||||
->withReplyToAddress('r@test')
|
||||
->withReplyToName('r');
|
||||
|
||||
$this->assertEquals('test@test', $params->getFromAddress());
|
||||
$this->assertEquals('name', $params->getFromName());
|
||||
|
||||
$this->assertEquals('r@test', $params->getReplyToAddress());
|
||||
$this->assertEquals('r', $params->getReplyToName());
|
||||
}
|
||||
}
|
||||
93
tests/unit/Espo/Core/Mail/SmtpParamsTest.php
Normal file
93
tests/unit/Espo/Core/Mail/SmtpParamsTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\Espo\Core\Mail;
|
||||
|
||||
use Espo\Core\Mail\SmtpParams;
|
||||
|
||||
class SmtpParamsTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testFromArray1(): void
|
||||
{
|
||||
$array = [
|
||||
'server' => 'localhost',
|
||||
'port' => 587,
|
||||
'fromAddress' => 'test@test',
|
||||
'fromName' => 'name',
|
||||
'auth' => false,
|
||||
];
|
||||
|
||||
$this->assertEquals($array, SmtpParams::fromArray($array)->toArray());
|
||||
}
|
||||
|
||||
public function testFromArray2(): void
|
||||
{
|
||||
$array = [
|
||||
'server' => 'localhost',
|
||||
'port' => 587,
|
||||
'fromAddress' => 'test@test',
|
||||
'fromName' => 'name',
|
||||
'auth' => true,
|
||||
'connectionOptions' => ['test' => 'test'],
|
||||
'authMechanism' => 'login',
|
||||
'authClassName' => 'Test',
|
||||
'username' => 'tester',
|
||||
'password' => 'password',
|
||||
'security' => 'ssl',
|
||||
];
|
||||
|
||||
$this->assertEquals($array, SmtpParams::fromArray($array)->toArray());
|
||||
}
|
||||
|
||||
public function testBuilding(): void
|
||||
{
|
||||
$params = SmtpParams::create('localhost', 587)
|
||||
->withFromAddress('test@test')
|
||||
->withFromName('name')
|
||||
->withAuth()
|
||||
->withUsername('tester')
|
||||
->withPassword('test')
|
||||
->withAuthMechanism('login')
|
||||
->withAuthClassName('Test')
|
||||
->withConnectionOptions(['test' => 'test']);
|
||||
|
||||
$this->assertEquals('localhost', $params->getServer());
|
||||
$this->assertEquals(587, $params->getPort());
|
||||
|
||||
$this->assertEquals('test@test', $params->getFromAddress());
|
||||
$this->assertEquals('name', $params->getFromName());
|
||||
|
||||
$this->assertEquals(true, $params->useAuth());
|
||||
|
||||
$this->assertEquals('tester', $params->getUsername());
|
||||
$this->assertEquals('test', $params->getPassword());
|
||||
$this->assertEquals(['test' => 'test'], $params->getConnectionOptions());
|
||||
$this->assertEquals('Test', $params->getAuthClassName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user