Ejemplo n.º 1
0
        private void StartNewsLetter(NewletterSettings newletterSettings, bool testEmail = false)
        {
            var embySettings = EmbySettings.GetSettings();

            if (embySettings.Enable)
            {
                var html = EmbyNewsletter.GetNewsletterHtml(testEmail);

                var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray());
                Log.Debug(escapedHtml);
                SendNewsletter(newletterSettings, escapedHtml, testEmail, "New Content On Emby!");
            }
            else
            {
                var plexSettings = PlexSettings.GetSettings();
                if (plexSettings.Enable)
                {
                    var html = PlexNewsletter.GetNewsletterHtml(testEmail);

                    var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray());
                    Log.Debug(escapedHtml);
                    SendNewsletter(newletterSettings, html, testEmail);
                }
            }
        }
Ejemplo n.º 2
0
        private void StartNewsLetter(NewletterSettings newletterSettings, bool testEmail = false)
        {
            var embySettings = EmbySettings.GetSettings();

            if (embySettings.Enable)
            {
                var letter = EmbyNewsletter.GetNewsletter(testEmail) ?? new Newsletter();
                if (letter.Send || testEmail)
                {
                    SendNewsletter(newletterSettings, letter.Html, testEmail, "New Content On Emby!");
                }
                else
                {
                    Log.Warn("There is no new content to send the newsletter");
                }
            }
            else
            {
                var plexSettings = PlexSettings.GetSettings();
                if (plexSettings.Enable)
                {
                    var letter = PlexNewsletter.GetNewsletter(testEmail) ?? new Newsletter();
                    if (letter.Send || testEmail)
                    {
                        SendNewsletter(newletterSettings, letter.Html, testEmail);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void SendNewsletter(NewletterSettings newletterSettings, string html, bool testEmail = false, string subject = "New Content on Plex!")
        {
            Log.Debug("Entering SendNewsletter");
            var settings = EmailSettings.GetSettings();

            if (!settings.Enabled || string.IsNullOrEmpty(settings.EmailHost))
            {
                return;
            }

            var body = new BodyBuilder {
                HtmlBody = html, TextBody = "This email is only available on devices that support HTML."
            };

            var message = new MimeMessage
            {
                Body    = body.ToMessageBody(),
                Subject = subject
            };

            Log.Debug("Created Plain/HTML MIME body");

            if (!testEmail)
            {
                var users = UserHelper.GetUsersWithFeature(Features.Newsletter);
                if (users != null)
                {
                    foreach (var user in users)
                    {
                        if (!string.IsNullOrEmpty(user.EmailAddress))
                        {
                            message.Bcc.Add(new MailboxAddress(user.Username, user.EmailAddress));
                        }
                    }
                }

                if (newletterSettings.CustomUsersEmailAddresses != null &&
                    newletterSettings.CustomUsersEmailAddresses.Any())
                {
                    foreach (var user in newletterSettings.CustomUsersEmailAddresses)
                    {
                        if (!string.IsNullOrEmpty(user))
                        {
                            message.Bcc.Add(new MailboxAddress(user, user));
                        }
                    }
                }
            }

            message.Bcc.Add(new MailboxAddress(settings.EmailUsername, settings.RecipientEmail)); // Include the admin

            message.From.Add(new MailboxAddress(settings.EmailUsername, settings.EmailSender));
            SendMail(settings, message);
        }
Ejemplo n.º 4
0
        private void Start(NewletterSettings newletterSettings, bool testEmail = false)
        {
            var sb           = new StringBuilder();
            var plexSettings = PlexSettings.GetSettings();

            var libs         = Api.GetLibrarySections(plexSettings.PlexAuthToken, plexSettings.FullUri);
            var tvSection    = libs.Directories.FirstOrDefault(x => x.type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase));
            var movieSection = libs.Directories.FirstOrDefault(x => x.type.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase));

            var recentlyAddedTv     = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri, tvSection.Key);
            var recentlyAddedMovies = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri, movieSection.Key);

            GenerateMovieHtml(recentlyAddedMovies, plexSettings, sb);
            GenerateTvHtml(recentlyAddedTv, plexSettings, sb);

            var template = new RecentlyAddedTemplate();
            var html     = template.LoadTemplate(sb.ToString());

            Send(newletterSettings, html, plexSettings, testEmail);
        }
