Exemplo n.º 1
0
        /// <summary>
        /// QueueMail inserts a queue message and saves the Data.MailTemplate mail in blob storage.
        /// </summary>
        /// <param name="mail">Data.MailTemplate with its properties filled</param>
        /// <param name="emailTo">E-mail address to send to</param>
        /// <param name="customerGUID">Optional customer GUID, which can be set to null</param>
        /// <returns></returns>
        public async Task <bool> QueueMailAsync(Data.MailTemplate mail, string emailTo, Guid?customerGUID)
        {
            // new guid to use as identifier for blob and queue
            var id = Guid.NewGuid();

            (BlobClient blob, QueueClient queue, string email) = await GetBlobQueueAndEmailAsync(mail, emailTo, id, customerGUID);

            // upload to blob
            using (var ms = new MemoryStream())
            {
                StreamWriter writer = new StreamWriter(ms);
                writer.Write(email);
                writer.Flush();
                ms.Position = 0;
                await blob.UploadAsync(ms);
            }

            // queue message only has id as reference to blob
            var queueMessage = id.ToString();

            // add queue message
            await queue.SendMessageAsync(queueMessage);

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Provide a mail template object, placeholder group replacements, placeholder replacements and optional sections to include
        /// </summary>
        /// <param name="mailTemplate"></param>
        /// <param name="placeholderGroupReplacements"></param>
        /// <param name="placeholderReplacements"></param>
        /// <param name="optionalSectionsToInclude"></param>
        /// <param name="logger"></param>
        /// <returns>MailTemplate with Body and Subject replaced</returns>
        public async Task <Data.MailTemplate> ApplyPlaceholdersAsync(Data.MailTemplate mailTemplate, List <PlaceholderGroup> placeholderGroupReplacements = null, List <Placeholder> placeholderReplacements = null, List <string> optionalSectionsToInclude = null, System.IO.TextWriter logger = null)
        {
            var listOfDefaultValues = await _defaultValuePlaceholderRepository.FetchAllByMailTemplateAsync(mailTemplate.Identifier);

            mailTemplate.Body    = HttpUtility.HtmlDecode(ApplyPlaceholders(mailTemplate.Body, logger, placeholderGroupReplacements, placeholderReplacements, listOfDefaultValues, optionalSectionsToInclude));
            mailTemplate.Subject = HttpUtility.HtmlDecode(ApplyPlaceholders(mailTemplate.Subject, logger, placeholderGroupReplacements, placeholderReplacements, listOfDefaultValues, optionalSectionsToInclude));

            return(mailTemplate);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Permet la convertion d'un objet mailTemplate data en un objet mailTemplate DTO
        /// </summary>
        /// <param name="mailTemplate">mailTemplate Data</param>
        /// <returns>mailTemplate DTO</returns>
        public static MailTemplate EntityToDto(Data.MailTemplate mailTemplate)
        {
            MailTemplate mailTemplateDto = new MailTemplate();

            mailTemplateDto.Code    = mailTemplate.Code;
            mailTemplateDto.Contenu = mailTemplate.Contenu;
            mailTemplateDto.Id      = mailTemplate.Id;
            mailTemplateDto.Objet   = mailTemplate.Objet;

            return(mailTemplateDto);
        }
Exemplo n.º 4
0
        public async Task QueueMail()
        {
            var mail = new Data.MailTemplate
            {
                DefaultSenderEmail = TEST_SENDER_EMAIL,
                DefaultSenderName  = TEST_SENDER_NAME,
                Subject            = "Test",
                Body       = "body",
                Identifier = TEST_TEMPLATE_ID
            };

            var result = await _mailer.QueueMailAsync(mail, emailTo : TEST_RECEIVER_EMAIL, customerGUID : null);

            Assert.IsTrue(result);
        }
Exemplo n.º 5
0
        private async Task MailTemplatesList_ListLoad(ComponentListEventArgs e)
        {
            Implement = await _mailTemplateRepository.FetchSingleAsync(e.SelectedKey);

            if (Implement != null)
            {
                var latestTemplate = await _mailTemplateListRepository.FetchSingleByIdentifierAsync(Implement.Identifier);

                if (latestTemplate != null && Implement.ID != latestTemplate.ID)
                {
                    // someone is using the url to go to an old template, redirect to newest
                    Response.Redirect(wim.GetUrl(new KeyValue[] {
                        new KeyValue {
                            Key = "list", Value = wim.CurrentList.ID
                        },
                        new KeyValue {
                            Key = "item", Value = latestTemplate.ID
                        }
                    }));
                }

                if (e.SelectedKey > 0)
                {
                    ListOfAvailablePlaceholders  = "<b>Subject:</b><br/>";
                    ListOfAvailablePlaceholders += string.Join("<br/>", Logic.PlaceholderLogic.GetPlaceholderTags(Implement.Subject));
                    ListOfAvailablePlaceholders += "<br/><br/><b>Body:</b><br/>";
                    ListOfAvailablePlaceholders += string.Join("<br/>", Logic.PlaceholderLogic.GetPlaceholderTags(Implement.Body));
                }
            }
            else
            {
                Implement = new Data.MailTemplate();
            }

            // only show option to revert if there is a published version, a.k.a. a major version
            wim.SetPropertyVisibility(nameof(BtnRevert), Implement.VersionMinor > 0 && Implement.VersionMajor > 0);
            // only show publish button if current version is not published
            wim.SetPropertyVisibility(nameof(BtnPublish), !(Implement.IsPublished.HasValue && Implement.IsPublished.Value));
            wim.SetPropertyVisibility(nameof(BtnPreview), !wim.IsEditMode);
            wim.SetPropertyVisibility(nameof(BtnSendTestMail), !wim.IsEditMode);
            //wim.SetPropertyVisibility(nameof(BtnDefaultValues), !wim.IsEditMode);
            // not working yet, so invisible
            wim.SetPropertyVisibility(nameof(BtnDefaultValues), false);


            Utility.ReflectProperty(Implement, this);
        }
Exemplo n.º 6
0
        private async Task DefaultValues_List_ListSave(ComponentListEventArgs e)
        {
            if (wim.ChangedSearchGridItem != null)
            {
                var idQueryString = Request.Query["item"].ToString();

                int.TryParse(idQueryString, out int id);

                Data.MailTemplate mailTemplate = null;

                foreach (var item in wim.ChangedSearchGridItem)
                {
                    var defaultValue = item as Data.DefaultValuePlaceholder;
                    if (defaultValue != null)
                    {
                        if (defaultValue.ID < 0 && !string.IsNullOrEmpty(defaultValue.Value))
                        {
                            if (mailTemplate == null)
                            {
                                mailTemplate = await _mailTemplateRepository.FetchSingleAsync(id);
                            }

                            // create
                            defaultValue.ID             = 0;
                            defaultValue.MailTemplateID = mailTemplate.ID;
                            await _defaultValuePlaceholderRepository.SaveAsync(defaultValue);
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(defaultValue.Value))
                            {
                                // delete
                                await _defaultValuePlaceholderRepository.DeleteAsync(defaultValue);
                            }
                            else
                            {
                                // edit
                                await _defaultValuePlaceholderRepository.SaveAsync(defaultValue);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        private async Task <bool> CanSaveAsync()
        {
            if (Implement == null)
            {
                Implement = new Data.MailTemplate();
            }
            Utility.ReflectProperty(this, Implement, true);

            if (string.IsNullOrWhiteSpace(Implement.Identifier))
            {
                wim.Notification.AddError("Mail template Identifier is required");

                return(false);
            }

            //check there are no other templates with the same identifier
            var otherMailTemplate = await _mailTemplateListRepository.FetchSingleByIdentifierAsync(Implement.Identifier);

            if (otherMailTemplate != null && otherMailTemplate.ID != Implement.ID)
            {
                wim.Notification.AddError($"Mail template {Implement.Identifier} is already in use by {otherMailTemplate.Name}.");
                return(false);
            }

            if (!Logic.Helper.IsValid(Body))
            {
                wim.Notification.AddError("Body contains illegal placeholder characters. Only alphanumeric characters are allowed.");
            }

            if (!Logic.Helper.IsValid(Subject))
            {
                wim.Notification.AddError("Subject contains illegal placeholder characters. Only alphanumeric characters are allowed.");
            }

            return(true);
        }
Exemplo n.º 8
0
        private async Task <(BlobClient blob, QueueClient queue, string email)> GetBlobQueueAndEmailAsync(Data.MailTemplate mail, string emailTo, Guid id, Guid?customerGUID)
        {
            string emailFromName = mail.DefaultSenderName;
            string emailFrom     = mail.DefaultSenderEmail;
            string subject       = mail.Subject;
            string body          = mail.Body;
            string templateName  = mail.Identifier;
            string bccs          = mail.BCCReceivers;

            // get blob information
            var blob = await _blobPersister.GetBlobClientAsync(_sendGridMailerOptions.BlobContainer, id.ToString());

            // create email message to save to blob
            var email = new Email
            {
                ID           = id,
                FromName     = emailFromName,
                From         = emailFrom,
                To           = emailTo,
                Subject      = subject,
                Body         = body,
                Bccs         = bccs,
                TemplateName = templateName,
                CustomerGUID = customerGUID
            };
            var jsonEmail = Newtonsoft.Json.JsonConvert.SerializeObject(email);

            // create queue information
            var queue = await _queuePersister.GetQueueAsync(_sendGridMailerOptions.QueueName);

            return(blob, queue, jsonEmail);
        }