예제 #1
0
        public void Setup()
        {
            var template = new NewsletterTemplate
            {
                Name         = "Default Newsletter Template",
                BaseTemplate =
                    System.IO.File.ReadAllText(
                        HttpContext.Current.Server.MapPath(
                            @"\Apps\Ecommerce\Installation\Content\NewsletterBaseTemplate.txt")),
                Divider =
                    System.IO.File.ReadAllText(
                        HttpContext.Current.Server.MapPath(@"\Apps\Ecommerce\Installation\Content\NewsletterDivider.txt")),
            };

            _newsletterTemplateAdminService.Add(template);


            var productListTemplate = _getContentItemTemplateData.Get <ProductListTemplateData>(template);

            productListTemplate.ProductGridTemplate = System.IO.File.ReadAllText(
                HttpContext.Current.Server.MapPath(
                    @"\Apps\Ecommerce\Installation\Content\NewsletterProductGridTemplate.txt"));
            productListTemplate.ProductRowTemplate = System.IO.File.ReadAllText(
                HttpContext.Current.Server.MapPath(
                    @"\Apps\Ecommerce\Installation\Content\NewsletterProductRowTemplate.txt"));
            productListTemplate.ProductTemplate = System.IO.File.ReadAllText(
                HttpContext.Current.Server.MapPath(
                    @"\Apps\Ecommerce\Installation\Content\NewsletterProductTemplate.txt"));
            _templateDataAdminService.Update(productListTemplate);


            var freeTextTemplate = _getContentItemTemplateData.Get <FreeTextTemplateData>(template);

            freeTextTemplate.FreeTextTemplate = System.IO.File.ReadAllText(
                HttpContext.Current.Server.MapPath(
                    @"\Apps\Ecommerce\Installation\Content\NewsletterFreeTextTemplate.txt"));
            _templateDataAdminService.Update(freeTextTemplate);


            var bannerTemplate = _getContentItemTemplateData.Get <BannerTemplateData>(template);

            bannerTemplate.BannerTemplate = System.IO.File.ReadAllText(
                HttpContext.Current.Server.MapPath(
                    @"\Apps\Ecommerce\Installation\Content\NewsletterBannerTemplate.txt"));
            _templateDataAdminService.Update(bannerTemplate);

            var imageLeftTextRightTemplateData = _getContentItemTemplateData.Get <ImageLeftAndTextRightTemplateData>(template);

            imageLeftTextRightTemplateData.ImageLeftAndTextRightTemplate = System.IO.File.ReadAllText(
                HttpContext.Current.Server.MapPath(
                    @"\Apps\Ecommerce\Installation\Content\ImageLeftAndTextRightTemplate.txt"));
            _templateDataAdminService.Update(imageLeftTextRightTemplateData);

            var imageRightTextLeftTemplateData = _getContentItemTemplateData.Get <ImageRightAndTextLeftTemplateData>(template);

            imageRightTextLeftTemplateData.ImageRightAndTextLeftTemplate = System.IO.File.ReadAllText(
                HttpContext.Current.Server.MapPath(
                    @"\Apps\Ecommerce\Installation\Content\ImageRightAndTextLeftTemplate.txt"));
            _templateDataAdminService.Update(imageRightTextLeftTemplateData);
        }
예제 #2
0
 public T Get <T>(NewsletterTemplate template) where T : ContentItemTemplateData
 {
     return(template == null
         ? null
         : _session.QueryOver <T>()
            .Where(arg => arg.NewsletterTemplate.Id == template.Id)
            .Cacheable()
            .Take(1)
            .SingleOrDefault());
 }
