-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
[Mailer] add File transport #31947
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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')]; | ||
} | ||
} |
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()); | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this commen 8000 t to others. Learn more. How integration test will looks like? Why not save in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know about But if it can be easily parsed with PHP or JS, why not. |
||
if (false === file_put_contents($file, $serializedMessage)) { | ||
Kocal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new \RuntimeException(sprintf('Unable to write sent message in file "%s".', $file)); | ||
} | ||
} | ||
} |
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'); | ||
Kocal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.