Forum New Post Email Notification Format

Sam Mueller asked on November 30, 2016 16:45

Hi Kentico Devs,

I'm having an issue trying to display the proper HTML format for an email notification when a new forum post is created when the user subscribes to that thread. I'm currently using Forum - New Post email template HTML Version.

Here is the K#/HTML code:

<html>
<head>
  <style>
    BODY, TD
    {
      font-size: 12px; 
      font-family: arial
    }
  </style>
</head> 
<body>
<p>
  This is a notification of a new post added to the forum you subscribed to:
</p>
<table>
  <tr valign="top">
    <td>
    <strong>Forum:</strong>
    </td>
    <td>
    {%forumdisplayname%}
    </td>
  </tr>
  <tr valign="top">
    <td>
    <strong>Subject:</strong>
    </td>
    <td>
    {%postsubject%}
    </td>
  </tr>
  <tr valign="top">
    <td>
    <strong>Posted by:</strong>
    </td>
    <td>
    {%TrimSitePrefix(postusername)%}
    </td>
  </tr>
  <tr valign="top">
    <td>
    <strong>Date and time:</strong>
    </td>
    <td>
    {% posttime %}
    </td>
  </tr>
  <tr valign="top">
    <td>
    <strong>Text:</strong>
    </td>
    <td>
    {%StripTags(posttext)%}         
    </td>
  </tr>     
</table>      

<p>
<a href="{% link + "&tpage=" + Math.Ceiling(Convert.ToDouble(ForumContext.CurrentThread.PostThreadPostsAbsolute + 1, 1) / 10d) #%}">Click here to view forum on-line</a> &nbsp;
    <a href="{%unsubscribelink%}">Click here to unsubscribe</a>
</p>
</body>

Here is an example of the current output and what I want to achieve (Note: this is just a snippet from date/time and text but it showcases enough):

Currently it produces this:

Date and time: 11/30/2016 8:19:57 AM

Text: 1. Is fixing the naming issue worth no longer having your default RDP settings apply? 2. Would auto-sharing (by default) your clipboard with the RDP session be a bad thing, or a good thing? 3. Should we enable multi-monitor support be default? (for some interesting reading on that, click here) Thanks! 1. Here's hoping it's not one or the other. I wouldn't mind that it's not just the default settings. 2. Good Thing. Having this forced off would be an absolute deal breaker. 3. Not sure. It's rare that we'd use rdp for anything other than servers, so I'm not sure what multi-monitor support with RDP is actually like.

Click here to view forum on-line Click here to unsubscribe

I would like it to produce this:

Date and time: 11/30/2016 8:19:57 AM

Text:

  1. Is fixing the naming issue worth no longer having your default RDP settings apply?
  2. Would auto-sharing (by default) your clipboard with the RDP session be a bad thing, or a good thing?
  3. Should we enable multi-monitor support be default? (for some interesting reading on that, click here)

Thanks!

  1. Here's hoping it's not one or the other. I wouldn't mind that it's not just the default settings.

  2. Good Thing. Having this forced off would be an absolute deal breaker.

  3. Not sure. It's rare that we'd use rdp for anything other than servers, so I'm not sure what multi-monitor support with RDP is actually like.

Click here to view forum on-line Click here to unsubscribe

As you can see, I'm looking to return the format of the original forum post as closely as possible, preferably identical to the original post sent to and viewed as an email notification. Any insight in how to achieve this would be appreciated, thanks.

Recent Answers


Anton Grekhovodov answered on November 30, 2016 17:31

Hi Sam,

Your HTML is broken, because there is this macros {%StripTags(posttext)%} which removes all HTML tags from blog post. For secure reasons, it will be better not remove it, because the user can insert some scripts into post. But you can write your own macro to remove only some potentially dangerous tags.

2 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on November 30, 2016 17:42 (last edited on November 30, 2016 17:51)

Hi Anton,

I've tried removing the StripTag macro before and just have it as {%(posttext)%}, it still displayed it itself in this format:

Text: 1. Is fixing the naming issue worth no longer having your default RDP settings apply? 2. Would auto-sharing (by default) your clipboard with the RDP session be a bad thing, or a good thing? 3. Should we enable multi-monitor support be default? (for some interesting reading on that, click here) Thanks! 1. Here's hoping it's not one or the other. I wouldn't mind that it's not just the default settings. 2. Good Thing. Having this forced off would be an absolute deal breaker. 3. Not sure. It's rare that we'd use rdp for anything other than servers, so I'm not sure what multi-monitor support with RDP is actually like.

Click here to view forum on-line Click here to unsubscribe

Except it had the HTML tags in it. Removing the macro never changed the format, it just added the tags back in to the original format. I want to port over the exact format as it was written in the actual forum post in an email notification.