예제 #3
0
 // ReSharper disable UnusedMember.Local - Called via reflection
 private static string ParseItem <T>(NewsletterTemplate template, T item)
 {
     try
     {
         var parser = MrCMSApplication.Get <INewsletterItemParser <T> >();
         return(parser != null?parser.Parse(template, item) : string.Empty);
     }
     catch
     {
         return(string.Empty);
     }
 }
        public string Parse(NewsletterTemplate template, FreeText item)
        {
            var freeTextTemplateData = _getContentItemTemplateData.Get <FreeTextTemplateData>(template);

            if (freeTextTemplateData == null)
            {
                return(string.Empty);
            }
            string output = freeTextTemplateData.FreeTextTemplate;

            if (string.IsNullOrWhiteSpace(output))
            {
                return(output);
            }
            output = TextRegex.Replace(output, item.Text ?? string.Empty);
            return(output);
        }
        public string Parse(NewsletterTemplate template, ProductList item)
        {
            var templateData = _getContentItemTemplateData.Get <ProductListTemplateData>(template);

            if (templateData == null)
            {
                return(string.Empty);
            }

            string output = templateData.ProductGridTemplate;

            if (string.IsNullOrWhiteSpace(output))
            {
                return(string.Empty);
            }

            Product[] products = GetProducts(item);
            output = RowRegex.Replace(output, match => ParseRows(templateData, products));
            return(output);
        }
        public string Parse(NewsletterTemplate template, Banner item)
        {
            var bannerTemplateData = _getContentItemTemplateData.Get <BannerTemplateData>(template);

            if (bannerTemplateData == null)
            {
                return(string.Empty);
            }

            string output = bannerTemplateData.BannerTemplate;

            if (string.IsNullOrWhiteSpace(output))
            {
                return(string.Empty);
            }

            output = ImageRegex.Replace(output, _urlHelper.ToAbsolute(item.ImageUrl));
            output = LinkRegex.Replace(output, _urlHelper.ToAbsolute(item.LinkUrl));
            return(output);
        }
        public string Parse(NewsletterTemplate template, ImageRightAndTextLeft item)
        {
            var templateData = _getContentItemTemplateData.Get <ImageRightAndTextLeftTemplateData>(template);

            if (templateData == null)
            {
                return(string.Empty);
            }

            string output = templateData.ImageRightAndTextLeftTemplate;

            if (string.IsNullOrWhiteSpace(output))
            {
                return(string.Empty);
            }

            var resizedImageUrl = _newsletterBuilderHelperService.GetResizedImage(item.ImageUrl, new Size {
                Width = 151
            });

            output = ImageRegex.Replace(output, _urlHelper.ToAbsolute(resizedImageUrl));
            output = TextRegex.Replace(output, item.Text ?? string.Empty);
            return(output);
        }
