Jonathas Costa
Jonathas Costa

Reputation: 1016

How to transform a Kentico Email Template without sending it?

My Kentico server is unable to send e-mails, so I have to transform my e-mail using MacroResolver, but send it using some other way.

var clients = new List<Client>();

var macroResolver = MacroResolver.GetInstance();
macroResolver.AddDynamicParameter("clients", clients);

var emailMessage = new EmailMessage { 
    From = "[email protected]", 
    Recipients = "[email protected]", 
    Subject = "Whatever" 
};

var template = EmailTemplateProvider.GetEmailTemplate(templateName, siteName);

EmailSender.SendEmailWithTemplateText(siteName, emailMessage, template, macroResolver, true);

In other words, I would like to use Kentico just as a Template Engine. Is there anyway to achieve this?

Upvotes: 1

Views: 814

Answers (1)

mivra
mivra

Reputation: 1400

What SendEmailWithTemplateText method basically does is it fills empty fields of message by its equivalent from a template and resolve macro values in it. If you are only after message body, then you can create the email message by:

emailMessage.Body = macroResolver.ResolveMacros(emailMessage.Body);
emailMessage.PlainTextBody = macroResolver.ResolveMacros(emailMessage.PlainTextBody);

For most scenarios it's also better to tell the resolver to encode resolved values. You can do it by: resolver.EncodeResolvedValues = true;

Also you are passing whole 'clients' collection to the resolver. You'll probably need to take it one by one and generate emails in a loop.

Upvotes: 1

Related Questions