Creating Custom Email / Message Templates

10 years ago
I want to create my own email templates to support my custom features. Can I do this? If not, can someone suggest a workaround.

Thanks!
10 years ago
bump.  I'm wondering the same thing!  This would be incredibly helpful.
10 years ago
If you've got the source code, then it's easy to add another function that mimics e.g. MessageManager.SendCustomerPasswordRecoveryMessage, but uses a different template name. And then use the Administration UI to author the email template for that name.

In fact, MessageManager looks ripe for a Refactoring. A lot of the routines follow exactly the same structure and perhaps a function as per below would be worth including in the next build?


        /// <summary>
        /// Sends generic message to a customer, using a specific template
        /// </summary>
        /// <param name="customer">Customer instance</param>
        /// <param name="languageId">Message language identifier</param>
        /// <param name="templateName">Name of template to use</param>
        /// <returns>Queued email identifier</returns>
        public static int SendMessageFromTemplate(Customer customer, int languageId, string templateName)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");
            if(string.IsNullOrEmpty(templateName))
                throw new ArgumentNullException("templateName");

            var localizedMessageTemplate = MessageManager.GetLocalizedMessageTemplate(templateName, languageId);
            if (localizedMessageTemplate == null || !localizedMessageTemplate.IsActive)
                return 0;

            var emailAccount = localizedMessageTemplate.EmailAccount;

            string subject = ReplaceMessageTemplateTokens(customer, localizedMessageTemplate.Subject);
            string body = ReplaceMessageTemplateTokens(customer, localizedMessageTemplate.Body);
            string bcc = localizedMessageTemplate.BccEmailAddresses;
            var from = new MailAddress(emailAccount.Email, emailAccount.DisplayName);
            var to = new MailAddress(customer.Email, customer.FullName);
            var queuedEmail = InsertQueuedEmail(5, from, to, string.Empty, bcc, subject, body,
                DateTime.UtcNow, 0, null, emailAccount.EmailAccountId);
            return queuedEmail.QueuedEmailId;
        }
10 years ago
Try http://www.stringtemplate.org/

I have been using String Template and it has many features. You can pass collections into and it formats templates accordingly.