Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Custom Email Templates View modes: 
User avatar
Member
Member
Armysniper89 - 3/26/2013 10:45:09 AM
   
Custom Email Templates
I am moving a module I created in another CMS into Kentico as a module. Well right now it is already in Kentico as Custom Tables but I digress...in my module I have my own custom email template engine that has worked for years. I am looking to use Kentico now and its email template capabilities. My main question is, how do I create a custom email template for my module and how can I get access to the fields of a custom table?

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 3/26/2013 4:13:00 PM
   
RE:Custom Email Templates
First you need to create an email template (CMSSiteManager>Administration>Email Templates) for the site you are working with, or create a global one, doesn't matter I guess. Next in code you can send an email with the newly created template using this code:
EmailMessage msg = new CMS.EmailEngine.EmailMessage();
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("EmailTemplateName", CMSContext.CurrentSiteID);

if (eti != null)
{
// Macros can be resolved, we will have to define them per template
MacroResolver mcr = MacroResolver.GetInstance();
mcr.SpecialMacros = new String[,] { { "", "" } }; // resolve macros if needed

msg.EmailFormat = EmailFormatEnum.Both;
msg.From = eti.TemplateFrom;
msg.Recipients = user.Email;
msg.Subject = eti.TemplateSubject;
msg.Body = eti.TemplateText;
msg.PlainTextBody = eti.TemplatePlainText;

// send the email to the queue
EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, msg, eti, mcr, false);
ReturnValue = true;
}
To access a custom table via API, you can follow the instructions here.

User avatar
Kentico Support
Kentico Support
kentico_filipl - 3/27/2013 5:56:02 AM
   
RE:Custom Email Templates
Hello,

Email templates can be created the way it is mentioned in the post above (thanks FroggEye). As for the question regarding access to fields of a custom table - you can do that in code behind using TreeNode's GetValue(string columnName) method.

Example could look like this:
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
TreeNode node = tree.SelectSingleNode(1900, CMSContext.CurrentPageInfo.DocumentCulture);
TextBoxExample.Text = node.GetValue("TestColumn").ToString();

where parameters for SelectSingleNode() method are:
TreeNode SelectSingleNode(int nodeID, string cultureCode)

Complete documentation can be found in API Reference guide.

Best regards,
Filip Ligac

User avatar
Member
Member
Armysniper89 - 4/1/2013 10:44:39 PM
   
RE:Custom Email Templates
I am looking for how to do access the custom table fields in the Email template module not the code behind. So when I am entering the HTML for the template, that is where I want to access the custom table fields.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 4/1/2013 11:05:46 PM
   
RE:Custom Email Templates
Hi,

Have you tried creating the macros and then just resolve them in the code and send the e-mail with given template using the API? IT is basically the same as suggested above - I think this is the best and easiest way since otherwise how the e-mail template will know abvout your custom columns in the custom table? You need to dsefine them somewhere - in the code behind.

E.g just like it is used in our UI files (\CMSModules\Membership\Pages\Users\User_Edit_Password.aspx.cs):
// Get e-mail template
EmailTemplateInfo template = EmailTemplateProvider.GetEmailTemplate(templateName, null);
if (template != null)
{
em.Body = template.TemplateText;

// Macros
string[,] macros = new string[2, 2];
macros[0, 0] = "UserName"; //this is the username macro used in the template
macros[0, 1] = ui.UserName;
macros[1, 0] = "Password"; //this is the pwd macro used in the template
macros[1, 1] = pswd;
// Create macro resolver
ContextResolver resolver = CMSContext.CurrentResolver;
resolver.SourceParameters = macros;

// Add template attachments
MetaFileInfoProvider.ResolveMetaFileImages(em, template.TemplateID, EmailObjectType.EMAILTEMPLATE, MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE);
// Send message immediately (+ resolve macros)
EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, em, template, resolver, true);

Best regards,
Juraj Ondrus

User avatar
Member
Member
Armysniper89 - 4/1/2013 11:28:12 PM
   
RE:Custom Email Templates
I havent tried anything. ;) I am just trying to figure out what I need to do before I start doing it. I looked at email templates that are already in the system and I see things like this:
<html>
<head>
</head>
<body>
<p>Thank you for registering at Great Giving. You can find your credentials below:<br />
<br />
Username: [color="yellow"]{%username%}[/color]<br />
Password: {%password%}<br />
</body>
</html>

When I look at the code in YELLOW, I am trying to figure out how I can create an email template in the module and then access fields in my custom table. Or lets say custom fields that I have added to registration. As an example. How would I do this?

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 4/1/2013 11:35:01 PM
   
RE:Custom Email Templates
Hi,

That is exacly what I described in my post above and also FroggEye:
// Macros
string[,] macros = new string[2, 2];
macros[0, 0] = "UserName"; //this is the username macro used in the template
macros[0, 1] = ui.UserName;
macros[1, 0] = "Password"; //this is the pwd macro used in the template
macros[1, 1] = pswd;
// Create macro resolver
ContextResolver resolver = CMSContext.CurrentResolver;
resolver.SourceParameters = macros;

As you can see, the macro is defined in the code behind. In the same way you need to define your macros you want to use.

Best regards,
Juraj Ondrus

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 4/1/2013 11:36:41 PM
   
RE:Custom Email Templates
Hi,

That is exacly what I described in my post above and also FroggEye:
// Macros
string[,] macros = new string[2, 2];
macros[0, 0] = "UserName"; //this is the username macro used in the template
macros[0, 1] = ui.UserName;
macros[1, 0] = "Password"; //this is the pwd macro used in the template
macros[1, 1] = pswd;
// Create macro resolver
ContextResolver resolver = CMSContext.CurrentResolver;
resolver.SourceParameters = macros;

As you can see, the macro is defined in the code behind. In the same way you need to define your macros you want to use.

Best regards,
Juraj Ondrus

User avatar
Member
Member
Armysniper89 - 4/2/2013 1:15:59 AM
   
RE:Custom Email Templates
What about the Email Type field. Is there a way to get my custom module in that list? What is the purpose of this field?

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 4/2/2013 8:00:12 AM
   
RE:Custom Email Templates
I've got a post here about that. It will require some work.

User avatar
Member
Member
Armysniper89 - 6/7/2013 1:42:30 PM
   
RE:Custom Email Templates
Thanks, that makes sense. BTW: Does this article: Using Kentico's Email Templates still work? It is written in 2010. Were there any changes since 5.5R2 that would effect this code?

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 6/9/2013 7:32:19 PM
   
RE:Custom Email Templates
Off the top of my head (without looking at any code) I believe there would be a few changes but a simple copy and paste would almost immediately tell you what the errors would be.

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 6/10/2013 8:14:38 AM
   
RE:Custom Email Templates
Hi,

Yes, in general the principles are still the same, however if you will met any issues, I would recommend you to take a look at the Martin's blog post: Changes in the API from version Kentico CMS 5.5 R2 to 6 as well as the Kentico CMS API Reference.

Best regards,
Martin Danko