silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 823 forks source link

ENHANCEMENT: make it easier to extend email #10969

Closed sunnysideup closed 11 months ago

sunnysideup commented 11 months ago

Affected Version

SS 5.0

Description

It is really hard to extend Email. I have a module that extends the Email Class, but there is no way to get between the compilation of the email body (updateHtmlAndTextWithRenderedTemplates) and the sending of it.

updateHtmlAndTextWithRenderedTemplates should be a protected function OR there should be a $this->extend at some point. I am happy to code this if this is a good idea. .

sunnysideup commented 11 months ago

This is what I ended up doing:


    /**
     * @param resource|string|null $body
     *
     * @return $this
     */
    public function html($body, string $charset = 'utf-8'): static
    {
        if (null !== $body && !\is_string($body)) {
            // change the HTML
            $body = self::emogrify_html($body);
        }
        return parent::html($body, $charset);
    }
GuySartorelli commented 11 months ago

Your workaround seems like it's very low effort - what use case do you foresee the proposed extension hook allowing you to do that your workaround doesn't allow?

sunnysideup commented 11 months ago

Yes, that is a very fair point. It just took me a lot of time to even work out where to find that point so it may be better just to improve the docs - e.g.


### extensions
If you, for example, would like to add inline CSS to your email, 
using something like Emogrifier, (https://github.com/MyIntervals/emogrifier) 
then you could look at extending the `html` method. 

However, one of the issues is, for example, that this set html method can be called multiple times and that this is hard to control. So, ultimately, it would still be good to have a justBeforeSending point, where you can add any extras, etc...

GuySartorelli commented 11 months ago

Adding inline CSS sounds like something to handle in your email template, or however else you're defining your HTML body. A "before sending point" sounds like something you're probably better off handling via symfony/mailer functionality, i.e. by adding an event subscriber to the message event.

In either case I don't think any new extension hooks or changes to method visibility are required.

sunnysideup commented 11 months ago

that all makes sense. The emogrifier technique is a technique where you merge CSS and HTML for email purposes. This is done in PHP, so it can't really be done in the template.

I am happy for this one to close and someone can re-open it if they feel the same way.