Exclude javascript from email template

Using SilverStripe 4.12

I have a ContactPage and it has an AJAX form on there that sends emails. In my ContactPage_Controller I have the email template generated like this:

$emailBody =  $this->customise([
    			'Layout' => $this->customise($templateData)->renderWith(['MyModule\Layout\Contact_Email'])])->renderWith(['Email']);

However, because ContactPage_Controller has a javascript requirement, it includes this javascript reference in the email html source which is very bad regarding anti-spam filters.

protected function init() {





I’m a bit clueless as to exclude the script from the email template rendering.

Well, a couple of choices…

  1. Use the setHTMLTemplate() method on the Email class, and set the body up that way rather than generating it through the controller
  2. Generate your body HTML using SSViewer instead of the controller (but option 1 is really the ‘normal’ way)
  3. Move the Requirements call out of the init() method, and into the index() method on your controller (assuming you don’t have a lot of different actions defined in the controller)
1 Like

Thanks so much! Your answer is definitely more elaborate and complete but I went with the simple method of @GuySartorelli because I haven’t yet figured out how setHTMTemplate() would work with a sub-$Layout

In addition to Tim’s suggestions, you can also use Requirements::block('themes/mytheme/js/contact.js); in your email action to block the javascript file for that action.

All of the suggestions Tim has suggested are honestly better than this, but it never hurts to know another way of doing things.

1 Like