Skip to content

[Mailer] add File transport #31947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Added possibility to register custom transport for dsn by implementing
`Symfony\Component\Mailer\Transport\TransportFactoryInterface` and tagging with `mailer.transport_factory` tag in DI.
* Added `Symfony\Component\Mailer\Test\TransportFactoryTestCase` to ease testing custom transport factories.
* Added the File transport

4.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Tests\Transport;

use Symfony\Component\Mailer\Tests\TransportFactoryTestCase;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\FileTransport;
use Symfony\Component\Mailer\Transport\FileTransportFactory;
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;

class FileTransportFactoryTest extends TransportFactoryTestCase
{
public function getFactory(): TransportFactoryInterface
{
return new FileTransportFactory($this->getDispatcher(), $this->getClient(), $this->getLogger());
}

public function supportsProvider(): iterable
{
yield [
new Dsn('smtp', 'null', null, null, null, ['path' => sys_get_temp_dir().'/symfony/emails']),
true,
];

yield [
new Dsn('smtp', 'null'),
false,
];

yield [
new Dsn('smtp', 'example.com'),
false,
];
}

public function createProvider(): iterable
{
$path = sys_get_temp_dir().'/symfony/emails';

yield [
new Dsn('file', 'null', null, null, null, ['path' => $path]),
new FileTransport($path, $this->getDispatcher(), $this->getLogger()),
];
}

public function unsupportedSchemeProvider(): iterable
{
yield [new Dsn('foo', 'null')];
}
}
46 changes: 46 additions & 0 deletions src/Symfony/Component/Mailer/Tests/Transport/FileTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\SmtpEnvelope;
use Symfony\Component\Mailer\Transport\FileTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\RawMessage;

/**
* @group time-sensitive
*/
class FileTransportTest extends TestCase
{
public function testSend()
{
$path = sys_get_temp_dir().'/symfony/emails';
(new Filesystem())->remove($path);

$transport = new FileTransport($path);
$message = new RawMessage('');
$envelope = new SmtpEnvelope(new Address('fabien@example.com'), [new Address('helene@example.com')]);
$transport->send($message, $envelope);

$file = glob($path.'/*')[0];
/** @var SentMessage $sentMessage */
$sentMessage = unserialize(file_get_contents($file));

$this->assertInstanceOf(SentMessage::class, $sentMessage);
$this->assertEquals($envelope->getSender(), $sentMessage->getEnvelope()->getSender());
$this->assertEquals($envelope->getRecipients(), $sentMessage->getEnvelope()->getRecipients());
$this->assertEquals($message, $sentMessage->getMessage());
}
}
46 changes: 46 additions & 0 deletions src/Symfony/Component/Mailer/Transport/FileTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Transport;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* Pretends messages have been sent, but just ignores them.
*
* @author Hugo Alliaume <@kocal>
*/
final class FileTransport extends AbstractTransport
{
private $path;

public function __construct(string $path, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct($dispatcher, $logger);
$this->path = $path;
if (!file_exists($this->path)) {
if (!mkdir($this->path, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create path "%s".', $this->path));
}
}
}

protected function doSend(SentMessage $message): void
{
$file = $this->path.'/'.uniqid().'.message';
$serializedMessage = serialize($message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How integration test will looks like? Why not save in .eml format?

Copy link
Member Author

@Kocal Kocal Jul 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about .eml files. In my project, we exposed a endpoint only available in dev/test env that list *.message file and which display message details like from, to, subject and body. It was consumed by Cypress and it worked well.

But if it can be easily parsed with PHP or JS, why not.

if (false === file_put_contents($file, $serializedMessage)) {
throw new \RuntimeException(sprintf('Unable to write sent message in file "%s".', $file));
}
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/Mailer/Transport/FileTransportFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Transport;

use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;

/**
* @author Hugo Alliaume <@kocal>
*/
final class FileTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
if ('file' === $dsn->getScheme()) {
return new FileTransport($dsn->getOption('path'), $this->dispatcher, $this->logger);
}

throw new UnsupportedSchemeException($dsn);
}

public function supports(Dsn $dsn): bool
{
return 'null' === $dsn->getHost() && null !== $dsn->getOption('path');
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"symfony/amazon-mailer": "^4.4|^5.0",
"symfony/filesystem": "^4.4|^5.0",
"symfony/google-mailer": "^4.4|^5.0",
"symfony/http-client-contracts": "^1.1",
"symfony/mailgun-mailer": "^4.4|^5.0",
Expand Down