Ejemplo n.º 1
0
        /// <summary>
        /// Sends the email
        /// </summary>
        /// <param name="site"></param>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public static void SendEmail(string to, string subject, string body, bool queue = true, Nancy.NancyContext ctx = null)
        {
            if (_Outbox == null)
            {
                _Outbox = new ConcurrentQueue <MailMessage>();
            }

            if (string.IsNullOrEmpty(to))
            {
                System.Diagnostics.Debugger.Break();
                return;
            }

            MailMessage mail = new MailMessage();

            mail.To.Add(to);
            mail.Subject    = subject;
            mail.Body       = body;
            mail.IsBodyHtml = true;

            _Outbox.Enqueue(mail);

            if (queue == false)
            {
                if (ctx == null)
                {
                    throw new InvalidOperationException("Context is required");
                }

                MailSenderModule.ProcessQueue(ctx);
            }
        }
Ejemplo n.º 2
0
 public void Hook(IPipelines p)
 {
     p.AfterRequest.AddItemToEndOfPipeline((ctx) =>
     {
         MailSenderModule.ProcessQueue(ctx);
     });
 }
Ejemplo n.º 3
0
        private dynamic TestSendEmail(dynamic arg)
        {
            if (this.CurrentUser.HasClaim("admin") == false)
            {
                return(403);
            }

            var target   = (string)arg.body.Value.to;
            var settings = arg.body.Value.settings;
            var template = arg.body.Value.template;

            if (template != null)
            {
                MailSenderModule.SendEmail(settings, target, (string)template.Subject,
                                           (string)template.Body);
            }
            else
            {
                MailSenderModule.SendEmail(settings, target, "Test Email From NancyBlack",
                                           "Email was sent successfully from <a href=\"" + this.Request.Url + "\">NancyBlack</a>");
            }

            return(200);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Old method signature for compatibility
 /// </summary>
 /// <param name="site"></param>
 /// <param name="to"></param>
 /// <param name="subject"></param>
 /// <param name="body"></param>
 public static void SendEmail(dynamic site, string to, string subject, string body)
 {
     MailSenderModule.SendEmail(to, subject, body);
 }
Ejemplo n.º 5
0
        public void Hook(IPipelines p)
        {
            // Use Pipeline Hook so that we can get information about the requests before performing action
            // with the data

            p.AfterRequest.AddItemToStartOfPipeline((ctx) =>
            {
                if (_Events == null)
                {
                    return;
                }

                var siteconfig = ctx.Items["CurrentSite"] as JObject;
                var user       = ctx.CurrentUser as NcbUser;
                // this will not work when more than one person updating the site at the same time

                var events = _Events.ToList();
                _Events    = null;

                if (siteconfig.Property("watcher") == null)
                {
                    return; // not configured
                }

                var watcher = siteconfig.Property("watcher").Value as JObject;
                var userIP  = ctx.Request.UserHostAddress;

                Task.Run(() =>
                {
                    foreach (var item in events)
                    {
                        var datatype = watcher.Property(item.DataTypeName.ToLowerInvariant());
                        if (datatype == null)
                        {
                            continue;
                        }

                        var config = datatype.Value.ToObject <WatcherConfig>();

                        if (item.Action == "newAttachments")
                        {
                            if (config.autoPrintAttachment == true)
                            {
                                new DataWatcherHub().PrintDocument(item.AffectedRow);
                            }

                            continue;
                        }

                        if (config.version == true)
                        {
                            item.Database.UpsertRecord(new RowVersion()
                            {
                                Action          = item.Action,
                                js_Row          = JsonConvert.SerializeObject(item.AffectedRow),
                                UserId          = user.Id,
                                UserHostAddress = userIP,
                                __createdAt     = DateTime.Now,
                                RowId           = (int)item.AffectedRow.Id,
                                DataType        = item.DataTypeName
                            });
                        }

                        var emailConfig = config[item.Action];
                        if (emailConfig != null)
                        {
                            var autoGenPdfConfig = emailConfig.autoGeneratePDF;
                            if (autoGenPdfConfig != null && autoGenPdfConfig.enable == true)
                            {
                                Object dataToClient = new
                                {
                                    config = autoGenPdfConfig,
                                    data   = item.AffectedRow,
                                };
                                new DataWatcherHub().GenPDFandUpload(dataToClient);
                            }

                            if (emailConfig.enable)
                            {
                                var subject = Razor.Parse <DatabaseEvent>(emailConfig.emailSubject, item);
                                var body    = Razor.Parse <DatabaseEvent>(emailConfig.emailTemplate, item);

                                MailSenderModule.SendEmail(emailConfig.sendTo, subject, body);
                            }
                        }
                    }
                });
            });
        }