예제 #8
0
        public async Task Start(NewsletterSettings settings, bool test)
        {
            if (!settings.Enabled)
            {
                return;
            }
            var template = await _templateRepo.GetTemplate(NotificationAgent.Email, NotificationType.Newsletter);

            if (!template.Enabled)
            {
                return;
            }

            var emailSettings = await _emailSettings.GetSettingsAsync();

            if (!ValidateConfiguration(emailSettings))
            {
                return;
            }

            var customization = await _customizationSettings.GetSettingsAsync();

            // Get the Content
            var plexContent = _plex.GetAll().Include(x => x.Episodes).AsNoTracking();
            var embyContent = _emby.GetAll().Include(x => x.Episodes).AsNoTracking();

            var addedLog              = _recentlyAddedLog.GetAll();
            var addedPlexMovieLogIds  = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
            var addedEmbyMoviesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Parent).Select(x => x.ContentId);

            var addedPlexEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Episode).Select(x => x.ContentId);
            var addedEmbyEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Episode).Select(x => x.ContentId);

            // Filter out the ones that we haven't sent yet
            var plexContentMoviesToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && !addedPlexMovieLogIds.Contains(x.Id));
            var embyContentMoviesToSend = embyContent.Where(x => x.Type == EmbyMediaType.Movie && !addedEmbyMoviesLogIds.Contains(x.Id));

            var plexEpisodesToSend = _plex.GetAllEpisodes().Include(x => x.Series).Where(x => !addedPlexEpisodesLogIds.Contains(x.Id)).AsNoTracking();
            var embyEpisodesToSend = _emby.GetAllEpisodes().Include(x => x.Series).Where(x => !addedEmbyEpisodesLogIds.Contains(x.Id)).AsNoTracking();

            var body = string.Empty;

            if (test)
            {
                var plexm = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie).OrderByDescending(x => x.AddedAt).Take(10);
                var embym = embyContent.Where(x => x.Type == EmbyMediaType.Movie).OrderByDescending(x => x.AddedAt).Take(10);
                var plext = _plex.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.Series.AddedAt).Take(10);
                var embyt = _emby.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.AddedAt).Take(10);
                body = await BuildHtml(plexm, embym, plext, embyt);
            }
            else
            {
                body = await BuildHtml(plexContentMoviesToSend, embyContentMoviesToSend, plexEpisodesToSend, embyEpisodesToSend);

                if (body.IsNullOrEmpty())
                {
                    return;
                }
            }

            if (!test)
            {
                // Get the users to send it to
                var users = await _userManager.GetUsersInRoleAsync(OmbiRoles.RecievesNewsletter);

                if (!users.Any())
                {
                    return;
                }
                var emailTasks = new List <Task>();
                foreach (var user in users)
                {
                    if (user.Email.IsNullOrEmpty())
                    {
                        continue;
                    }

                    var messageContent = ParseTemplate(template, customization, user);
                    var email          = new NewsletterTemplate();

                    var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo);

                    emailTasks.Add(_email.Send(
                                       new NotificationMessage {
                        Message = html, Subject = messageContent.Subject, To = user.Email
                    },
                                       emailSettings));
                }

                // Now add all of this to the Recently Added log
                var recentlyAddedLog = new HashSet <RecentlyAddedLog>();
                foreach (var p in plexContentMoviesToSend)
                {
                    recentlyAddedLog.Add(new RecentlyAddedLog
                    {
                        AddedAt     = DateTime.Now,
                        Type        = RecentlyAddedType.Plex,
                        ContentType = ContentType.Parent,
                        ContentId   = p.Id
                    });
                }

                foreach (var p in plexEpisodesToSend)
                {
                    recentlyAddedLog.Add(new RecentlyAddedLog
                    {
                        AddedAt     = DateTime.Now,
                        Type        = RecentlyAddedType.Plex,
                        ContentType = ContentType.Episode,
                        ContentId   = p.Id
                    });
                }

                foreach (var e in embyContentMoviesToSend)
                {
                    if (e.Type == EmbyMediaType.Movie)
                    {
                        recentlyAddedLog.Add(new RecentlyAddedLog
                        {
                            AddedAt     = DateTime.Now,
                            Type        = RecentlyAddedType.Emby,
                            ContentType = ContentType.Parent,
                            ContentId   = e.Id
                        });
                    }
                }

                foreach (var p in embyEpisodesToSend)
                {
                    recentlyAddedLog.Add(new RecentlyAddedLog
                    {
                        AddedAt     = DateTime.Now,
                        Type        = RecentlyAddedType.Emby,
                        ContentType = ContentType.Episode,
                        ContentId   = p.Id
                    });
                }
                await _recentlyAddedLog.AddRange(recentlyAddedLog);

                await Task.WhenAll(emailTasks.ToArray());
            }
            else
            {
                var admins = await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin);

                foreach (var a in admins)
                {
                    if (a.Email.IsNullOrEmpty())
                    {
                        continue;
                    }
                    var messageContent = ParseTemplate(template, customization, a);

                    var email = new NewsletterTemplate();

                    var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo);

                    await _email.Send(
                        new NotificationMessage { Message = html, Subject = messageContent.Subject, To = a.Email },
                        emailSettings);
                }
            }
        }
