Example #1
0
        public MailingModule()
        {
            this.RequiresAuthentication();

            Get["/mailing"] = parameters =>
            {
                return(View["View/mailing.sshtml",
                            new MailingViewModel(Translator, CurrentSession)]);
            };
            Get["/mailing/list"] = parameters =>
            {
                return(View["View/mailinglist.sshtml",
                            new MailingListViewModel(Translator, Database, CurrentSession)]);
            };
            Get["/mailing/add"] = parameters =>
            {
                return(View["View/mailingedit.sshtml",
                            new MailingEditViewModel(Translator, Database, CurrentSession)]);
            };
            Post["/mailing/add/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <MailingEditViewModel>(ReadBody());
                var    status   = CreateStatus();

                if (Guid.TryParse(idString, out Guid id))
                {
                    var mailing = new Mailing(id);
                    status.AssignStringRequired("Title", mailing.Title, model.Title);
                    status.AssignObjectIdString("RecipientFeed", mailing.RecipientFeed, model.RecipientFeed);
                    status.AssignObjectIdString("RecipientTag", mailing.RecipientTag, model.RecipientTag);
                    status.AssignEnumIntString("RecipientLanguage", mailing.RecipientLanguage, model.RecipientLanguage);
                    status.AssignObjectIdString("SenderGroup", mailing.Sender, model.SenderGroup);
                    status.AssignObjectIdString("Header", mailing.Header, model.Header);
                    status.AssignObjectIdString("Footer", mailing.Footer, model.Footer);
                    status.AssignStringRequired("Subject", mailing.Subject, model.Subject);
                    var worker = new HtmlWorker(model.HtmlText);
                    mailing.HtmlText.Value  = worker.CleanHtml;
                    mailing.PlainText.Value = worker.PlainText;
                    mailing.Creator.Value   = CurrentSession.User;

                    if (status.IsSuccess)
                    {
                        if (status.HasAccess(mailing.RecipientFeed.Value, PartAccess.Mailings, AccessRight.Write) &&
                            (mailing.Sender.Value == null || status.HasAccess(mailing.Sender.Value, PartAccess.Mailings, AccessRight.Write)))
                        {
                            Database.Save(mailing);
                            Notice("{0} added mailing {1}", CurrentSession.User.UserName.Value, mailing);
                        }
                    }
                }
                else
                {
                    status.SetErrorNotFound();
                }

                return(status.CreateJsonData());
            };
            Get["/mailing/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    mailing  = Database.Query <Mailing>(idString);

                if (mailing != null)
                {
                    if (mailing.Status.Value == MailingStatus.New)
                    {
                        return(View["View/mailingedit.sshtml",
                                    new MailingEditViewModel(Translator, Database, CurrentSession, mailing)]);
                    }
                    else
                    {
                        return(View["View/mailingscheduled.sshtml",
                                    new MailingScheduledViewModel(Translator, Database, CurrentSession, mailing)]);
                    }
                }

                return(Response.AsRedirect("/mailing"));
            };
            Post["/mailing/edit/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <MailingEditViewModel>(ReadBody());
                var    mailing  = Database.Query <Mailing>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(mailing))
                {
                    if (mailing.Status.Value == MailingStatus.New)
                    {
                        status.AssignStringRequired("Title", mailing.Title, model.Title);
                        status.AssignObjectIdString("RecipientFeed", mailing.RecipientFeed, model.RecipientFeed);
                        status.AssignObjectIdString("RecipientTag", mailing.RecipientTag, model.RecipientTag);
                        status.AssignEnumIntString("RecipientLanguage", mailing.RecipientLanguage, model.RecipientLanguage);
                        status.AssignObjectIdString("SenderGroup", mailing.Sender, model.SenderGroup);
                        status.AssignObjectIdString("Header", mailing.Header, model.Header);
                        status.AssignObjectIdString("Footer", mailing.Footer, model.Footer);
                        status.AssignStringRequired("Subject", mailing.Subject, model.Subject);
                        var worker = new HtmlWorker(model.HtmlText);
                        mailing.HtmlText.Value  = worker.CleanHtml;
                        mailing.PlainText.Value = worker.PlainText;
                        mailing.Creator.Value   = CurrentSession.User;

                        if (status.IsSuccess)
                        {
                            if (status.HasAccess(mailing.RecipientFeed.Value, PartAccess.Mailings, AccessRight.Write) &&
                                (mailing.Sender.Value == null || status.HasAccess(mailing.Sender.Value, PartAccess.Mailings, AccessRight.Write)))
                            {
                                Database.Save(mailing);
                                Notice("{0} changed mailing {1}", CurrentSession.User.UserName.Value, mailing);
                            }
                        }
                    }
                    else
                    {
                        status.SetErrorAccessDenied();
                    }
                }

                return(status.CreateJsonData());
            };
            Post["/mailing/test"] = parameters =>
            {
                var model  = JsonConvert.DeserializeObject <MailingEditViewModel>(ReadBody());
                var worker = new HtmlWorker(model.HtmlText);
                var header = Database.Query <MailingElement>(model.Header);
                var footer = Database.Query <MailingElement>(model.Footer);
                var sender = Database.Query <Group>(model.SenderGroup);

                var htmlText  = worker.CleanHtml;
                var plainText = worker.PlainText;

                if (header != null)
                {
                    htmlText  = HtmlWorker.ConcatHtml(header.HtmlText.Value, htmlText);
                    plainText = header.PlainText.Value + plainText;
                }

                if (footer != null)
                {
                    htmlText  = HtmlWorker.ConcatHtml(htmlText, footer.HtmlText.Value);
                    plainText = plainText + footer.PlainText.Value;
                }

                var contact = Database
                              .Query <Contact>()
                              .Where(p => HasAccess(p, PartAccess.Contact, AccessRight.Read))
                              .FirstOrDefault();

                if (contact == null)
                {
                    return(PostResult.Failed(
                               Translate("Mailing.Edit.Test.Failed", "Failed message on sending test mail in mailing edit page", "E-Mail could not be sent.")));
                }

                var templator = new Templator(new ContactContentProvider(Translator, contact));
                htmlText  = templator.Apply(htmlText);
                plainText = templator.Apply(plainText);

                try
                {
                    var language = CurrentSession.User.Language.Value;
                    var from     = new MailboxAddress(
                        sender.MailName.Value[language],
                        sender.MailAddress.Value[language]);
                    var to        = new MailboxAddress(model.TestAddress);
                    var senderKey = sender.GpgKeyId.Value == null ? null :
                                    new GpgPrivateKeyInfo(
                        sender.GpgKeyId.Value,
                        sender.GpgKeyPassphrase.Value);
                    var content  = new Multipart("alternative");
                    var textPart = new TextPart("plain")
                    {
                        Text = plainText
                    };
                    textPart.ContentTransferEncoding = ContentEncoding.QuotedPrintable;
                    content.Add(textPart);
                    var htmlPart = new TextPart("html")
                    {
                        Text = htmlText
                    };
                    htmlPart.ContentTransferEncoding = ContentEncoding.QuotedPrintable;
                    content.Add(htmlPart);

                    Global.Mail.Send(from, to, senderKey, null, model.Subject, content);
                    Notice("{0} tests mailing with subject {1}", CurrentSession.User.UserName.Value, model.Subject);

                    return(PostResult.Success(
                               Translate("Mailing.Edit.Test.Success", "Success message on sending test mail in mailing edit page", "Test E-Mail sent.")));
                }
                catch
                {
                    return(PostResult.Failed(
                               Translate("Mailing.Edit.Test.Failed", "Failed message on sending test mail in mailing edit page", "E-Mail could not be sent.")));
                }
            };
            Get["/mailing/send/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    mailing  = Database.Query <Mailing>(idString);

                if (mailing != null)
                {
                    if (HasAccess(mailing.RecipientFeed.Value, PartAccess.Mailings, AccessRight.Write) &&
                        (mailing.Sender.Value == null || HasAccess(mailing.Sender.Value, PartAccess.Mailings, AccessRight.Write)))
                    {
                        return(View["View/mailingsend.sshtml",
                                    new MailingSendViewModel(Translator, Database, CurrentSession, mailing)]);
                    }
                }

                return(AccessDenied());
            };
            Post["/mailing/send/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    model    = JsonConvert.DeserializeObject <MailingSendViewModel>(ReadBody());
                var    mailing  = Database.Query <Mailing>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(mailing))
                {
                    status.AssignDateString("Date", mailing.SendingDate, model.Date, true);
                    status.AddAssignTimeString("Time", mailing.SendingDate, model.Time);

                    if (status.IsSuccess)
                    {
                        if (status.HasAccess(mailing.RecipientFeed.Value, PartAccess.Mailings, AccessRight.Write) &&
                            (mailing.Sender.Value == null || status.HasAccess(mailing.Sender.Value, PartAccess.Mailings, AccessRight.Write)))
                        {
                            mailing.SendingDate.Value = mailing.SendingDate.Value.Value.ToUniversalTime();
                            mailing.Status.Value      = MailingStatus.Scheduled;
                            Database.Save(mailing);
                            Notice("{0} sent mailing {1}", CurrentSession.User.UserName.Value, mailing);
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/mailing/delete/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    mailing  = Database.Query <Mailing>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(mailing))
                {
                    if (status.HasAccess(mailing.RecipientFeed.Value, PartAccess.Mailings, AccessRight.Write) &&
                        (mailing.Sender.Value == null || status.HasAccess(mailing.Sender.Value, PartAccess.Mailings, AccessRight.Write)))
                    {
                        using (var transaction = Database.BeginTransaction())
                        {
                            mailing.Delete(Database);
                            transaction.Commit();
                            Notice("{0} deleted mailing {1}", CurrentSession.User.UserName.Value, mailing);
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/mailing/cancel/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    mailing  = Database.Query <Mailing>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(mailing))
                {
                    if (status.HasAccess(mailing.RecipientFeed.Value, PartAccess.Mailings, AccessRight.Write) &&
                        (mailing.Sender.Value == null || status.HasAccess(mailing.Sender.Value, PartAccess.Mailings, AccessRight.Write)))
                    {
                        if (mailing.Status.Value == MailingStatus.Scheduled ||
                            mailing.Status.Value == MailingStatus.Sending)
                        {
                            using (var transaction = Database.BeginTransaction())
                            {
                                mailing.Status.Value = MailingStatus.Canceled;
                                Database.Save(mailing);
                                transaction.Commit();
                                Notice("{0} canceled mailing {1}", CurrentSession.User.UserName.Value, mailing);
                            }
                        }
                        else
                        {
                            status.SetErrorAccessDenied();
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/mailing/copy/{id}"] = parameters =>
            {
                string idString = parameters.id;
                var    mailing  = Database.Query <Mailing>(idString);
                var    status   = CreateStatus();

                if (status.ObjectNotNull(mailing))
                {
                    if (status.HasAccess(mailing.RecipientFeed.Value, PartAccess.Mailings, AccessRight.Write) &&
                        (mailing.Sender.Value == null || status.HasAccess(mailing.Sender.Value, PartAccess.Mailings, AccessRight.Write)))
                    {
                        var newMailing = new Mailing(Guid.NewGuid());
                        newMailing.Title.Value = mailing.Title.Value +
                                                 Translate("Mailing.Copy.Postfix", "Postfix of copied mailings", " (Copy)");
                        newMailing.RecipientFeed.Value     = mailing.RecipientFeed.Value;
                        newMailing.RecipientTag.Value      = mailing.RecipientTag.Value;
                        newMailing.RecipientLanguage.Value = mailing.RecipientLanguage.Value;
                        newMailing.Sender.Value            = mailing.Sender.Value;
                        newMailing.Header.Value            = mailing.Header.Value;
                        newMailing.Footer.Value            = mailing.Footer.Value;
                        newMailing.Subject.Value           = mailing.Subject.Value;
                        newMailing.HtmlText.Value          = mailing.HtmlText.Value;
                        newMailing.PlainText.Value         = mailing.PlainText.Value;
                        newMailing.Creator.Value           = CurrentSession.User;
                        newMailing.CreatedDate.Value       = DateTime.UtcNow;
                        newMailing.Status.Value            = MailingStatus.New;
                        Database.Save(newMailing);
                        Notice("{0} copied mailing {1}", CurrentSession.User.UserName.Value, mailing);
                    }
                }

                return(status.CreateJsonData());
            };
        }
        public MailingElementEdit()
        {
            this.RequiresAuthentication();

            Get["/mailingelement"] = parameters =>
            {
                if (HasSystemWideAccess(PartAccess.Mailings, AccessRight.Read))
                {
                    return(View["View/mailingelement.sshtml",
                                new MailingElementViewModel(Translator, CurrentSession)]);
                }
                return(AccessDenied());
            };
            Get["/mailingelement/list"] = parameters =>
            {
                if (HasSystemWideAccess(PartAccess.Mailings, AccessRight.Read))
                {
                    return(View["View/mailingelementlist.sshtml",
                                new MailingElementListViewModel(Translator, Database)]);
                }
                return(null);
            };
            Get["/mailingelement/edit/{id}"] = parameters =>
            {
                if (HasSystemWideAccess(PartAccess.Mailings, AccessRight.Write))
                {
                    string idString       = parameters.id;
                    var    mailingElement = Database.Query <MailingElement>(idString);

                    if (mailingElement != null)
                    {
                        return(View["View/mailingelementedit.sshtml",
                                    new MailingElementEditViewModel(Translator, Database, CurrentSession, mailingElement)]);
                    }
                }

                return(null);
            };
            Post["/mailingelement/edit/{id}"] = parameters =>
            {
                var    status         = CreateStatus();
                string idString       = parameters.id;
                var    model          = JsonConvert.DeserializeObject <MailingElementEditViewModel>(ReadBody());
                var    mailingElement = Database.Query <MailingElement>(idString);

                if (status.ObjectNotNull(mailingElement))
                {
                    if (status.HasAccess(mailingElement.Owner.Value, PartAccess.Mailings, AccessRight.Write))
                    {
                        status.AssignObjectIdString("Owner", mailingElement.Owner, model.Owner);
                        status.AssignStringRequired("Name", mailingElement.Name, model.Name);
                        status.AssignEnumIntString("Type", mailingElement.Type, model.Type);
                        var worker = new HtmlWorker(model.HtmlText);
                        mailingElement.HtmlText.Value  = worker.CleanHtml;
                        mailingElement.PlainText.Value = worker.PlainText;

                        if (status.IsSuccess)
                        {
                            Database.Save(mailingElement);
                            Notice("{0} changed mailing element {1}", CurrentSession.User.UserName.Value, mailingElement);
                        }
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/mailingelement/add"] = parameters =>
            {
                if (HasAnyFeedAccess(PartAccess.Mailings, AccessRight.Write))
                {
                    return(View["View/mailingelementedit.sshtml",
                                new MailingElementEditViewModel(Translator, Database, CurrentSession)]);
                }
                return(AccessDenied());
            };
            Post["/mailingelement/add/new"] = parameters =>
            {
                var model          = JsonConvert.DeserializeObject <MailingElementEditViewModel>(ReadBody());
                var status         = CreateStatus();
                var mailingElement = new MailingElement(Guid.NewGuid());
                status.AssignObjectIdString("Owner", mailingElement.Owner, model.Owner);
                status.AssignStringRequired("Name", mailingElement.Name, model.Name);
                status.AssignEnumIntString("Type", mailingElement.Type, model.Type);
                var worker = new HtmlWorker(model.HtmlText);
                mailingElement.HtmlText.Value  = worker.CleanHtml;
                mailingElement.PlainText.Value = worker.PlainText;

                if (status.HasAccess(mailingElement.Owner.Value, PartAccess.Mailings, AccessRight.Write))
                {
                    if (status.IsSuccess)
                    {
                        Database.Save(mailingElement);
                        Notice("{0} added mailing element {1}", CurrentSession.User.UserName.Value, mailingElement);
                    }
                }

                return(status.CreateJsonData());
            };
            Get["/mailingelement/delete/{id}"] = parameters =>
            {
                var    status         = CreateStatus();
                string idString       = parameters.id;
                var    mailingElement = Database.Query <MailingElement>(idString);

                if (status.ObjectNotNull(mailingElement))
                {
                    if (status.HasAccess(mailingElement.Owner.Value, PartAccess.Mailings, AccessRight.Write))
                    {
                        mailingElement.Delete(Database);
                        Notice("{0} deleted mailing element {1}", CurrentSession.User.UserName.Value, mailingElement);
                    }
                }
                return(status.CreateJsonData());
            };
        }
Example #3
0
        private void RunSending(IDatabase database, Mailing mailing)
        {
            int remainingCount = 0;

            foreach (var sending in database.Query <Sending>(DC.Equal("mailingid", mailing.Id.Value)))
            {
                if (sending.Status.Value == SendingStatus.Created)
                {
                    if (_maxMailsCount > 0)
                    {
                        _maxMailsCount--;
                        var header    = mailing.Header.Value;
                        var footer    = mailing.Footer.Value;
                        var htmlText  = mailing.HtmlText.Value;
                        var plainText = mailing.PlainText.Value;

                        if (header != null)
                        {
                            htmlText  = HtmlWorker.ConcatHtml(header.HtmlText.Value, htmlText);
                            plainText = header.PlainText.Value + plainText;
                        }

                        if (footer != null)
                        {
                            htmlText  = HtmlWorker.ConcatHtml(htmlText, footer.HtmlText.Value);
                            plainText = plainText + footer.PlainText.Value;
                        }

                        var translation = new Translation(database);
                        var translator  = new Translator(translation, sending.Address.Value.Contact.Value.Language.Value);
                        var templator   = new Templator(new ContactContentProvider(translator, sending.Address.Value.Contact.Value));
                        htmlText  = templator.Apply(htmlText);
                        plainText = templator.Apply(plainText);

                        try
                        {
                            var language = sending.Address.Value.Contact.Value.Language.Value;
                            var from     = new MimeKit.MailboxAddress(
                                mailing.Sender.Value.MailName.Value[language],
                                mailing.Sender.Value.MailAddress.Value[language]);
                            var to = new MimeKit.MailboxAddress(
                                sending.Address.Value.Contact.Value.ShortHand,
                                sending.Address.Value.Address.Value);
                            var senderKey = mailing.Sender.Value.GpgKeyId.Value == null ? null :
                                            new GpgPrivateKeyInfo(
                                mailing.Sender.Value.GpgKeyId.Value,
                                mailing.Sender.Value.GpgKeyPassphrase.Value);
                            var content  = new Multipart("alternative");
                            var textPart = new TextPart("plain")
                            {
                                Text = plainText
                            };
                            textPart.ContentTransferEncoding = ContentEncoding.QuotedPrintable;
                            content.Add(textPart);
                            var htmlPart = new TextPart("html")
                            {
                                Text = htmlText
                            };
                            htmlPart.ContentTransferEncoding = ContentEncoding.QuotedPrintable;
                            content.Add(htmlPart);

                            Global.Mail.Send(from, to, senderKey, null, mailing.Subject.Value, content);
                            sending.Status.Value   = SendingStatus.Sent;
                            sending.SentDate.Value = DateTime.UtcNow;
                            database.Save(sending);
                            Journal(database, mailing, sending.Address.Value.Contact.Value,
                                    "MailTask.Journal.Sent",
                                    "Journal entry sent mail",
                                    "Task sent mail {0}",
                                    t => mailing.Title.Value);
                        }
                        catch (Exception exception)
                        {
                            sending.Status.Value         = SendingStatus.Failed;
                            sending.FailureMessage.Value = exception.Message;
                            sending.SentDate.Value       = DateTime.UtcNow;
                            database.Save(sending);
                            Journal(database, mailing, sending.Address.Value.Contact.Value,
                                    "MailTask.Journal.Failed",
                                    "Journal entry sending mail failed",
                                    "Task failed sending mail {0}",
                                    t => mailing.Title.Value);
                        }
                    }
                    else
                    {
                        remainingCount++;
                    }
                }
            }

            if (remainingCount < 1)
            {
                mailing.Status.Value   = MailingStatus.Sent;
                mailing.SentDate.Value = DateTime.UtcNow;
                database.Save(mailing);
                Global.Log.Notice("Mailing {0} has finished sending", mailing.Title);
            }
            else
            {
                Global.Log.Notice("Mailing {0} needs to send {1} more mails", mailing.Title, remainingCount);
            }
        }