Пример #1
0
        public Message Generate(string messageCode, object model, string locale)
        {
            if (model == null)
            {
                throw new NullReferenceException("model");
            }

            try
            {
                var cacheName = string.Format("{0}.{1}", messageCode, locale);

                if (!_service.HasTemplate(cacheName))
                {
                    var template = _templateLocator.GetTemplate(messageCode, locale);

                    _service.Compile(template, typeof(MessageTemplate <>), cacheName);
                }

                var resolvedTemplate = _service.Resolve(cacheName, model);
                var result           = _service.Run(resolvedTemplate, null);

                return(new Message
                {
                    Subject = ((IMessageTemplate)resolvedTemplate).Subject,
                    Body = result
                });
            }
            catch (TemplateCompilationException tce)
            {
                _log.Error("Template compilation error", tce);
                throw;
            }
        }
Пример #2
0
        public SendEmailResponse Invoke(SendEmailRequest request)
        {
            Message message;

            Trace("Starting...");

            if (request.EmailInfo is NonTemplatedEmailInfo)
            {
                var emailInfo = request.EmailInfo as NonTemplatedEmailInfo;
                Trace("...Sending non templated email");

                message = new Message
                {
                    To      = emailInfo.To,
                    Bcc     = emailInfo.Bcc,
                    Body    = emailInfo.Body,
                    Cc      = emailInfo.Cc,
                    Subject = emailInfo.Subject,
                };
                _emailSender.Send(message);
                Trace("...Sent");
            }
            else
            {
                Trace("...Getting template");
                string template = _templateLocator.GetTemplate(request.EmailInfo);
                Trace("...Parsing email info");
                var emailParams = _emailInfoParser.Parse(request.EmailInfo);
                Trace("...Rendering email");
                message = _emailRenderer.Render(template, emailParams);
                if (!request.SkipSend)
                {
                    Trace("...Sending");
                    _emailSender.Send(message);
                }
                Trace("...Sent");
            }

            return(new SendEmailResponse
            {
                Message = message
            });
        }
Пример #3
0
        public string RenderFromTemplate(string template, IDictionary <string, string> emailParams)
        {
            _state       = EmailRendererState.Normal;
            _emailParams = emailParams;
            _outputSb    = _finalSb;

            foreach (var c in template)
            {
                switch (_state)
                {
                case EmailRendererState.Normal:
                    if (c == '$')
                    {
                        _outputSb.Append(_bufferSb.ToString());
                        _bufferSb.Clear();
                        _bufferSb.Append(c);
                        _state = EmailRendererState.BeginParam;
                    }
                    else
                    {
                        _bufferSb.Append(c);
                    }
                    break;

                case EmailRendererState.BeginParam:
                    if (c == '(')
                    {
                        _bufferSb.Clear();
                        _state = EmailRendererState.InParam;
                    }
                    else
                    {
                        _state = EmailRendererState.Normal;
                    }
                    break;

                case EmailRendererState.InParam:
                    if (c == ')')
                    {
                        var directive = GetDirective();
                        directive.MutateState(this);
                        _bufferSb.Clear();
                        _state = EmailRendererState.Normal;
                    }
                    else
                    {
                        _bufferSb.Append(c);
                    }
                    break;
                }
            }

            _outputSb.Append(_bufferSb.ToString());

            if (_masterTemplateName != null)
            {
                var masterRenderer  = new EmailRenderer(_config, _templateLocator);
                var emailParamsCopy = emailParams.ToDictionary(x => x.Key, x => x.Value);

                foreach (var placeholder in _placeholderSbs)
                {
                    emailParamsCopy[placeholder.Key] = placeholder.Value.ToString();
                }

                if (!_placeholderSbs.Any())
                {
                    emailParamsCopy["Placeholder"] = _finalSb.ToString();
                }

                var masterTemplate = _templateLocator.GetTemplate(_masterTemplateName);
                return(masterRenderer.RenderFromTemplate(masterTemplate, emailParamsCopy));
            }

            return(_outputSb.ToString());
        }