예제 #9
0
 public NewsletterActionResult(Newsletter newsletter, NewsletterTemplate templateOverride = null)
 {
     _newsletter = newsletter;
     _template   = templateOverride ?? newsletter.NewsletterTemplate;
 }
 public void Delete(NewsletterTemplate newsletterTemplate)
 {
     _session.Transact(session => session.Delete(newsletterTemplate));
 }
 public void Edit(NewsletterTemplate newsletterTemplate)
 {
     _session.Transact(session => session.Update(newsletterTemplate));
 }
 public void Add(NewsletterTemplate newsletterTemplate)
 {
     _session.Transact(session => session.Save(newsletterTemplate));
 }
        public RedirectToRouteResult Delete_POST(NewsletterTemplate newsletterTemplate)
        {
            _newsletterTemplateAdminService.Delete(newsletterTemplate);

            return(RedirectToAction("Index"));
        }
 public RedirectToRouteResult Edit_POST(NewsletterTemplate newsletterTemplate)
 {
     _newsletterTemplateAdminService.Edit(newsletterTemplate);
     TempData.SuccessMessages().Add("Template Edited");
     return(RedirectToAction("Edit", new { id = newsletterTemplate.Id }));
 }
예제 #15
0
        public async Task Start(NewsletterSettings settings, bool test)
        {
            if (!settings.Enabled)
            {
                return;
            }
            var template = await _templateRepo.GetTemplate(NotificationAgent.Email, NotificationType.Newsletter);

            if (!template.Enabled)
            {
                return;
            }

            await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
            .SendAsync(NotificationHub.NotificationEvent, "Newsletter Started");

            var emailSettings = await _emailSettings.GetSettingsAsync();

            if (!ValidateConfiguration(emailSettings))
            {
                await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
                .SendAsync(NotificationHub.NotificationEvent, "Newsletter Email Settings Not Configured");

                return;
            }

            try
            {
                var plexSettings = await _plexSettings.GetSettingsAsync();

                var embySettings = await _embySettings.GetSettingsAsync();

                var jellyfinSettings = await _jellyfinSettings.GetSettingsAsync();

                var customization = await _customizationSettings.GetSettingsAsync();

                var moviesContents = new List <IMediaServerContent>();
                var seriesContents = new List <IMediaServerEpisode>();
                if (plexSettings.Enable)
                {
                    moviesContents.AddRange(await GetMoviesContent(_plex, test));
                    seriesContents.AddRange(GetSeriesContent(_plex, test));
                }
                if (embySettings.Enable)
                {
                    moviesContents.AddRange(await GetMoviesContent(_emby, test));
                    seriesContents.AddRange(GetSeriesContent(_emby, test));
                }
                if (jellyfinSettings.Enable)
                {
                    moviesContents.AddRange(await GetMoviesContent(_jellyfin, test));
                    seriesContents.AddRange(GetSeriesContent(_jellyfin, test));
                }

                var albumsContents = GetMusicContent(_lidarrAlbumRepository, test);

                var body = await BuildHtml(moviesContents, seriesContents, albumsContents, settings);

                if (body.IsNullOrEmpty())
                {
                    return;
                }

                if (!test)
                {
                    var users = new List <OmbiUser>();
                    foreach (var emails in settings.ExternalEmails)
                    {
                        users.Add(new OmbiUser
                        {
                            UserName = emails,
                            Email    = emails
                        });
                    }

                    // Get the users to send it to
                    users.AddRange(await _userManager.GetUsersInRoleAsync(OmbiRoles.ReceivesNewsletter));
                    if (!users.Any())
                    {
                        return;
                    }

                    var messageContent = ParseTemplate(template, customization);
                    var email          = new NewsletterTemplate();

                    foreach (var user in users.DistinctBy(x => x.Email))
                    {                        // Get the users to send it to
                        if (user.Email.IsNullOrEmpty())
                        {
                            continue;
                        }

                        var url  = GenerateUnsubscribeLink(customization.ApplicationUrl, user.Id);
                        var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo, url);

                        var bodyBuilder = new BodyBuilder
                        {
                            HtmlBody = html,
                        };

                        var message = new MimeMessage
                        {
                            Body    = bodyBuilder.ToMessageBody(),
                            Subject = messageContent.Subject
                        };

                        // Send the message to the user
                        message.To.Add(new MailboxAddress(user.Email.Trim(), user.Email.Trim()));

                        // Send the email
                        await _email.Send(message, emailSettings);
                    }

                    // Now add all of this to the Recently Added log
                    var recentlyAddedLog = new HashSet <RecentlyAddedLog>();
                    AddToRecentlyAddedLog(moviesContents, recentlyAddedLog);
                    AddToRecentlyAddedLog(seriesContents, recentlyAddedLog);
                    await _recentlyAddedLog.AddRange(recentlyAddedLog);
                }
                else
                {
                    var admins = await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin);

                    foreach (var a in admins)
                    {
                        if (a.Email.IsNullOrEmpty())
                        {
                            continue;
                        }

                        var unsubscribeLink = GenerateUnsubscribeLink(customization.ApplicationUrl, a.Id);

                        var messageContent = ParseTemplate(template, customization);

                        var email = new NewsletterTemplate();

                        var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo, unsubscribeLink);

                        await _email.Send(
                            new NotificationMessage { Message = html, Subject = messageContent.Subject, To = a.Email },
                            emailSettings);
                    }
                }
            }
            catch (Exception e)
            {
                await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
                .SendAsync(NotificationHub.NotificationEvent, "Newsletter Failed");

                _log.LogError(e, "Error when attempting to create newsletter");
                throw;
            }

            await _notification.Clients.Clients(NotificationHub.AdminConnectionIds)
            .SendAsync(NotificationHub.NotificationEvent, "Newsletter Finished");
        }
