Symfony: How to send a file as email-attachment without saving it - symfony

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?

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.

Related

Is there a way to access Doctrine repositories from stand-alone code?

I have a Symfony 4 app which is complete and working. Any uploaded files are put inside an upload folder outside the doc root and accessed via a separate cdn url.
Inside the upload folder I have a htaccess file that redirects any request, let’s say for example an image, to a php file which then serves the image.
What I want to be able to do is send an auth key along with the image request so that the standalone index.php file can check the user is valid from the database.
This means I need some way of accessing the symfony entity manager from outside of symfony, by including it. The other option would be a curl request to a symfony controller, but I’d rather not add additional network requests if possible. The third option is a separate pdo direct to the database and make sql queries... but again I’d prefer to use the symfony entity manager by including symfony somehow so I can use other features if needed.
I actually tried including the symfony bootstrap in the index.php but that then drags in the need for routing to work which isn’t needed in this case.
You can access EntityManager like that:
require __DIR__.'/../vendor/autoload.php';
(new Dotenv())->load(__DIR__.'/../.env');
$kernel = new Kernel('dev', true);
$kernel->boot();
$user = $kernel->getContainer()->get('doctrine.orm.entity_manager')->getRepository(Repo::class)->find(id);
dd($user->getRoles());

How to define Wordpress global variable for use in a REST Controller?

I'm extending the WP REST API by writing a Controller class.
I'm trying to read the config for this class from a file, e.g:
{
"base-namespace": "myapi",
"version": "v1",
"resource": "things"
}
This would allow me to keep server and client in sync as they would both use the same config file.
However, I do not want WP to stay reading this file for every request it serves... Currently, if I read this file from anywhere in the plugin file (or any of its required files - including the Controller definition), and if I also echo out where I'm reading, I see it's always passing through that bit of code (including the reading) for every request.
I imagine I need to read this file somewhere outside the plugin itself - make it a global, and then access it when instantiating the Controller.
I'm new to WP - this is the first time dabbling with it. Where should this global variable definition go such that it's only executed once?
Note:
I have tried using require_once in my plugin to require a config file which does the file reading. I had put an echo statement there and it shows that that file gets executed for every request (despite the require_once).
I have also tried wrapping the file reading in an if(!isset($my_global_var) statement. But adding an echo statement inside the if statement shows that this global variable is always unset with every request served... clearly this needs to go in some kind of WP startup file which only gets executed once.
Thank you.
Store your config data as a PHP array in a .php file and then include it using the PHP include statement. Advanced PHP engines parse the PHP source once and cache a compiled representation of the script so that it does not have to re-parse the PHP sources everytime. So if your data is inside a PHP source file it would be saved in the PHP's engine compiled script cache.
Of course if your client is non-PHP it would need to have code to parse a PHP array.

Send file with Postman to FosRestBundle Symfony

Im trying to upload file to a Symfony app, then retrieve the file from the ParamFetcher of FosRestBundle, finally create a SonataMedia object with the uploaded file.
Im stuck on how to upload the file. The entire application has been developed using Postmans POST, GET, DELETE, etc functions for testing right until now.
If already tested as shown in the picture, simply doing a POST with de param file having the image file selected. ParamFetcher is not detecting the param and returns error message saying file is null.

How to get relative path to a file in cache

I'm trying to get the relative path to a file inside a app/cache dir.
I've tried via custom config file using:
parameters:
output_dat_path : %kernel.root_dir%/cache/%kernel.environment%/myfile.dat
Also I tried:
$kernel = $this->get('kernel');
$path = $kernel->getRootDir() . '/cache/' . $kernel->getEnvironment() . 'myfile.dat';
Both ways generate absolute paths, that both work fine using them inside a controller or service, but, I need a relative path to generate a link for the user.
I'm thinking to move the file to the web directory in my service and use asset in the template to generate the link, but the file is private for every user and I don't want to expose it.
Any thought?
Thanks in advance.
UPDATE
I solved this as follows:
User, once authenticated, generates his file. If the file doesn't exists, is generated. If it exists, the controller checks the user rights, time, etc. and returns the file.
If the system has to be generated, the file is created in a private folder and an encrypted name is generated. The encrypted name is generated using base_64 and some changes after that.
The system shows to the user a link. That link is something like /get/privatefile/eDfa...23Hn
When the user clicks on it, the controller checks the user, time from creation, etc.
If all is fine the controller returns the file.
I made this because the file must be created once, and be available for a short period of time and after must be deleted. If an user pass the link to other person, the checksum of the encrypted name fails and the file cannot be accessed.
I am sure that this is not the best solution but it works right know.

drupal 7 error when uploading with new field

I've got a very strange problem using drupal 7. I've got a wbesite running and using views and content types I have been able to build a lot of functionality. Now the following occurs:
When I add a new field to a content type (new or existing) of the the type file or image, I have this field in the form but when I try to upload a file or image using this form it gives an error "The file could not be uploaded" or the full error (from the recent log messages): "The upload directory public:// for the file field field_katapult_voorpagina could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled."
Strangely, when I upload a file or image using an older existing field (pointing to the same directory: public://) it does not give an error and the files upload without a problem. So this makes me think that the server settings are actually set properly.
Anybody who can help me with this problem?
If not, can anybody point me in the right direction as to where I can find the files that handle the upload process in drupal 7?
Many thanks in advance!
You need to check a couple of things :
where is your public files folder, and if that is writable by the webserver ( in /admin/config/media/file-system )
where does your working field send files (it might be a different directory), and if it is, use that for the new fields as well.
Check you file destination directory setting of new image field. Do not include preceding or trailing slashes. This field supports tokens.
You can also check max, min resolutions, file upload limit, image style (if it is new image style other than thumbnail/ or existing) & number of values ( if it set to 1 you can't upload 2 or more). Finally you have to set it public files type.
I think you should check the files folder permission.

Resources