New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DX] Add a new mailer capable of rendering Twig templates #15060
Comments
That should be either part of Swiftmailer directory or the Swiftmailer bundle, but that cannot be in Symfony itself. I think I'd prefer something in Swiftmailer directly as it would be useful outside the context of Symfony as well then. |
@stof do you think someone from the FOS organization could implement this feature based on the |
I'm -1 on rendering Twig blocks directly for each part of the email though. It has multiple drawbacks (and yes, I know I'm the one implementing this in FOSUserBundle, but I learned more about it when using this at work and the FOSUserBundle implementation actually does not deal with these drawbacks properly):
My idea for these points (not yet implemented in my work project) would be to change the API to give a template name prefix only (which would target a folder) and then having multiple templates ( Another thing is that I think this should not be implemented directly in Swiftmailer. This should probably be done in a different service than the In any case, this is something I want to experiment with during the summer. I will post feedback about it while working on it (it will probably have to wait until after July 14th though, after my vacations, as I doubt I will have time to do this before leaving) |
@stof thanks for the detailed explanation. I won't comment about technical details, but about DX details.
I don't like this idea at all. Having to create a template just for the subject of the email will make me to never use this mechanism.
I don't agree. Developer doesn't have to deal with this. The
|
Glad to see this discussion happening, since I too struggle with this and usually end up implementing a FOS-inspired Twig Mailer with mixed results. Just to add to this discussion, regardless of how the actual template/email is rendered, it would be nice to see some overall abstraction of the process. I think Sylius did a great job there: https://github.com/Sylius/Sylius/tree/master/src/Sylius/Component/Mailer |
Big +1 on this one, did also have to implement an abstraction layer to solve attachments and i18n. @stof 's point on Twig's error handling in my opinion is one of the biggest concerns I can't overcome right now. I don't see why you should use Twig blocks for from/to/subject, and think that also could be solved using an https://github.com/lexik/LexikMailerBundle also does a nice job on abstracting the mailings. |
possible duplicate of symfony/swiftmailer-bundle#108 |
The issue in the SwiftmailerBundle repository is younger so its not a duplicate :) other issue is a duplicate. |
@sstok OK, I close the other issue :) |
@javiereguiluz If your plain-text email contains HTML, you have a huge issue. |
👍 Often used feature |
I'm closing it because this won't be part of the Symfony core (it could be part of Swiftmailer as Fabien said) and it would be really hard to implement (as Stof showed). Thank you all for the discussion. |
@javiereguiluz Thank you for opening the discussion and moving a bit further. |
If any of you are still interested in this idea, we're giving it a shot at swiftmailer/swiftmailer#845 Thanks! |
The problem
In my opinion, the current solution to send emails with Symfony is too "low level". (Related article: Send mails in Symfony in a way that does not suck!)
In my experience, most Symfony applications want to render a Twig template when sending an email. That's why FOSUserBundle includes the TwigSwiftMailer and that's why looking for
Swift_Message::newInstance
in GitHub gives you thousands of results with lots of shortcut methods to render a template.The solution
I propose to create a new
renderAndSend()
method for themailer
service. It would work as follows:{% block body %}
is a shortcut to set the HTML content. If you need different contents for HTML and TXT, use these blocks instead:$to
or$from
email addresses are dynamic, you can pass them in the parameters array:Then you don't need to define the
to
andfrom
blocks:sendAndRender()
method would define a third optional parameter to set the file attachments:If the attachments array is associative, the keys are used as the file names:
The text was updated successfully, but these errors were encountered: