0

I am using symfony 6.1. I have a form that accepts a file upload which works as far as I can see.

The form is not backed by an entity and in this particular case I don't really see the necessity to save the uploaded file somewhere. It would be enough to send it as an attachment of a mail. I read about attaching files using symfony's mailer component but the examples only cover attaching files by paths.

Is it even possible to skip the step to save the file somewhere?

Wolfone
  • 1,276
  • 3
  • 11
  • 31

1 Answers1

2

It is possible to add attachments and embed images from streams - so without saving it.

Attachments

$email = (new Email())
    // ...
    ->attach(fopen('/path/to/documents/contract.doc', 'r'))
;

Embedded images

$email = (new Email())
    // ...
    // get the image contents from a PHP resource
    ->embed(fopen('/path/to/images/logo.png', 'r'), 'logo')
;

You can read more about it in the symfony docs.

Michel Rummens
  • 459
  • 3
  • 9
  • 1
    Thanks for your answer! I was a bit blind when reading the doc. So I got it to work coming from your answer. Though from what I understand this still requires a temporary copy of the file being in whatever temp-folder was configured for php. But that's probably unavoidable. – Wolfone Aug 11 '22 at 11:29
  • 1
    As far as my understanding goes, that is indeed unavoidable, but I could be in the wrong here. – Michel Rummens Aug 12 '22 at 12:07