Skip to content
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

Fix sending emails when storing invoices in S3 #242

Open
wants to merge 1 commit into
base: 1.0
Choose a base branch
from
Open
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
19 changes: 15 additions & 4 deletions src/Email/InvoiceEmailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sylius\Component\Mailer\Sender\SenderInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
use Sylius\InvoicingPlugin\Provider\InvoiceFileProviderInterface;
use Sylius\InvoicingPlugin\Filesystem\TemporaryFilesystem;

final class InvoiceEmailSender implements InvoiceEmailSenderInterface
{
Expand All @@ -25,12 +26,16 @@ final class InvoiceEmailSender implements InvoiceEmailSenderInterface
/** @var InvoiceFileProviderInterface */
private $invoiceFileProvider;

/** @var TemporaryFilesystem */
private $temporaryFilesystem;

public function __construct(
SenderInterface $emailSender,
InvoiceFileProviderInterface $invoiceFileProvider
) {
$this->emailSender = $emailSender;
$this->invoiceFileProvider = $invoiceFileProvider;
$this->temporaryFilesystem = new TemporaryFilesystem();
}

public function sendInvoiceEmail(
Expand All @@ -39,9 +44,15 @@ public function sendInvoiceEmail(
): void {
$invoicePdf = $this->invoiceFileProvider->provide($invoice);

$this
->emailSender
->send(Emails::INVOICE_GENERATED, [$customerEmail], ['invoice' => $invoice], [$invoicePdf->fullPath()])
;
// Since Sylius' Mailer does not support sending attachments which aren't real files
// we have to simulate the file being on the local filesystem, so that we save the PDF,
// run the callable and delete it when the callable is finished.
$this->temporaryFilesystem->executeWithFile(
$invoicePdf->filename(),
$invoicePdf->content(),
function (string $filepath) use ($invoice, $customerEmail): void {
$this->emailSender->send(Emails::INVOICE_GENERATED, [$customerEmail], ['invoice' => $invoice], [$filepath]);
}
);
}
}
40 changes: 40 additions & 0 deletions src/Filesystem/TemporaryFilesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\InvoicingPlugin\Filesystem;

final class TemporaryFilesystem
{
/** @var string */
private $targetDirectory;

public function __construct(?string $targetDirectory = null)
{
$this->targetDirectory = rtrim($targetDirectory ?? sys_get_temp_dir(), \DIRECTORY_SEPARATOR);
}

public function executeWithFile(string $filename, string $content, callable $callback): void
{
$filepath = $this->targetDirectory . \DIRECTORY_SEPARATOR . $filename;

if (!file_put_contents($filepath, $content)) {
throw new \RuntimeException(sprintf('Could not create file "%s"!', $filepath));
}

try {
$callback($filepath);
} finally {
unlink($filepath);
}
}
}