Exemplo n.º 1
0
        private void TemplateParameterPopulator(EmailTemplateProcessor processor)
        {
            var jTemplate = new JObject();


            foreach (var item in processor.Items)
            {
                string n = item.GetType().Name;
                if (n.EndsWith("Model"))
                {
                    n = n.Substring(0, n.Length - "Model".Length);
                }
                else if (n.EndsWith("Object"))
                {
                    n = n.Substring(0, n.Length - "Object".Length);
                }

                jTemplate.Add(n, Newtonsoft.Json.Linq.JObject.Parse(JsonConvert.SerializeObject(item)));
            }

            // walk through the injected custom parameters, if any
            if (processor.CustomParameters != null)
            {
                //remove any subject or body parameters that are also in the custom parameters
                processor.SubjParameters.RemoveWhere(c => processor.CustomParameters.Keys.Contains(c));
                processor.BodyParameters.RemoveWhere(c => processor.CustomParameters.Keys.Contains(c));

                foreach (var item in processor.CustomParameters.Where(c => !string.IsNullOrWhiteSpace(c.Value)))
                {
                    processor.Template.UpdateSubject(processor.Template.Subject.Replace("{" + item.Key + "}", item.Value));
                    processor.Template.UpdateText(processor.Template.Text.Replace("{" + item.Key + "}", item.Value));
                }
            }

            // walk through the injected Items for the subject
            foreach (var key in processor.SubjParameters)
            {
                var token = jTemplate.SelectToken(key);
                if (token != null)
                {
                    processor.Template.UpdateSubject(processor.Template.Subject.Replace("{" + key + "}", token.ToString()));
                }
                else
                {
                    processor.AddError($"Value for subject parameter '{key}' is null");
                }
            }

            // walk through the injected Items for the body
            foreach (var key in processor.BodyParameters)
            {
                var token = jTemplate.SelectToken(key);
                if (token != null)
                {
                    processor.Template.UpdateText(processor.Template.Text.Replace("{" + key + "}", token.ToString()));
                }
                else
                {
                    processor.AddError($"Value for html/body parameter '{key}' is null");
                }
            }
        }
Exemplo n.º 2
0
        public bool TryProcessTemplate(out EmailTemplateProcessor processor, EmailTemplateId templateId, Dictionary <string, string> parameters, params object[] items)
        {
            var t = this.DataManager.GetEmailTemplate(templateId);

            processor = null;

            if (t == null)
            {
                processor = new EmailTemplateProcessor("No template found for: " + templateId.ToString());
            }
            else
            {
                var url = this.BaseUrl();

                if (parameters == null)
                {
                    parameters = new Dictionary <string, string>();
                }

                UserModel user = null;
                if (items != null && items.Length > 0)
                {
                    user = items.FirstOrDefault(c => c is UserModel) as UserModel;
                }

                //DisplayName is used in emails as a safety valve to identify who the user is...
                if (user != null)
                {
                    parameters.TryAdd("DisplayName", user.Name ?? string.Empty);
                }
                else
                {
                    parameters.TryAdd("DisplayName", "");
                }

                parameters.TryAdd("HomeUrl", url);
                parameters.TryAdd("LoginUrl", url + "login");
                parameters.TryAdd("SignupUrl", url + "signup");
                parameters.TryAdd("LogoUrl", url + "statics/logo.png");
                parameters.TryAdd("PhishingUrl", url + "phishing");

                //get the html containing the master layout for all emails
                //string layout = string.Copy( this.DataManager.EmailLayout.Text );
                //processor = new EmailTemplateProcessor( t, layout, parameters, items );

                processor = new EmailTemplateProcessor(t, parameters, items);

                //var matches = Regex.Matches("Test {Token1} {Token 2}", @"{([^{}]*)");
                var regex = new System.Text.RegularExpressions.Regex(@"{([^{}]*)", System.Text.RegularExpressions.RegexOptions.Multiline);

                try
                {
                    TemplateParameterDiscoverer(regex.Match(processor.Template.Subject), processor.SubjParameters);
                }
                catch (Exception ex)
                {
                    processor.AddError("Error parsing subject parameters: " + ex.ToString());
                }
                try
                {
                    TemplateParameterDiscoverer(regex.Match(processor.Template.Text), processor.BodyParameters);
                }
                catch (Exception ex)
                {
                    processor.AddError("Error parsing html parameters: " + ex.ToString());
                }

                try
                {
                    TemplateParameterPopulator(processor);
                }
                catch (Exception ex)
                {
                    processor.AddError("Error populating parameters: " + ex.ToString());
                }
            }

            return(processor.Successful);
        }