Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > ResolveMacros - macros not resolved View modes: 
User avatar
Member
Member
Punt - 10/1/2013 6:41:08 AM
   
ResolveMacros - macros not resolved
I try to send an e-mail using a template in Kentico. The mail is sent correctly but the macros in the template are not resolved. resolver.ResolveMacros(template.TemplateText) returns "".

This is the method:

private void SendEmailUsingTemplate(string recipientEmail, string[,] replacements, string eventName)
{
// Set resolver
ContextResolver resolver = CMSContext.CurrentResolver;
resolver.SourceParameters = replacements;

// Get the email template
var template = EmailTemplateProvider.GetEmailTemplate("MembershipTest-RegistrationConfirmation", _applicationSettings.KenticoSiteName);

if (template != null)
{
// Email message
var emailMessage = new EmailMessage
{
EmailFormat = EmailFormatEnum.Default,
Recipients = recipientEmail,
From = EmailHelper.GetSender(template, SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSNoreplyEmailAddress")),
CcRecipients = template.TemplateCc,
BccRecipients = template.TemplateBcc,
Subject = resolver.ResolveMacros(template.TemplateSubject),
PlainTextBody = resolver.ResolveMacros(template.TemplatePlainText)
};

// Enable macro encoding for body
resolver.EncodeResolvedValues = true;

emailMessage.Body = resolver.ResolveMacros(template.TemplateText);

// Disable macro encoding for plaintext body and subject
resolver.EncodeResolvedValues = false;

try
{
MetaFileInfoProvider.ResolveMetaFileImages(emailMessage, template.TemplateID, EmailObjectType.EMAILTEMPLATE, MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE);
// Send the e-mail immediately
EmailSender.SendEmail(CMSContext.CurrentSiteName, emailMessage, true);
}
catch (Exception ex)
{
var eventLogProvider = new EventLogProvider();
eventLogProvider.LogEvent("E", eventName, ex);

throw;
}
}
}


And this is the method call:

string[,] replacements = new string[1, 2];
replacements[0, 0] = "username";
replacements[0, 1] = username;

SendEmailUsingTemplate("eapeldoorn@snelstart.nl", replacements, "Test - Sending Emails");


Mailtemplate only contains the following:
{%username%}

Does someone have any idea?

User avatar
Certified Developer 13
Certified Developer 13
Sunil - 10/1/2013 7:49:58 AM
   
RE:ResolveMacros - macros not resolved

Try to use SendEmailWithTemplateText() function which is in EmailSender class. There are other functions also as needed.

User avatar
Member
Member
Punt - 10/2/2013 7:58:10 AM
   
RE:ResolveMacros - macros not resolved
Thanks, that worked. My solution does also work. I was missing a reference to CMS.PortalEngine.