Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Possible to preserve string line breaks in EmailSender.SendEmail? View modes: 
User avatar
Member
Member
joeh42 - 8/19/2011 9:48:54 AM
   
Possible to preserve string line breaks in EmailSender.SendEmail?
I have an HTML text area in which users can use to email another individual. I want to preserve line breaks that users enter into the text area in the email that is sent, for both HTML and plain text emails. It seems as though Kentico may be dropping the line breaks, and HTML encoding the string (and ignoring the tags I insert).

Is there a way to preserve the line breaks?

I can see the \n characters in the string, but I've tried replacing them with <br />s, HTML encoding strings, wrapping the text in a <pre> tag, etc.

Is this possible?

        template = EmailTemplateProvider.GetEmailTemplate("SurroundHealthReferAFriend", CMSContext.CurrentSiteName);
replacements = new string[4, 2];
replacements[0, 0] = "message";
replacements[0, 1] = txtMessage.Text;
...
var resolver = CMSContext.CurrentResolver;
resolver.SourceParameters = replacements;
resolver.EncodeResolvedValues = true;

// Email message
email.EmailFormat = EmailFormatEnum.Default;
email.Recipients = txtEmail.Text;

email.From = template.TemplateFrom;
email.Body = resolver.ResolveMacros(template.TemplateText);

resolver.EncodeResolvedValues = false;
email.PlainTextBody = resolver.ResolveMacros(template.TemplatePlainText);

email.CcRecipients = template.TemplateCc;
email.BccRecipients = template.TemplateBcc;

EmailSender.SendEmail(CMSContext.CurrentSiteName, email, true);


Thanks,
Joe Hoppe

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/20/2011 8:45:31 AM
   
RE:Possible to preserve string line breaks in EmailSender.SendEmail?
Hi,

the html text area is a part where users can inserts their own html body and then send the email?

Or the imail is send based on template you have created?

To send an emial from html text area you could use the following code:


// Prepare the e-mail message
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtFrom.Text);

// Recipients
string[] to = txtTo.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in to)
{
msg.To.Add(new MailAddress(s));
}

// Subject
msg.Subject = TextHelper.LimitLength(txtSubject.Text.Trim(), 450);
msg.Body = txtText.Text;
msg.IsBodyHtml = true;


In case you would like to send an email based on template text, please take a look at below example. There you can use SendEmailWithTemplateText method:


EmailMessage msg = new CMS.EmailEngine.EmailMessage();
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("templateCodeName", CMSContext.CurrentSiteID);

MacroResolver mcr = new MacroResolver();
mcr.SpecialMacros = new String[,] {{"{#macro#}", "text"}} ; //here you can specify text for multiple macros specified in template

msg.EmailFormat = EmailFormatEnum.Both;
msg.From = eti.TemplateFrom;
msg.Recipients = "user@domain.com";
msg.Subject = eti.TemplateSubject;

EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, msg, eti, mcr, true );


Best regards,
Ivana Tomanickova


User avatar
Member
Member
joeh42 - 8/23/2011 8:20:07 AM
   
RE:Possible to preserve string line breaks in EmailSender.SendEmail?
Hi Ivana,

More specifically, I'm trying to use a replacements array in to fill an email template. I want to preserve line breaks that are in the strings in the replacement array (the strings are coming from an HTML text area, so there are line breaks in them).

When I use SendEmailWithTemplateText, Kentico it seems like Kentico is HTML encoding the replacement strings and dropping the line breaks. Also, since it's HTML encoded, and I cannot insert <p> tags to make line breaks. Is there a way to insert line breaks using a replacements array in SendEmailWithTemplateText?

Thanks,
Joe

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/24/2011 2:36:11 AM
   
RE:Possible to preserve string line breaks in EmailSender.SendEmail?
Hi,

I tried to use the above code with replacement array:

mcr.SpecialMacros = new String[,] { { "{#htmlpart#}", "first<br />second" }, { "{#plaintextpart#}", "first\r\n second" } };

Then inserted the macros to the template (Site Manager - Development - Email templates - Corporate site in the dropdown list - mytemplate):

HTML version: {#htmlpart#}
Plain text version: {#plaintextpart#}

The result was following in the html and plain text version of the email:

first
second

Is this requested behavior?

Best regards,
Ivana Tomanickova