Custom mail template body

I have a default mail template to send to clients.
However i am giving thjem the possibility to override this by creating their own mail subject and mail body in a rich text editor (so html output)

when I am trying to send the custom mail, I encountered errors, either the content is not parsed or there is an error because content contains HTML character.

Mail::send(
  [
    'raw' => $module->mail_template_body
  ],
  $params,
  function ($message) use ($module, $customer) {
    $message->to($customer->user->email);
    $message->subject($module->mail_template_subject);
  }
);
Unexpected character "&" in "__string_template__c95f228aeb20c2204c3cb56e2d72a939" at line 1.  

any idea please?

Hi Chris

I’ve had this error a few times before, and it’s usually caused by some syntax error, mostly with something Twig related. Are you allowing your customers to use variables?

I’d recommend to put the content into a partial and see what’s happening. Either a cms partial or a mail layout and see whats happening there.

Or, alternatively, maybe try using

Mail::raw([
    'text' => 'This is plain text',
    'html' => $module->mail_template_body
], function($messags) {

});

This way you should get the unparsed content of your mail to check for errors.

Thanks @marco.grueter

I tried that as well I am using twig variables indeed.

Mail::raw(
                    [
                        'text' => 'just plain text',
                        'html' => $module->mail_template_body
                    ],
                    function ($message) use ($module, $customer) {
                        $message->to($customer->user->email);
                        $message->subject($module->mail_template_subject);
                    }
                );

the twig variables are not parsed, there are just rendered as plaintext unfrotunately

The doc is suggesting to use the method send for wha I am trying to achieve but then it is trying to find a mail template from the html body mail i am trying to send

Mail::send(
                    [
                        'html' => $module->mail_template_body
                    ],
                    $params,
                    function ($message) use ($module, $customer) {
                        $message->to($customer->user->email);
                        $message->subject($module->mail_template_subject);
                    }
                );

View [<p>Hi&nbsp;{{&nbsp;user.first_name&nbsp;}},<.p><p>'{{&nbsp;module.name&nbsp;}}' is ready for you!<.p><p>To access the course, click the button below as usual. Enjoy!<.p><p>{%&nbsp;partial&nbsp;'button'&nbsp;url=course.link type='positive'&nbsp;body&nbsp;%}<.p><p>Course login<.p><p>{%&nbsp;endpartial&nbsp;%}<.p><p><br><.p><p>To your success,<.p><p>{{ course.author }}<.p>] not found.  

Ah yes, the Mail::send function expects a template slug/code.
But nonetheless, I think that the error gives a hint:

View [<p>Hi&nbsp;{{**&nbsp;**user.first_name**&nbsp;**}},<.p><p>'{{**&nbsp;**module.name**&nbsp;**}}' is ready for you!<.p><p>To access the course, click the button below as usual. Enjoy!<.p><p>{%&nbsp;partial&nbsp;'button'&nbsp;url=course.link type='positive'&nbsp;body&nbsp;%}<.p><p>Course login<.p><p>{%&nbsp;endpartial&nbsp;%}<.p><p><br><.p><p>To your success,<.p><p>{{ course.author }}<.p>] not found.

I’m pretty sure it’s the &nbsp; between the curly brackets and the Twig variable that trips up the parser. That’s unfortunately a problem when using the richeditor, imho.

You could try this Froala setting: https://froala.com/wysiwyg-editor/docs/options/#htmlUntouched
Sorry, I’ve only got an older example for changing froala setting, but I think this still works:

+function ($) { "use strict";
    $(document).render(function() {
        if ($.FroalaEditor) {
            $.FroalaEditor.DEFAULTS = $.extend($.FroalaEditor.DEFAULTS, {
                htmlUntouched: true
            });
        }
    })
}(window.jQuery);

Place this in a js file and use addJs to inject the asset in your backendcontroller.
But this might have other side effects.

You might have better luck using the markdown editor (that’s what I do, I usually use the mail template backend form as a nested form, and use a custom model with a related system MailTemplate model) or somehow sanitize the template string before feeding it to the parser.

thanks a bunch @marco.grueter i will explore the road of markdown, and if it does not work either, I will just use a simple textarea (to hold plain text) as last resort solution