예제 #16
0
    private string GetHeader(NewsletterTemplate template, AdaptiveNewsletter newsletter)
    {
        StringBuilder sb = new StringBuilder();
        //background-color: #FFFFFF; border-top: 0px solid #333333; border-bottom: 10px solid #FFFFFF;
        Dictionary<string,string> styleDict = new Dictionary<string,string>();
        styleDict.AddKeyValueOrDefault("background-color", template.HeaderBackgroundColor, "#ffffff");
        styleDict.AddKeyValueOrDefault("height", template.HeaderHeight.ToString()+"px", "");
        styleDict.AddKeyValueOrDefault("line-height", template.HeaderHeight.ToString()+"px", "");
        styleDict.AddKeyValueOrDefault("font-family", template.TitleFont, template.BodyTextFont);
        styleDict.AddKeyValueOrDefault("font-size", template.TitleTextSize.ToString().Substring(4) + "px", "36px");

        sb.AppendLine("<td style=\""+GetStylesStringFromDictionary(styleDict)+"\">");
        sb.AppendLine("<center>");
        string linkStart = "";
        string linkEnd = "";
        if (template.LogoLink.LinkType != WAF.Engine.Property.LinkType.Empty) { linkStart = "<a href=\"" + template.LogoLink.GetUrl() + "\" style=\"text-decoration:none;\">"; linkEnd = "</a>"; }
        if (template.LogoFile.ContainsFile()) {
            if(template.HeaderHeight > 0){
                sb.AppendLine(linkStart + template.LogoFile.GetImageHtml(template.ContentWidth - (template.ElementSideMargin*2),template.HeaderHeight) + linkEnd);
            }else{
                sb.AppendLine(linkStart + template.LogoFile.GetImageHtml(template.ContentWidth - (template.ElementSideMargin*2)) + linkEnd);
            }
        } else {
            sb.AppendLine("<h1 style=\"font-size:" + template.TitleTextSize.ToString().Substring(4) + "px;font-family:" + template.TitleFont + ";margin:0px;\">");
            sb.Append(linkStart);
            sb.AppendLine(newsletter.Title);
            sb.AppendLine(linkEnd);
            sb.AppendLine("</h1>");
        }
        sb.AppendLine("</td>");
        return sb.ToString();
    }
예제 #17
0
 private string GetFooterText(NewsletterTemplate template)
 {
     return "<span style=\"font-size: "+ template.FooterTextSize+"px; color: "+template.UnsubscribeTextColor+"; font-family: "+template.BodyTextFont+";\">"+template.FooterText+"</span>";
 }