Ejemplo n.º 5
0
        private void Send(NewletterSettings newletterSettings, string html, PlexSettings plexSettings, bool testEmail = false)
        {
            var settings = EmailSettings.GetSettings();

            if (!settings.Enabled || string.IsNullOrEmpty(settings.EmailHost))
            {
                return;
            }

            var body = new BodyBuilder {
                HtmlBody = html, TextBody = "This email is only available on devices that support HTML."
            };
            var message = new MimeMessage
            {
                Body    = body.ToMessageBody(),
                Subject = "New Content on Plex!",
            };

            if (!testEmail)
            {
                if (newletterSettings.SendToPlexUsers)
                {
                    var users = Api.GetUsers(plexSettings.PlexAuthToken);
                    foreach (var user in users.User)
                    {
                        message.Bcc.Add(new MailboxAddress(user.Username, user.Email));
                    }
                }

                if (newletterSettings.CustomUsersEmailAddresses.Any())
                {
                    foreach (var user in newletterSettings.CustomUsersEmailAddresses)
                    {
                        message.Bcc.Add(new MailboxAddress(user, user));
                    }
                }
            }
            message.Bcc.Add(new MailboxAddress(settings.EmailUsername, settings.RecipientEmail)); // Include the admin

            message.From.Add(new MailboxAddress(settings.EmailUsername, settings.EmailSender));
            try
            {
                using (var client = new SmtpClient())
                {
                    client.Connect(settings.EmailHost, settings.EmailPort); // Let MailKit figure out the correct SecureSocketOptions.

                    // Note: since we don't have an OAuth2 token, disable
                    // the XOAUTH2 authentication mechanism.
                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    if (settings.Authentication)
                    {
                        client.Authenticate(settings.EmailUsername, settings.EmailPassword);
                    }
                    Log.Info("sending message to {0} \r\n from: {1}\r\n Are we authenticated: {2}", message.To, message.From, client.IsAuthenticated);
                    client.Send(message);
                    client.Disconnect(true);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Ejemplo n.º 6
0
        private void Start(NewletterSettings newletterSettings, bool testEmail = false)
        {
            var sb           = new StringBuilder();
            var plexSettings = PlexSettings.GetSettings();

            Log.Debug("Got Plex Settings");

            var libs = Api.GetLibrarySections(plexSettings.PlexAuthToken, plexSettings.FullUri);

            Log.Debug("Getting Plex Library Sections");

            var tvSections = libs.Directories.Where(x => x.type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase)); // We could have more than 1 lib

            Log.Debug("Filtered sections for TV");
            var movieSection = libs.Directories.Where(x => x.type.Equals(PlexMediaType.Movie.ToString(), StringComparison.CurrentCultureIgnoreCase)); // We could have more than 1 lib

            Log.Debug("Filtered sections for Movies");

            var plexVersion = Api.GetStatus(plexSettings.PlexAuthToken, plexSettings.FullUri).Version;

            var html = string.Empty;

            if (plexVersion.StartsWith("1.3"))
            {
                var tvMetadata    = new List <Metadata>();
                var movieMetadata = new List <Metadata>();
                foreach (var tvSection in tvSections)
                {
                    var item = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri,
                                                 tvSection?.Key);
                    if (item?.MediaContainer?.Metadata != null)
                    {
                        tvMetadata.AddRange(item?.MediaContainer?.Metadata);
                    }
                }
                Log.Debug("Got RecentlyAdded TV Shows");
                foreach (var movie in movieSection)
                {
                    var recentlyAddedMovies = Api.RecentlyAdded(plexSettings.PlexAuthToken, plexSettings.FullUri, movie?.Key);
                    if (recentlyAddedMovies?.MediaContainer?.Metadata != null)
                    {
                        movieMetadata.AddRange(recentlyAddedMovies?.MediaContainer?.Metadata);
                    }
                }
                Log.Debug("Got RecentlyAdded Movies");

                Log.Debug("Started Generating Movie HTML");
                GenerateMovieHtml(movieMetadata, plexSettings, sb);
                Log.Debug("Finished Generating Movie HTML");
                Log.Debug("Started Generating TV HTML");
                GenerateTvHtml(tvMetadata, plexSettings, sb);
                Log.Debug("Finished Generating TV HTML");

                var template = new RecentlyAddedTemplate();
                html = template.LoadTemplate(sb.ToString());
                Log.Debug("Loaded the template");
            }
            else
            {
                // Old API
                var tvChild    = new List <RecentlyAddedChild>();
                var movieChild = new List <RecentlyAddedChild>();
                foreach (var tvSection in tvSections)
                {
                    var recentlyAddedTv = Api.RecentlyAddedOld(plexSettings.PlexAuthToken, plexSettings.FullUri, tvSection?.Key);
                    if (recentlyAddedTv?._children != null)
                    {
                        tvChild.AddRange(recentlyAddedTv?._children);
                    }
                }

                Log.Debug("Got RecentlyAdded TV Shows");
                foreach (var movie in movieSection)
                {
                    var recentlyAddedMovies = Api.RecentlyAddedOld(plexSettings.PlexAuthToken, plexSettings.FullUri, movie?.Key);
                    if (recentlyAddedMovies?._children != null)
                    {
                        tvChild.AddRange(recentlyAddedMovies?._children);
                    }
                }
                Log.Debug("Got RecentlyAdded Movies");

                Log.Debug("Started Generating Movie HTML");
                GenerateMovieHtml(movieChild, plexSettings, sb);
                Log.Debug("Finished Generating Movie HTML");
                Log.Debug("Started Generating TV HTML");
                GenerateTvHtml(tvChild, plexSettings, sb);
                Log.Debug("Finished Generating TV HTML");

                var template = new RecentlyAddedTemplate();
                html = template.LoadTemplate(sb.ToString());
                Log.Debug("Loaded the template");
            }



            Send(newletterSettings, html, plexSettings, testEmail);
        }