Skip to content

[TwigBridge] Allow attachment name to be set for inline images #60163

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

Merged
merged 1 commit into from
Apr 16, 2025
Merged
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/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Add `field_id()` Twig form helper function
* Add a `Twig` constraint that validates Twig templates
* Make `lint:twig` collect all deprecations instead of stopping at the first one
* Add `name` argument to `email.image` to override the attachment file name being set as the file path

7.2
---
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ public function toName(): string
* some Twig namespace for email images (e.g. '@email/images/logo.png').
* @param string|null $contentType The media type (i.e. MIME type) of the image file (e.g. 'image/png').
* Some email clients require this to display embedded images.
* @param string|null $name A custom file name that overrides the original name (filepath) of the image
*/
public function image(string $image, ?string $contentType = null): string
public function image(string $image, ?string $contentType = null, ?string $name = null): string
{
$file = $this->twig->getLoader()->getSourceContext($image);
$body = $file->getPath() ? new File($file->getPath()) : $file->getCode();
$this->message->addPart((new DataPart($body, $image, $contentType))->asInline());
$name = $name ?: $image;
$this->message->addPart((new DataPart($body, $name, $contentType))->asInline());

return 'cid:'.$image;
return 'cid:'.$name;
}

/**
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Attachments</h1>
{{ email.attach('@assets/images/logo1.png') }}
{{ email.attach('@assets/images/logo2.png', name='image.png') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<img src="{{ email.image('@assets/images/logo1.png') }}">
<img src="{{ email.image('@assets/images/logo2.png', name='image.png') }}">
103 changes: 103 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Mime/WrappedTemplatedEmailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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\Bridge\Twig\Tests\Mime;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\FilesystemLoader;

/**
* @author Alexander Hofbauer <a.hofbauer@fify.at
*/
class WrappedTemplatedEmailTest extends TestCase
{
public function testEmailImage()
{
$email = $this->buildEmail('email/image.html.twig');
$body = $email->toString();
$contentId1 = $email->getAttachments()[0]->getContentId();
$contentId2 = $email->getAttachments()[1]->getContentId();

$part1 = str_replace("\n", "\r\n",
<<<PART
Content-ID: <$contentId1>
Content-Type: image/png; name="$contentId1"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
name="$contentId1";
filename="@assets/images/logo1.png"

PART
);

$part2 = str_replace("\n", "\r\n",
<<<PART
Content-ID: <$contentId2>
Content-Type: image/png; name="$contentId2"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
name="$contentId2"; filename=image.png

PART
);

self::assertStringContainsString('![](cid:@assets/images/logo1.png)![](cid:image.png)', $body);
self::assertStringContainsString($part1, $body);
self::assertStringContainsString($part2, $body);
}

public function testEmailAttach()
{
$email = $this->buildEmail('email/attach.html.twig');
$body = $email->toString();

$part1 = str_replace("\n", "\r\n",
<<<PART
Content-Type: image/png; name=logo1.png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; name=logo1.png; filename=logo1.png

PART
);

$part2 = str_replace("\n", "\r\n",
<<<PART
Content-Type: image/png; name=image.png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; name=image.png; filename=image.png

PART
);

self::assertStringContainsString($part1, $body);
self::assertStringContainsString($part2, $body);
}

private function buildEmail(string $template): TemplatedEmail
{
$email = (new TemplatedEmail())
->from('a.hofbauer@fify.at')
->htmlTemplate($template);

$loader = new FilesystemLoader(\dirname(__DIR__).'/Fixtures/templates/');
$loader->addPath(\dirname(__DIR__).'/Fixtures/assets', 'assets');

$environment = new Environment($loader);
$renderer = new BodyRenderer($environment);
$renderer->render($email);

return $email;
}
}
Loading