예제 #18
0
        public async Task Start(NewsletterSettings settings, bool test)
        {
            if (!settings.Enabled)
            {
                return;
            }
            var template = await _templateRepo.GetTemplate(NotificationAgent.Email, NotificationType.Newsletter);

            if (!template.Enabled)
            {
                return;
            }

            var emailSettings = await _emailSettings.GetSettingsAsync();

            if (!ValidateConfiguration(emailSettings))
            {
                return;
            }

            try
            {
                var customization = await _customizationSettings.GetSettingsAsync();

                // Get the Content
                var plexContent   = _plex.GetAll().Include(x => x.Episodes).AsNoTracking();
                var embyContent   = _emby.GetAll().Include(x => x.Episodes).AsNoTracking();
                var lidarrContent = _lidarrAlbumRepository.GetAll().Where(x => x.FullyAvailable).AsNoTracking();

                var addedLog              = _recentlyAddedLog.GetAll();
                var addedPlexMovieLogIds  = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId).ToHashSet();
                var addedEmbyMoviesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Parent).Select(x => x.ContentId).ToHashSet();
                var addedAlbumLogIds      = addedLog.Where(x => x.Type == RecentlyAddedType.Lidarr && x.ContentType == ContentType.Album).Select(x => x.AlbumId).ToHashSet();

                var addedPlexEpisodesLogIds =
                    addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Episode);
                var addedEmbyEpisodesLogIds =
                    addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Episode);


                // Filter out the ones that we haven't sent yet
                var plexContentMoviesToSend   = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && x.HasTheMovieDb && !addedPlexMovieLogIds.Contains(StringHelper.IntParseLinq(x.TheMovieDbId)));
                var embyContentMoviesToSend   = embyContent.Where(x => x.Type == EmbyMediaType.Movie && x.HasTheMovieDb && !addedEmbyMoviesLogIds.Contains(StringHelper.IntParseLinq(x.TheMovieDbId)));
                var lidarrContentAlbumsToSend = lidarrContent.Where(x => !addedAlbumLogIds.Contains(x.ForeignAlbumId)).ToHashSet();
                _log.LogInformation("Plex Movies to send: {0}", plexContentMoviesToSend.Count());
                _log.LogInformation("Emby Movies to send: {0}", embyContentMoviesToSend.Count());
                _log.LogInformation("Albums to send: {0}", lidarrContentAlbumsToSend.Count());

                var plexEpisodesToSend =
                    FilterPlexEpisodes(_plex.GetAllEpisodes().Include(x => x.Series).Where(x => x.Series.HasTvDb).AsNoTracking(), addedPlexEpisodesLogIds);
                var embyEpisodesToSend = FilterEmbyEpisodes(_emby.GetAllEpisodes().Include(x => x.Series).Where(x => x.Series.HasTvDb).AsNoTracking(),
                                                            addedEmbyEpisodesLogIds);

                _log.LogInformation("Plex Episodes to send: {0}", plexEpisodesToSend.Count());
                _log.LogInformation("Emby Episodes to send: {0}", embyEpisodesToSend.Count());
                var plexSettings = await _plexSettings.GetSettingsAsync();

                var embySettings = await _embySettings.GetSettingsAsync();

                var body = string.Empty;
                if (test)
                {
                    var plexm  = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie).OrderByDescending(x => x.AddedAt).Take(10);
                    var embym  = embyContent.Where(x => x.Type == EmbyMediaType.Movie).OrderByDescending(x => x.AddedAt).Take(10);
                    var plext  = _plex.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.Series.AddedAt).Take(10).ToHashSet();
                    var embyt  = _emby.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.AddedAt).Take(10).ToHashSet();
                    var lidarr = lidarrContent.OrderByDescending(x => x.AddedAt).Take(10).ToHashSet();
                    body = await BuildHtml(plexm, embym, plext, embyt, lidarr, settings, embySettings, plexSettings);
                }
                else
                {
                    body = await BuildHtml(plexContentMoviesToSend, embyContentMoviesToSend, plexEpisodesToSend, embyEpisodesToSend, lidarrContentAlbumsToSend, settings, embySettings, plexSettings);

                    if (body.IsNullOrEmpty())
                    {
                        return;
                    }
                }

                if (!test)
                {
                    // Get the users to send it to
                    var users = await _userManager.GetUsersInRoleAsync(OmbiRoles.ReceivesNewsletter);

                    if (!users.Any())
                    {
                        return;
                    }

                    foreach (var emails in settings.ExternalEmails)
                    {
                        users.Add(new OmbiUser
                        {
                            UserName = emails,
                            Email    = emails
                        });
                    }

                    var messageContent = ParseTemplate(template, customization);
                    var email          = new NewsletterTemplate();

                    var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo);

                    var bodyBuilder = new BodyBuilder
                    {
                        HtmlBody = html,
                    };

                    var message = new MimeMessage
                    {
                        Body    = bodyBuilder.ToMessageBody(),
                        Subject = messageContent.Subject
                    };

                    foreach (var user in users)
                    {
                        // Get the users to send it to
                        if (user.Email.IsNullOrEmpty())
                        {
                            continue;
                        }
                        // BCC the messages
                        message.Bcc.Add(new MailboxAddress(user.Email, user.Email));
                    }

                    // Send the email
                    await _email.Send(message, emailSettings);

                    // Now add all of this to the Recently Added log
                    var recentlyAddedLog = new HashSet <RecentlyAddedLog>();
                    foreach (var p in plexContentMoviesToSend)
                    {
                        recentlyAddedLog.Add(new RecentlyAddedLog
                        {
                            AddedAt     = DateTime.Now,
                            Type        = RecentlyAddedType.Plex,
                            ContentType = ContentType.Parent,
                            ContentId   = StringHelper.IntParseLinq(p.TheMovieDbId),
                        });
                    }

                    foreach (var p in plexEpisodesToSend)
                    {
                        recentlyAddedLog.Add(new RecentlyAddedLog
                        {
                            AddedAt       = DateTime.Now,
                            Type          = RecentlyAddedType.Plex,
                            ContentType   = ContentType.Episode,
                            ContentId     = StringHelper.IntParseLinq(p.Series.TvDbId),
                            EpisodeNumber = p.EpisodeNumber,
                            SeasonNumber  = p.SeasonNumber
                        });
                    }
                    foreach (var e in embyContentMoviesToSend)
                    {
                        if (e.Type == EmbyMediaType.Movie)
                        {
                            recentlyAddedLog.Add(new RecentlyAddedLog
                            {
                                AddedAt     = DateTime.Now,
                                Type        = RecentlyAddedType.Emby,
                                ContentType = ContentType.Parent,
                                ContentId   = StringHelper.IntParseLinq(e.TheMovieDbId),
                            });
                        }
                    }

                    foreach (var p in embyEpisodesToSend)
                    {
                        recentlyAddedLog.Add(new RecentlyAddedLog
                        {
                            AddedAt       = DateTime.Now,
                            Type          = RecentlyAddedType.Emby,
                            ContentType   = ContentType.Episode,
                            ContentId     = StringHelper.IntParseLinq(p.Series.TvDbId),
                            EpisodeNumber = p.EpisodeNumber,
                            SeasonNumber  = p.SeasonNumber
                        });
                    }
                    await _recentlyAddedLog.AddRange(recentlyAddedLog);
                }
                else
                {
                    var admins = await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin);

                    foreach (var a in admins)
                    {
                        if (a.Email.IsNullOrEmpty())
                        {
                            continue;
                        }
                        var messageContent = ParseTemplate(template, customization);

                        var email = new NewsletterTemplate();

                        var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo);

                        await _email.Send(
                            new NotificationMessage { Message = html, Subject = messageContent.Subject, To = a.Email },
                            emailSettings);
                    }
                }
            }
            catch (Exception e)
            {
                _log.LogError(e, "Error when attempting to create newsletter");
                throw;
            }
        }
 public ViewResult Edit(NewsletterTemplate newsletterTemplate)
 {
     ViewData["template-data-options"] = _newsletterTemplateAdminService.GetTemplateDataOptions();
     return(View(newsletterTemplate));
 }
 public ViewResult Delete(NewsletterTemplate newsletterTemplate)
 {
     return(View(newsletterTemplate));
 }