This older post describes the issue I'm currently facing. I'm in the midst of trying {%posttext|(encode)false%} to see if that will solve it: https://devnet.kentico.com/forums/f51/t20778/please-format-forum-subscription-emails

0 votesVote for this answer Mark as a Correct answer

Andy T answered on November 30, 2016 18:00

Sam,

Yes, encode parameter can help you, and it's actual syntax Macro syntax. But you know, HTML email messages are shown in different ways in browsers or e-mail programs. If you insert all HTML from post, it may be not well-formatted.

1 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on November 30, 2016 18:55

Thanks for the reply Andy.

Yeah that's a fair assessment. There may be some limitations in what is rendered in terms of format. Setting the macro parameter posttext to encode false ({%posttext|(encode)false%}) did solve it from displaying the text as one big blob. I think I'll take this as a win and run with it.

Thanks everyone.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on November 30, 2016 19:05

When you remove the StripTags does the HTML have the line breaks?

it's been a while since I have used the email that is generated for forum posts, but I have had issues in the past getting the content from a post because Kentico doesn't store the line breaks as HTML in the DB if you are not using the WYSIWYG. So that might be the issue you are having.

You can create a custom macro to format the post using something like this:

 private string FormatPostText(string postText, ForumInfo forumInfo)
    {
        string @string = ValidationHelper.GetString(postText, "", (System.Globalization.CultureInfo)null);
        DiscussionMacroResolver discussionMacroResolver = new DiscussionMacroResolver();
        discussionMacroResolver.EnableBold = forumInfo.ForumEnableFontBold;
        discussionMacroResolver.EnableItalics = forumInfo.ForumEnableFontItalics;
        discussionMacroResolver.EnableStrikeThrough = forumInfo.ForumEnableFontStrike;
        discussionMacroResolver.EnableUnderline = forumInfo.ForumEnableFontUnderline;
        discussionMacroResolver.EnableCode = forumInfo.ForumEnableCodeSnippet;
        discussionMacroResolver.EnableColor = forumInfo.ForumEnableFontColor;
        discussionMacroResolver.EnableImage = forumInfo.ForumEnableImage || forumInfo.ForumEnableAdvancedImage;
        discussionMacroResolver.EnableQuote = forumInfo.ForumEnableQuote;
        discussionMacroResolver.EnableURL = forumInfo.ForumEnableURL || forumInfo.ForumEnableAdvancedURL;
        discussionMacroResolver.MaxImageSideSize = forumInfo.ForumImageMaxSideSize;
        discussionMacroResolver.QuotePostText = ResHelper.GetString("DiscussionMacroResolver.QuotePostText", (string)null, true);
        discussionMacroResolver.UseNoFollowForLinks = HTMLHelper.UseNoFollowForUsersLinks(SiteContext.CurrentSiteName);
        if (forumInfo.ForumHTMLEditor)
        {
            discussionMacroResolver.EncodeText = false;
            discussionMacroResolver.ConvertLineBreaksToHTML = false;
        }
        else
        {
            discussionMacroResolver.EncodeText = true;
            discussionMacroResolver.ConvertLineBreaksToHTML = true;
        }
        return discussionMacroResolver.ResolveMacros(@string);
    }
0 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on November 30, 2016 19:10 (last edited on November 30, 2016 19:19)

Hey Zachary,

Where exactly does that code get inserted?

We do use the WYSIWYG to post, shown here: Image and video hosting by TinyPic

It formats properly in the actual forum, just not in an Outlook email.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on November 30, 2016 19:18 (last edited on November 30, 2016 19:20)

Create a custom macro Then just use that macro in your email template.

So your macro would be {%FormatPostText(posttext,foruminfo)%}

You might not be able to pass in the forumInfo, you might have to pass in the forumId or something and in your macro get the forumInfo. You can try ForumContext.CurrentForum and see if it passes in the ForumInfo

0 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on November 30, 2016 19:20

I'll give that a shot, thanks Zachary.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on November 30, 2016 19:24

