Kentico CMS 6.0 Developer's Guide

Managing email templates

Managing email templates

Previous topic Next topic Mail us feedback on this topic!  

Managing email templates

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a subscription template.

 

private void CreateSubscriptionTemplate()
{
  // Create new subscription template object
  EmailTemplate newTemplate = new EmailTemplate();

 
  // Set the properties
   newTemplate.TemplateDisplayName = "My new subscription template";
   newTemplate.TemplateName = "MyNewSubscriptionTemplate";
   newTemplate.TemplateType = EmailTemplateType.Subscription;
   newTemplate.TemplateBody = "My new subscription template body";
   newTemplate.TemplateHeader = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Newsletter</title><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /></head><body>";
   newTemplate.TemplateFooter = "</body></html>";
   newTemplate.TemplateSiteID = CMSContext.CurrentSiteID;

 
  // Save the subscription template
  EmailTemplateProvider.SetEmailTemplate(newTemplate);
}

 

The following example creates an unsubscription template.

 

private void CreateUnsubscriptionTemplate()
{
  // Create new unsubscription template object
  EmailTemplate newTemplate = new EmailTemplate();

 
  // Set the properties
   newTemplate.TemplateDisplayName = "My new unsubscription template";
   newTemplate.TemplateName = "MyNewUnsubscriptionTemplate";
   newTemplate.TemplateType = EmailTemplateType.Unsubscription;
   newTemplate.TemplateBody = "My new unsubscription template body";
   newTemplate.TemplateHeader = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Newsletter</title><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /></head><body>";
   newTemplate.TemplateFooter = "</body></html>";
   newTemplate.TemplateSiteID = CMSContext.CurrentSiteID;

 
  // Save the unsubscription template
  EmailTemplateProvider.SetEmailTemplate(newTemplate);
}

 

The following example creates an issue template.

 

private void CreateIssueTemplate()
{
  // Create new issue template object
  EmailTemplate newTemplate = new EmailTemplate();

 
  // Set the properties
   newTemplate.TemplateDisplayName = "My new issue template";
   newTemplate.TemplateName = "MyNewIssueTemplate";
   newTemplate.TemplateType = EmailTemplateType.Issue;
   newTemplate.TemplateBody = "<p>My new issue template body</p><p>$$content:800:600$$</p>";
   newTemplate.TemplateHeader = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Newsletter< title><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /></head><body>";
   newTemplate.TemplateFooter = "</body></html>";
   newTemplate.TemplateSiteID = CMSContext.CurrentSiteID;

 
  // Save the issue template
  EmailTemplateProvider.SetEmailTemplate(newTemplate);
}

 

The following example gets and updates an issue template.

 

private bool GetAndUpdateIssueTemplate()
{
  // Get the issue template
  EmailTemplate updateTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewIssueTemplate", CMSContext.CurrentSiteID);
  if (updateTemplate != null)
   {
      // Update the properties
       updateTemplate.TemplateDisplayName = updateTemplate.TemplateDisplayName.ToLower();

 
      // Save the changes
      EmailTemplateProvider.SetEmailTemplate(updateTemplate);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates issue templates.

 

private bool GetAndBulkUpdateIssueTemplates()
{
  // Prepare the parameters
  string where = "TemplateName LIKE N'MyNewIssueTemplate%'";

 
  // Get the data
  DataSet templates = EmailTemplateProvider.GetEmailTemplates(where, null);
  if (!DataHelper.DataSourceIsEmpty(templates))
   {
      // Loop through the individual items
      foreach (DataRow templateDr in templates.Tables[0].Rows)
       {
          // Create object from DataRow
          EmailTemplate modifyTemplate = new EmailTemplate(templateDr);

 
          // Update the properties
           modifyTemplate.TemplateDisplayName = modifyTemplate.TemplateDisplayName.ToUpper();

 
          // Save the changes
          EmailTemplateProvider.SetEmailTemplate(modifyTemplate);
       }

 
      return true;
   }

 
  return false;
}

 

The following example deletes a subscription template.

 

private bool DeleteSubscriptionTemplate()
{
  // Get the subscription template
  EmailTemplate deleteTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewSubscriptionTemplate", CMSContext.CurrentSiteID);

 
  // Delete the subscription template
  EmailTemplateProvider.DeleteEmailTemplate(deleteTemplate);

 
  return (deleteTemplate != null);
}

 

The following example deletes an unsubscription template.

 

private bool DeleteUnsubscriptionTemplate()
{
  // Get the unsubscription template
  EmailTemplate deleteTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewUnsubscriptionTemplate", CMSContext.CurrentSiteID);

 
  // Delete the unsubscription template
  EmailTemplateProvider.DeleteEmailTemplate(deleteTemplate);

 
  return (deleteTemplate != null);
}

 

The following example deletes an issue template.

 

private bool DeleteIssueTemplate()
{
  // Get the issue template
  EmailTemplate deleteTemplate = EmailTemplateProvider.GetEmailTemplate("MyNewIssueTemplate", CMSContext.CurrentSiteID);

 
  // Delete the issue template
  EmailTemplateProvider.DeleteEmailTemplate(deleteTemplate);

 
  return (deleteTemplate != null);
}