init
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestModuleEmail\Model\Transport;
|
||||
|
||||
use Laminas\Mail;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
|
||||
class File implements \Laminas\Mail\Transport\TransportInterface
|
||||
{
|
||||
private const CONFIG_FILE = 'mail-transport-config.json';
|
||||
|
||||
/**
|
||||
* @var Filesystem
|
||||
*/
|
||||
private Filesystem $filesystem;
|
||||
|
||||
/**
|
||||
* @var Json
|
||||
*/
|
||||
private Json $json;
|
||||
|
||||
/**
|
||||
* @param Filesystem $filesystem
|
||||
* @param Json $json
|
||||
*/
|
||||
public function __construct(
|
||||
Filesystem $filesystem,
|
||||
Json $json
|
||||
) {
|
||||
$this->filesystem = $filesystem;
|
||||
$this->json = $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function send(Mail\Message $message)
|
||||
{
|
||||
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
|
||||
$config = $this->json->unserialize($directory->readFile(self::CONFIG_FILE));
|
||||
$directory = $this->filesystem->getDirectoryWrite($config['directory']);
|
||||
$mail = $message->toString();
|
||||
foreach ($message->getTo() as $address) {
|
||||
$index = 1;
|
||||
$filename = preg_replace('/[^a-z0-9_]/', '__', strtolower($address->getEmail()));
|
||||
$basePath = $config['path']. DIRECTORY_SEPARATOR . $filename;
|
||||
$path = $basePath . '.eml';
|
||||
while ($directory->isExist($path)) {
|
||||
$path = $basePath . '_' . ($index++) . '.eml';
|
||||
}
|
||||
$directory->writeFile($path, $mail);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds whether "file" mail transport is enabled
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Magento\Framework\Exception\FileSystemException
|
||||
*/
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
|
||||
return $directory->isExist(self::CONFIG_FILE);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestModuleEmail\Plugin;
|
||||
|
||||
use Laminas\Mail\Transport\TransportInterface;
|
||||
use Magento\Email\Model\Transport;
|
||||
use Magento\TestModuleEmail\Model\Transport\File;
|
||||
|
||||
class MockMailTransporter
|
||||
{
|
||||
/**
|
||||
* @var File
|
||||
*/
|
||||
private File $file;
|
||||
|
||||
/**
|
||||
* @param File $file
|
||||
*/
|
||||
public function __construct(
|
||||
File $file
|
||||
) {
|
||||
$this->file = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transport $subject
|
||||
* @param \Closure $proceed
|
||||
* @return TransportInterface
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
public function aroundGetTransport(
|
||||
Transport $subject,
|
||||
\Closure $proceed
|
||||
): TransportInterface {
|
||||
return $this->file->isEnabled() ? $this->file : $proceed();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
|
||||
<type name="Magento\Email\Model\Transport">
|
||||
<plugin name="mock_mail_transporter" type="Magento\TestModuleEmail\Plugin\MockMailTransporter"/>
|
||||
</type>
|
||||
</config>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
|
||||
<module name="Magento_TestModuleEmail" />
|
||||
</config>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\Framework\Component\ComponentRegistrar;
|
||||
|
||||
$registrar = new ComponentRegistrar();
|
||||
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Magento_TestModuleEmail') === null) {
|
||||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestModuleEmail', __DIR__);
|
||||
}
|
||||
Reference in New Issue
Block a user