简体   繁体   中英

How to get path to images generated with webpack encore

I have one problem with the images I want to include in my e-mail with phpmailer. I have tried to use swift mailer with my symfony app but not works I have tried all configuration (all host all ports all encryption etc) during 3 days I have called them etc without any success. It works in local not in remote env... Anyway, let's go back to my problem.

I use webpack with encore (new way of setting assets for symfony4). All my images are renamed in the public folder. myimagename.65342324b5.jg I/O myimagename.jpg when I run encore to deploy assets.

The problem is when I want to AddEmbeddedImage

let's supposed I have logo.png in assetsfolder. When I deploy assets it is renamed logo.536324324V3.png in public images folder. So when I do

$mail->AddEmbeddedImage('images/logo.png','mon_logo', 'logo.png');

and when I want to refer in my html body of the e-mail to 'mon_logo' the image is not loaded because it is not existing only logo.536324324V3.png.

I have 2 options:

  1. First I disable the automatic renaming of my images with webpack encore .but I don't know how.
  2. Second: I make this script able to recognise logo.png as logo.536324324V3.png. Normally it is automatic with the manifest.json generated after the assets deployment, but in my case the path of my image is in my controller so I cannot specify that it is an asset.

    But I cannot do

     $mail->AddEmbeddedImage(" {{ asset('images/logo.png')}}",'mon_logo',  \n    'logo.png');  

    Anyway it makes no sense.

So if you have solutions I'll take it:)

Thank you :)

Assuming you are using the Asset-component as suggested in your second approach, you could just pass in the class \\Symfony\\Component\\Asset\\Packages which should be available under the service id assets.packages and with Symfony 4 likely also under it's fully qualifieed class name. The Packages class manages your assets and is also what's used in the Twig_Extension in the asset() function.

So in your code you would probably do something like this:

$fileUrl = $packages->getUrl('images/logo.png');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM