octobercmsoctobercms-backend

What can be the reason why Mail::send in Octobercms cuts the HTML tags from richeditor?


I am using the Mail::send service to send an email from a contact form. The body of the email is HTML, provided with the richeditor. I have a template 'my.plugin::mail.default':

subject = "Overwritten"
==
<p>Hi {{ name }},</p>

{{message_body | raw}}

<p>This email was sent using formatting (HTML)</p>

and the code to send is like this:

$data=post();
$vars = [
 'subject' => $data['subject'],
 'message_body' =>$data['message'],
 'name'=>'STB '
];
Mail::send('my.plugin::mail.default', $vars, function ($message) use ($vars) {
 $message->to("reciever@email.com");
 $message->subject($vars['subject']);
});

Email sent this way has the HTML message striped and only first tag (ex: <p>abc<p/>) is sent.

After many test, I figured out that when the HTML has the new-line character \n (as var $html below), Mail::send sends the message correctly. However, when the HTML hasn't the new-line character (as var $html_richeditor below), then it is cut.

$html = '<p>Aenean finibus erat eget maximus luctus. Phasellus vitae lectus dolor. Morbi convallis ex et turpis porttitor mattis morbi convallis ex et turpis porttitor mattis</p>
                <p>Proin porta, nibh eget elementum aliquet, metus elit viverra nisi, vitae ullamcorper elit eros ut urna.</p>
                <br/><br><p>Morbi convallis ex et turpis porttitor mattis.</p>';

$html_richeditor = "<p>Aenean finibus erat eget maximus luctus. Phasellus vitae lectus dolor. Morbi convallis ex et turpis porttitor mattis morbi convallis ex et turpis porttitor mattis</p><p>Proin porta, nibh eget elementum aliquet, metus elit viverra nisi, vitae ullamcorper elit eros ut urna.</p><p><br></p><p><br></p><p>Morbi convallis ex et turpis porttitor mattis.</p>";
        

Any idea how I can send the whole HTML from the richeditor? Should I config something in the richeditor or in the Mail?


Solution

  • I have debugged a lot and found out that HTML was parsed by Markdown and it was not linking that single line stuff and just keep the first tag.

    UPDATE - as per OctoberCMS 2.0 Docs

    It is important to note that the Markdown parser will only accept one HTML node per line.

    Ref: https://docs.octobercms.com/2.x/services/parser.html#using-html-in-markdown

    So we need to make our HTML like one node per line. we can just append \n to all tag's ending > bracket so it will persist during Markdown parsing.

    $data=post();
    $vars = [
     'subject' => $data['subject'],
     'message_body' => strtr($data['message'], ['>' => ">\n"]),
      // replacing `>` with `>\n` please use double quote with `\n`
     'name'=>'STB '
    ];
    Mail::send('my.plugin::mail.default', $vars, function ($message) use ($vars) {
     $message->to("reciever@email.com");
     $message->subject($vars['subject']);
    });
    
    

    OR as per @SamuelGeorges you can wrap your message within <div> tag.

    Now it should work as expected.

    if any doubts please comment.