Sending email using templates by Kentico 5 CMS email engine API

First of all let's create new email template

Then let's add some content and placeholders to it

Now in our custom code we can add next method

private void SendEmail()

{

   // let's get our email template

 EmailTemplateInfo emailTemplate =

                   EmailTemplateProvider.GetEmailTemplate

                   ("MyModuleName.TestEmailTemplate",CMSContext.CurrentSiteName);

 

 if (emailTemplate == null)

 {

     // TODO: Log error

     // (check my post how to use Kentico Event log API)         

 }

  else

  {

    // set vars which we are going to use in email template

    string firstName = "John";

    string lastName = "Locke";

 

    // replace email template placeholders with real values

    string[,] replacements = new string[2, 2];

    replacements[0, 0] = "firstname";

    replacements[0, 1] = firstName;

    replacements[1, 0] = "lastname";

    replacements[1, 1] = lastName;

 

    // Set resolver

    ContextResolver resolver = CMSContext.CurrentResolver;

    resolver.SourceParameters = replacements;

 

    // Enable macro encoding for body

    resolver.EncodeResolvedValues = true;

 

    EmailMessage message = new EmailMessage();

    string fromEmailAddress = "some@from-email.com";

    string toEmailAddress = "some@to-email.com";

 

    // you can set here which template to use html

    // or plain text or default (which has been setup in Settings)

    message.EmailFormat = EmailFormatEnum.Default;

 

    message.From = EmailHelper.GetSender(emailTemplate, fromEmailAddress);

    message.Recipients = toEmailAddress;

    message.Subject = emailTemplate.TemplateSubject;

 

    message.Body = resolver.ResolveMacros(emailTemplate.TemplateText);

    // Disable macro encoding for plaintext body and subject

    resolver.EncodeResolvedValues = false;                       

    message.PlainTextBody = resolver.ResolveMacros(emailTemplate.TemplatePlainText);

 

    message.CcRecipients = emailTemplate.TemplateCc;

    message.BccRecipients = emailTemplate.TemplateBcc;

 

   try

   {

     // Attach template meta-files to e-mail

     MetaFileInfoProvider.ResolveMetaFileImages(message,

                                                emailTemplate.TemplateID,

                                               EmailObjectType.EMAILTEMPLATE,

                                                  MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE);

     EmailSender.SendEmail(CMSContext.CurrentSiteName, message);

   }

   catch

   }

      // TODO: Log error

      // (check my post how to use Kentico Event log API)     

   }

 }

}


Jobs done :)

Posted by paxer Wed, 24 Feb 2010 13:46:00 GMT


Comments

Leave a response

  1. Avatar
    Ron Deijkers about 1 year later:

    The } just after the last catch should be {

  2. Avatar
    ainol novo 10 eternal over 3 years later:

    nice post, I can see your point, something different from other guy’s, please keep updating.

Leave a comment