The code I posted is used somewhere(can't remember exactly where) that Kentico uses to format the text before displaying it in the forum.

0 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on December 1, 2016 21:22 (last edited on December 1, 2016 22:09)

Hey Zachary,

Is there supposed to be a [MacroMethodParam] and am I not supposed to define the method as a static object (public static object FormatPostText(EvaluationContext context, params object[] parameters)) as cited in the example? Could you provide a bit more insight on the method signature and definition that needs to be in place before this can run? I'm not able to get this going with the combined example. There's a bit of a discrepancy with this and applying that to your coding example.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on December 1, 2016 22:21 (last edited on December 10, 2019 02:30)

Should be able to use this. Then to call your macro you can just type {% ForumContext.FormatPostText(PostTest, ForumID) |(identity)GlobalAdministrator%}

using System;
using CMS; 
using CMS.Forums; 
using CMS.Helpers;
using CMS.MacroEngine;
using CMS.SiteProvider;
[assembly: RegisterExtension(typeof(ForumCustomMacroMethds), typeof(ForumContext))]

public class ForumCustomMacroMethds : MacroMethodContainer
{

    [MacroMethod(typeof(string), "Formats for forum Post Info", 1)]
    [MacroMethodParam(0, "PostText", typeof(string), "PostText")]
    [MacroMethodParam(1, "ForumID", typeof(int), "ForumID")]
    public static object FormatPostText(EvaluationContext context, params object[] parameters)
    {
        // Branches according to the number of the method's parameters
        switch (parameters.Length)
        {
            case 3:
                // Overload with two parameters
                return FormatPostText(ValidationHelper.GetString(parameters[1], ""),
                    ValidationHelper.GetInteger(parameters[2], 0));

            default:
                // No other overloads are supported
                throw new NotSupportedException();
        }
    }

    private static object FormatPostText(string postText,int forumId)
    {
        var forumInfo = ForumInfoProvider.GetForumInfo(forumId);
        if (forumInfo != null)
        {
            string @string = ValidationHelper.GetString(postText, "", (System.Globalization.CultureInfo) null);
            DiscussionMacroResolver discussionMacroResolver = new DiscussionMacroResolver();
            discussionMacroResolver.EnableBold = forumInfo.ForumEnableFontBold;
            discussionMacroResolver.EnableItalics = forumInfo.ForumEnableFontItalics;
            discussionMacroResolver.EnableStrikeThrough = forumInfo.ForumEnableFontStrike;
            discussionMacroResolver.EnableUnderline = forumInfo.ForumEnableFontUnderline;
            discussionMacroResolver.EnableCode = forumInfo.ForumEnableCodeSnippet;
            discussionMacroResolver.EnableColor = forumInfo.ForumEnableFontColor;
            discussionMacroResolver.EnableImage = forumInfo.ForumEnableImage || forumInfo.ForumEnableAdvancedImage;
            discussionMacroResolver.EnableQuote = forumInfo.ForumEnableQuote;
            discussionMacroResolver.EnableURL = forumInfo.ForumEnableURL || forumInfo.ForumEnableAdvancedURL;
            discussionMacroResolver.MaxImageSideSize = forumInfo.ForumImageMaxSideSize;
            discussionMacroResolver.QuotePostText = ResHelper.GetString("DiscussionMacroResolver.QuotePostText",
                (string) null, true);
            discussionMacroResolver.UseNoFollowForLinks =
                HTMLHelper.UseNoFollowForUsersLinks(SiteContext.CurrentSiteName);
            if (forumInfo.ForumHTMLEditor)
            {
                discussionMacroResolver.EncodeText = false;
                discussionMacroResolver.ConvertLineBreaksToHTML = false;
            }
            else
            {
                discussionMacroResolver.EncodeText = true;
                discussionMacroResolver.ConvertLineBreaksToHTML = true;
            }
            return discussionMacroResolver.ResolveMacros(@string);
        }
        return postText;

    }
}
0 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on December 2, 2016 17:39

Thanks for that Zachary.

Unfortunately, it behaves the same way it did when it was just {%posttext%}, without StripTags and no |(encode)false.

Basically this: 1. Is fixing the naming issue worth no longer having your default RDP settings apply? 2. Would auto-sharing (by default) your clipboard with the RDP session be a bad thing, or a good thing? 3. Should we enable multi-monitor support be default? (for some interesting reading on that, click here) Thanks! 1. Here's hoping it's not one or the other. I wouldn't mind that it's not just the default settings. 2. Good Thing. Having this forced off would be an absolute deal breaker. 3. Not sure. It's rare that we'd use rdp for anything other than servers, so I'm not sure what multi-monitor support with RDP is actually like.

But with HTML tags in it, without italics, quotes and the regular BB encoded stuff you would see on a Kentico-based forum.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on December 2, 2016 21:42

So it has the HTML tags in the source? But it isn't styling them in the email? Then your email needs to include the css that styles it.

0 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on December 2, 2016 23:05

It has the <br /> tags in it that would normally space out the text lines. No other tags, sorry I didn't specify. I'll look into applying the style that we use in the forum onto the e-mail template. Thanks.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on December 5, 2016 15:44

Are the
tags encoded?

Instead of |(encode)false, have you tried |(encode)true ?

0 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on December 5, 2016 15:53

{%posttext|(encode)true%} gives the same result as {%posttext%}, giant blob of text with line break tags that space the ordered list in the original post.

0 votesVote for this answer Mark as a Correct answer

Sam Mueller answered on December 5, 2016 17:18

I really think the underlying issue is that the BB code used in the forums is not being recognized/transferred to the email. I even tried {% ResolveBBCode(posttext) %} and all that did was double the amount of <br /> tags in the block.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.