Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Fix for Community Server Contact Form Email Line Break Constipation

Since upgrading to CS2007.1 my Contact Form line breaks no longer existed.  Do yours?  There were some threads about it on CS Forums.  I reported on them and even applied the patch (updating Globals.cs NewLine constant), but no go in Vermont.  So I took a deeper look into the flow of a text message from my site's contact form to my Outlook inbox and here's the fix I came up with.  There may be another fix of which I'm unaware, or maybe there was never a problem in the first place, but all I know is that I now have HTML formatted contact form email with line breaks and everything.  I received a site email this morning, about my blog list popup mod, and it was sweet to see line breaks again.

So the comment text flows straight from the Contact Form's Chameleon ContactForm class in CS.Blogs to the CS.MailRoom.CommonEmailProvider.QueueContactRequest() method.  The text at that point contains \r\n's as line break characters from the textbox ASPNET control.  The CS EmailTemplateProvider contains two static methods, FormatPlainHtmlAsText and FormatPlainTextAsHtml which do a lot of the Regex-to-Unicode work, and are very cool in what they do, but we can't get there from here, as the two methods are protected within the provider class.  But our requirements are the same: regex in, HTML out. 

The changes I made in the CS.MailRoom.CommonEmails.QueueContactRequest() method are in bold below.

 

body = Regex.Replace(body, "\n", "<br />",
    RegexOptions.IgnoreCase | RegexOptions.Compiled);
body = body.Replace("\r", string.Empty);

tokens.Add(new EmailTextToken("AppTitle", appName));
tokens.Add(new EmailTextToken("Body", body));
...

tokens.Add(new EmailTextToken("Name", submittedName));

email.ProcessTokens(tokens);
email.ParseTemplate();

email.To.Add(to);
email.From = new MailAddress(from);

email.IsBodyHtml = true;
Emails.QueueMessage(email);

 

That revitalizes the email body's shapely figure, but there's still the remaining <body /> area of the ContactForm Email Template in emails.xml.  The dashed line and "This email sent..." text.  That change is below where you'll notice the HTML code around the areas of the template body.



Line breaks.  Didn't know how much I'd miss'em.

Comments (8) | Post RSS RSS comment feed

Posted on 12/15/2007 1:06:25 PM by Dave Burke
Categories: .NET | Community Server

Related posts

Comments

Comments are closed

Copyright © 2008 Dave Burke Consulting  |  All Rights reserved.