Chapters
31 Chapters
|
3:19:40
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
31.
Processing Encore Files through inline_css()
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial is built on Symfony 4.3, but will work well with Symfony 4.4 or 5.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.110.11
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-snappy-bundle": "^1.6", // v1.6.0
"knplabs/knp-time-bundle": "^1.8", // v1.9.1
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"league/html-to-markdown": "^4.8", // 4.8.2
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.1,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.1.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.1
"sensio/framework-extra-bundle": "^5.1", // v5.4.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.3.4
"symfony/console": "^4.0", // v4.3.4
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.3.4
"symfony/framework-bundle": "^4.0", // v4.3.4
"symfony/mailer": "4.3.*", // v4.3.4
"symfony/messenger": "4.3.*", // v4.3.4
"symfony/property-access": "4.3.*", // v4.3.4
"symfony/property-info": "4.3.*", // v4.3.4
"symfony/security-bundle": "^4.0", // v4.3.4
"symfony/sendgrid-mailer": "4.3.*", // v4.3.4
"symfony/serializer": "4.3.*", // v4.3.4
"symfony/twig-bundle": "^4.0", // v4.3.4
"symfony/validator": "^4.0", // v4.3.4
"symfony/web-server-bundle": "^4.0", // v4.3.4
"symfony/webpack-encore-bundle": "^1.4", // v1.6.2
"symfony/yaml": "^4.0", // v4.3.4
"twig/cssinliner-extra": "^2.12", // v2.12.0
"twig/extensions": "^1.5", // v1.5.4
"twig/extra-bundle": "^2.12|^3.0", // v2.12.1
"twig/inky-extra": "^2.12", // v2.12.0
"twig/twig": "^2.12|^3.0" // v2.13.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.2.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/browser-kit": "4.3.*", // v4.3.5
"symfony/debug-bundle": "^3.3|^4.0", // v4.3.4
"symfony/dotenv": "^4.0", // v4.3.4
"symfony/maker-bundle": "^1.0", // v1.13.0
"symfony/monolog-bundle": "^3.0", // v3.4.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.4
"symfony/stopwatch": "4.3.*", // v4.3.4
"symfony/var-dumper": "^3.3|^4.0", // v4.3.4
"symfony/web-profiler-bundle": "4.3.*" // v4.3.4
}
}
24 Comments
Hello!
After changing to encore_entry_css_source, inline_css does not work anymore. I have checked that the twig extension is successfully called (it found the css files and store them in source variable) but it's not included in the mail anymore. I am developping locally. Stop/start messenger consumer command, removed cache, ... but still not working. Any idea?
The twig extension:
Thx!
Hey be_tnt!
So.... I am aware of a potential issue that you could be hitting. You don't have quite the setup I would expect for that problem... but it's close enough that I think it's the same.
First let me ask: it sounds like you are using messenger to send your emails asynchronously, is that correct? You are "routing" the messages to a messenger transport, then the emails are actually sent when you run the messenger:consume command, right?
The problem (I believe) is that your email is being rendered twice. And the second time it's rendered, because Encore thinks the CSS & JS have already been "output", the getCssFiles() method returns an empty array. You could verify that this the cause by adding this line right after the getCssFiles() call:
If that fixes the problem, then this is the problem :). Let me know if it helps - and then we can debug further. I mean, having the "reset" is a "fine" thing to keep there - it won't hurt anything (it just shouldn't be needed). From my research, there is a problem when Messenger is installed, but emails are still handed synchronously - ir I remember correctly, Mailer dispatches an event 2 times in one request... they both render the message... and voila! The second one has no CSS. It's (I believe) actually a subtle bug in Symfony that you only see when using Encore. But, let me know what you find out.
Cheers!
Hi weaverryan !
You were completely right. I am indeed sending the emails asynchronously with thee messenger:consume command. After adding the suggested line after the getCssFiles(), the css was inlined again in my mail :)
thx!
Hey weaverryan!
Old post, but I stumbled upon the same error without using Messenger:
When a user submits a form and selects one special value, a first email is sent to the user AND a second one to an admin group - which then gets no styling...
This one was hard to catch, esp. as the mail rendered fine in a DevController to display all mails in an iframe and in tests.
I solved this by wrapping
getEncoreEntryCssSourcein acache->get()call (AND resetting EntryPointLookupInterface ;)Regarding this lesson:
It is nice to have the "Tip" at the end, but:
getEncoreEntryCssSourcecould also simply include thereset()call, so this warning is not even needed </li>What do you think?
(jesus, Disqus should simply use Markdown :)
Hey boedah!
> (jesus, Disqus should simply use Markdown :)
Yea, we're replacing Disqus right now - we hate it too ;).
> * it is not about Messenger at all (it happens when 2 or more mails are rendered in the same request)
Good point!
> * maybe `getEncoreEntryCssSource` could also simply include the `reset()` call, so this warning is not even needed </li>
Even better point! I really like that idea!
I'm going to propose a modified note to use your solution. I appreciate it - great thinking!
Cheers!
👍
Issue seems still there, I am not using Messenger and I got the same issues. Reset is required.
This is also processing twice.
Hey Florent,
Yes, you're right, you still need that reset(). It's not a bug, it's just a limitation... a compromise to make WebpackEncoreBundle great in other cases :)
Cheers!
Not a problem ;) I think it should appear in tutorial.
Hey Florent,
Agree! And it IS in the tutorial ;) See https://symfonycasts.com/sc...
Cheers!
This is another tutorial :D
I am not generating a PDF for now (but I will, surely)
And reset is not fixing the twice rendering.
Hey Florent,
I'm confused, we're talking on https://symfonycasts.com/sc... page, and I linked to https://symfonycasts.com/sc... page, so both pages are chapters of https://symfonycasts.com/sc... , or am I missing something here? :)
> And reset is not fixing the twice rendering.
Actually, the purpose of reset() is the opposite, every time you call reset and then render encore assets - it will render all the assets again. And this workaround is needed because Webpack Encore avoid rendering same assets twice.
Cheers!
Is there more information on this issue anywhere? Why the double rendering when using Messenger? Thanks
Hey Benr77!
I believe this is the related issue - https://github.com/symfony/... - I've just bumped it :).
Cheers!
Hi!
Is there an equivalent Twig function to use with AssetMapper?
Hey TigerShark,
No, at least not yet :) AssetMapper comes with
importmap()Twig function, and you can also leverage theasset()Twig function to link to your assets.Cheers!
"First let me ask: it sounds like you are using messenger to send your emails asynchronously, is that correct?"
Even if you use messenger in sync mode, EntrypointLookup has to be reset.
What's more in SF 5.2 the ServiceSubscriberInterface doesn't work any more. EntrypointLookup should by DI in constructor.
Hi @sabat24!
> Even if you use messenger in sync mode, EntrypointLookup has to be reset
Yes, I think that may be correct: when Messenger is installed, even if you're sending the emails sync, Symfony does the odd double-render. I really need to make handling this easier in WebpackEncoreBundle (which I maintain), but it's a tricky issue to get right :/.
> What's more in SF 5.2 the ServiceSubscriberInterface doesn't work any more. EntrypointLookup should by DI in constructor
You can definitely inject through the constructor, but I'm not aware of any changes in Symfony 5.2 related to ServiceSusbcriberInterface - that interface still exists and should still work exactly like before. If you're having any trouble with it, let me know and we'll see if we can debug :).
Cheers!
When I tried to use ServiceSusbcriberInterface I received following error:
<blockquote>Symfony\Component\DependencyInjection\Exception\
The "Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.
</blockquote>
And this is the line which causes the problem
<blockquote>Container->make('Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface', 1)
</blockquote>
You can ignore that error above. I used wrong ContainerInterface. It was DependencyInjection one instead of Psr\Container. Everything works fine.
Hi.
Hey Usuri,
If you send the email sync, then you should have access to the rendered HTML via $mail->getHtmlBody() method - call it right after the "$this->mailer->send($mail);" call. The renderer will render the template and set its HTML on the same email object.
If you send async - you probably need to do this via Messenger component.
I hope this helps!
Cheers!
Hello !
In the event that the administrator of a site wishes to send a message to all of its customers, it would be useful to use "bcc" rather than recreating the email each time.
In function, this could result in
That must be it if I don't say nonsense. (confirmation?)
However, in the Twig template, we will no longer be able to use {{email.toName}}
So my question is:
<b>"How to properly manage the sending of this kind of grouped emails"</b> so that each recipient receives the same email (but being able to include the recipient's name in the content each time as we did for {{email.toName}} ?
Hey Kiuega,
Good question! Well, BCC is not quite for this. When you use BCC - you suppose to send the exact same email to the email addresses specified in BCC, but in your case you're going to send a different unique emails. They are unique because of the unique user name in the email.
I think the only way to achieve "group emails" is to use Sendgrid API (or other service API you're using). For Sendgrid - take a look at Bulk Email Service: https://sendgrid.com/docs/g... . Usually, each email service gives you its own implementation of this, and it may vary from service to service.
Otherwise, you can try to make the email the same for every user and use BCC. Or, create unique emails for every user and send them separately. You may want to use Messenger integration in this case that will help with sending them async behind the scene.
I hope this helps!
Cheers!
"Houston: no signs of life"
Start the conversation!