Пример #1
0
        /// <summary>
        /// Loads a named email template from database and replaces tokens with passed entities, and returns a new email message object with template values
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        private EmailMessage LoadAndProcessTemplate(string templateName, object model = null)
        {
            //first load the template from database
            var template = _emailTemplateService.FirstOrDefault(x => x.TemplateSystemName == templateName);

            if (template == null)
            {
                return(null);
            }

            var content = _emailTemplateService.GetProcessedContentTemplate(template);

            //render the content
            //expand the routes first
            content = Expander.ExpandRoutes(content, model);
            var processedTemplateString = _viewAccountant.RenderView(templateName, content, model);
            var subjectString           = _viewAccountant.RenderView($"{templateName}:Subject", template.Subject, model);
            var emailAccount            = template.EmailAccount;
            //create a new email message
            var emailMessage = new EmailMessage()
            {
                IsEmailBodyHtml       = true,
                EmailBody             = processedTemplateString,
                EmailAccount          = emailAccount,
                EmailAccountId        = emailAccount?.Id ?? 0,
                Subject               = subjectString,
                OriginalEmailTemplate = template,
                SendingDate           = DateTime.UtcNow,
                Tos = new List <EmailMessage.UserInfo>()
            };

            return(emailMessage);
        }
Пример #2
0
        /// <summary>
        /// Loads a named email template from database and replaces tokens with passed entities, and returns a new email message object with template values
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="entities"></param>
        /// <returns></returns>
        private EmailMessage LoadAndProcessTemplate(string templateName, params object[] entities)
        {
            //first load the template from database
            var template = _emailTemplateService.FirstOrDefault(x => x.TemplateSystemName == templateName, x => x.EmailAccount, x => x.ParentEmailTemplate);

            if (template == null)
            {
                return(null);
            }
            //we'll check if there are parent templates and get all parent template
            var processedContentTemplate = _emailTemplateService.GetProcessedContentTemplate(template);

            var processedTemplateString = _tokenProcessor.ProcessAllTokens(processedContentTemplate, entities);
            var emailAccount            = template.EmailAccount;
            //create a new email message
            var emailMessage = new EmailMessage()
            {
                IsEmailBodyHtml       = true,
                EmailBody             = processedTemplateString,
                EmailAccount          = emailAccount,
                Subject               = template.Subject,
                OriginalEmailTemplate = template,
                Tos         = new List <EmailMessage.UserInfo>(),
                SendingDate = DateTime.UtcNow
            };

            return(emailMessage);
        }
Пример #3
0
        public IActionResult Put(EmailTemplate entityModel)
        {
            var emailTemplate = _emailTemplateService.FirstOrDefault(x => x.Id == entityModel.Id);

            //save it and respond
            emailTemplate.Subject             = entityModel.Subject;
            emailTemplate.Name                = entityModel.Name;
            emailTemplate.Template            = entityModel.Template;
            emailTemplate.AdministrationEmail = entityModel.AdministrationEmail;
            emailTemplate.IsMaster            = entityModel.IsMaster; //a system template can't be used as master
            if (entityModel.EmailAccountId > 0)
            {
                emailTemplate.EmailAccountId = entityModel.EmailAccountId;
            }

            _emailTemplateService.Update(emailTemplate);

            VerboseReporter.ReportSuccess("Sửa EmailTemplate thành công", "post");

            return(RespondSuccess(emailTemplate));
        }