Kentico CMS 7.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()
{
  // Creates a new subscription template object
  EmailTemplateInfo newTemplate = new EmailTemplateInfo();

 
  // Sets 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;

 
  // Saves the subscription template
  EmailTemplateInfoProvider.SetEmailTemplateInfo(newTemplate);
}

 

The following example creates an unsubscription template.

 

private void CreateUnsubscriptionTemplate()
{
  // Creates a new unsubscription template object
  EmailTemplateInfo newTemplate = new EmailTemplateInfo();

 
  // Sets 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;

 
  // Saves the unsubscription template
  EmailTemplateInfoProvider.SetEmailTemplateInfo(newTemplate);
}

 

The following example creates an issue template.

 

private void CreateIssueTemplate()
{
  // Creates a new issue template object
  EmailTemplateInfo newTemplate = new EmailTemplateInfo();

 
  // Sets 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;

 
  // Saves the issue template
  EmailTemplateInfoProvider.SetEmailTemplateInfo(newTemplate);
}

 

The following example gets and updates an issue template.

 

private bool GetAndUpdateIssueTemplate()
{
  // Gets the issue template
  EmailTemplateInfo updateTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("MyNewIssueTemplate", CMSContext.CurrentSiteID);
  if (updateTemplate != null)
   {
      // Updates the properties
       updateTemplate.TemplateDisplayName = updateTemplate.TemplateDisplayName.ToLower();

 
      // Saves the changes
      EmailTemplateInfoProvider.SetEmailTemplateInfo(updateTemplate);

 
      return true;
   }

 
  return false;
}

 

The following example gets and bulk updates issue templates.

 

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

 
  // Gets the data
  DataSet templates = EmailTemplateInfoProvider.GetEmailTemplates(where, null);
  if (!DataHelper.DataSourceIsEmpty(templates))
   {
      // Loops through individual items
      foreach (DataRow templateDr in templates.Tables[0].Rows)
       {
          // Creates an object from the DataRow
          EmailTemplateInfo modifyTemplate = new EmailTemplateInfo(templateDr);

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

 
          // Saves the changes
          EmailTemplateInfoProvider.SetEmailTemplateInfo(modifyTemplate);
       }

 
      return true;
   }

 
  return false;
}

 

The following example deletes a subscription template.

 

private bool DeleteSubscriptionTemplate()
{
  // Gets the subscription template
  EmailTemplateInfo deleteTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("MyNewSubscriptionTemplate", CMSContext.CurrentSiteID);

 
  // Deletes the subscription template
  EmailTemplateInfoProvider.DeleteEmailTemplateInfo(deleteTemplate);

 
  return (deleteTemplate != null);
}

 

The following example deletes an unsubscription template.

 

private bool DeleteUnsubscriptionTemplate()
{
  // Gets the unsubscription template
  EmailTemplateInfo deleteTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("MyNewUnsubscriptionTemplate", CMSContext.CurrentSiteID);

 
  // Deletes the unsubscription template
  EmailTemplateInfoProvider.DeleteEmailTemplateInfo(deleteTemplate);

 
  return (deleteTemplate != null);
}

 

The following example deletes an issue template.

 

private bool DeleteIssueTemplate()
{
  // Gets the issue template
  EmailTemplateInfo deleteTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("MyNewIssueTemplate", CMSContext.CurrentSiteID);

 
  // Deletes the issue template
  EmailTemplateInfoProvider.DeleteEmailTemplateInfo(deleteTemplate);

 
  return (deleteTemplate != null);
}