Skip to content

Twig-Bridge TemplatedEmail::__serialize throws error with file attachment #49880

Closed
@doenietzomoeilijk

Description

@doenietzomoeilijk

Symfony version(s) affected

5.4.21

Description

I came across this when using a Symfony based CMS, but I believe the actual problem lies within the twig bridge.

When using the Mailer to send an e-mail message with an attachment, the entire context is stored for the profiler. This allows me to see the entire email, which is nice, but it throws an error when an attachment is involved: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed.

How to reproduce

Use Mailer to send an e-mail with an uploaded attachment, with the Profiler active.

Possible Solution

When I edit \Symfony\Bridge\Twig\Mime\TemplatedEmail::__serialize and add this bit of code before the return, the Profiler can continue to do its thing:

        foreach ($this->context["data"] as $index => &$item) {
            if ($item instanceof UploadedFile) {
                $item = "[Uploaded file {$item->getClientOriginalName()}]";
            }
        }

Additional Context

I'm not sure if this is actually the best place to put this; maybe the Profiler itself should do this, to catch all possible instances of uploaded files, not just those in this place in the Twig Bridge. That's up for discussion, I think, I can't claim to know the entire Symfony codebase that well. ;)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions