Exemplo n.º 1
0
        /// <summary>
        /// Sends an email notification out for the workflow process
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="emailType">the type of email to be sent</param>
        public async void Send(WorkflowInstancePoco instance, EmailType emailType)
        {
            WorkflowSettingsPoco settings = _settingsService.GetSettings();

            if (!settings.SendNotifications)
            {
                return;
            }

            if (!instance.TaskInstances.Any())
            {
                instance.TaskInstances = _tasksService.GetTasksWithGroupByInstanceGuid(instance.Guid);
            }

            if (!instance.TaskInstances.Any())
            {
                Log.Error($"Notifications not sent - no tasks exist for instance { instance.Id }");
                return;
            }

            WorkflowTaskPoco finalTask = null;

            try
            {
                string docTitle = instance.Node.Name;
                string docUrl   = UrlHelpers.GetFullyQualifiedContentEditorUrl(instance.NodeId);

                WorkflowTaskPoco[] flowTasks = instance.TaskInstances.OrderBy(t => t.ApprovalStep).ToArray();

                // always take get the emails for all previous users, sometimes they will be discarded later
                // easier to just grab em all, rather than doing so conditionally
                List <string> emailsForAllTaskUsers = new List <string>();

                // in the loop, also store the last task to a variable, and keep the populated group
                var taskIndex = 0;
                int taskCount = flowTasks.Length;

                foreach (WorkflowTaskPoco task in flowTasks)
                {
                    taskIndex += 1;

                    UserGroupPoco group = await _groupService.GetPopulatedUserGroupAsync(task.GroupId);

                    if (group == null)
                    {
                        continue;
                    }

                    emailsForAllTaskUsers.AddRange(group.PreferredEmailAddresses());
                    if (taskIndex != taskCount)
                    {
                        continue;
                    }

                    finalTask           = task;
                    finalTask.UserGroup = group;
                }

                if (finalTask == null)
                {
                    Log.Error("No valid task found for email notifications");
                    return;
                }

                List <string> to = new List <string>();

                var    body                = "";
                string typeDescription     = instance.WorkflowType.Description(instance.ScheduledDate);
                string typeDescriptionPast = instance.WorkflowType.DescriptionPastTense(instance.ScheduledDate);

                switch (emailType)
                {
                case EmailType.ApprovalRequest:
                    to   = finalTask.UserGroup.PreferredEmailAddresses();
                    body = string.Format(EmailApprovalRequestString,
                                         to.Count > 1 ? "Umbraco user" : finalTask.UserGroup.Name, docUrl, docTitle, instance.AuthorComment,
                                         instance.AuthorUser.Name, typeDescription, string.Empty);
                    break;

                case EmailType.ApprovalRejection:
                    to = emailsForAllTaskUsers;
                    to.Add(instance.AuthorUser.Email);
                    body = string.Format(EmailRejectedString,
                                         "Umbraco user", docUrl, docTitle, finalTask.Comment,
                                         finalTask.ActionedByUser.Name, typeDescription.ToLower());

                    break;

                case EmailType.ApprovedAndCompleted:
                    to = emailsForAllTaskUsers;
                    to.Add(instance.AuthorUser.Email);

                    //Notify web admins
                    to.Add(settings.Email);

                    if (instance.WorkflowType == WorkflowType.Publish)
                    {
                        IPublishedContent n = _utility.GetPublishedContent(instance.NodeId);
                        docUrl = UrlHelpers.GetFullyQualifiedSiteUrl(n.Url);
                    }

                    body = string.Format(EmailApprovedString,
                                         "Umbraco user", docUrl, docTitle,
                                         typeDescriptionPast.ToLower()) + "<br/>";

                    body += instance.BuildProcessSummary();

                    break;

                case EmailType.ApprovedAndCompletedForScheduler:
                    to = emailsForAllTaskUsers;
                    to.Add(instance.AuthorUser.Email);

                    body = string.Format(EmailApprovedString,
                                         "Umbraco user", docUrl, docTitle,
                                         typeDescriptionPast.ToLower()) + "<br/>";

                    body += instance.BuildProcessSummary();

                    break;

                case EmailType.WorkflowCancelled:
                    to = emailsForAllTaskUsers;

                    // include the initiator email
                    to.Add(instance.AuthorUser.Email);

                    body = string.Format(EmailCancelledString,
                                         "Umbraco user", typeDescription, docUrl, docTitle, finalTask.ActionedByUser.Name, finalTask.Comment);
                    break;

                case EmailType.SchedulerActionCancelled:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(emailType), emailType, null);
                }

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

                var client = new SmtpClient();
                var msg    = new MailMessage
                {
                    Subject    = $"{emailType.ToString().ToTitleCase()} - {instance.Node.Name} ({typeDescription})",
                    IsBodyHtml = true,
                };

                if (settings.Email.HasValue())
                {
                    msg.From = new MailAddress(settings.Email);
                }

                // if offline is permitted, email group members individually as we need the user id in the url
                if (emailType == EmailType.ApprovalRequest && finalTask.UserGroup.OfflineApproval)
                {
                    foreach (User2UserGroupPoco user in finalTask.UserGroup.Users)
                    {
                        string offlineString = string.Format(EmailOfflineApprovalString, settings.SiteUrl, instance.NodeId,
                                                             user.UserId, finalTask.Id, instance.Guid);

                        body = string.Format(EmailApprovalRequestString,
                                             user.User.Name, docUrl, docTitle, instance.AuthorComment,
                                             instance.AuthorUser.Name, typeDescription, offlineString);

                        msg.To.Clear();
                        msg.To.Add(user.User.Email);
                        msg.Body = string.Format(EmailBody, msg.Subject, body);

                        client.Send(msg);
                    }
                }
                else
                {
                    msg.To.Add(string.Join(",", to.Distinct()));
                    msg.Body = string.Format(EmailBody, msg.Subject, body);

                    client.Send(msg);
                }

                Log.Info($"Email notifications sent for task { finalTask.Id }, to { msg.To }");
            }
            catch (Exception e)
            {
                Log.Error($"Error sending notifications for task { finalTask.Id }", e);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends an email notification out for the workflow process
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="emailType">the type of email to be sent</param>
        /// <param name="errorDetail"></param>
        public async Task <string> Send(WorkflowInstancePoco instance, EmailType emailType, string errorDetail = "")
        {
            var msg = new MailMessage();

            if (!_settings.SendNotifications)
            {
                return(null);
            }

            if (!instance.TaskInstances.Any())
            {
                instance.TaskInstances = _tasksService.GetTasksWithGroupByInstanceGuid(instance.Guid);
            }

            if (!instance.TaskInstances.Any())
            {
                Log.Error($"Notifications not sent - no tasks exist for instance { instance.Id }");
                return(null);
            }

            try
            {
                WorkflowTaskPoco[] flowTasks = instance.TaskInstances.OrderBy(t => t.ApprovalStep).ToArray();

                // always take get the emails for all previous users, sometimes they will be discarded later
                // easier to just grab em all, rather than doing so conditionally
                List <string> emailsForAllTaskUsers = new List <string>();

                // in the loop, also store the last task to a variable, and keep the populated group
                var taskIndex = 0;
                int taskCount = flowTasks.Length;

                foreach (WorkflowTaskPoco task in flowTasks)
                {
                    taskIndex += 1;

                    UserGroupPoco group = await _groupService.GetPopulatedUserGroupAsync(task.GroupId);

                    if (group == null)
                    {
                        continue;
                    }

                    emailsForAllTaskUsers.AddRange(group.PreferredEmailAddresses());
                    if (taskIndex != taskCount)
                    {
                        continue;
                    }

                    _finalTask           = task;
                    _finalTask.UserGroup = group;
                }

                if (_finalTask == null)
                {
                    Log.Error("No valid task found for email notifications");
                    return(null);
                }

                // populate list of recipients
                List <string> to = GetRecipients(emailType, instance, emailsForAllTaskUsers);
                if (!to.Any())
                {
                    return(null);
                }

                string body = GetBody(emailType, instance, out string typeDescription, errorDetail);

                var client = new SmtpClient();

                msg = new MailMessage
                {
                    Subject    = $"{emailType.ToString().ToTitleCase()} - {instance.Node.Name} ({typeDescription})",
                    IsBodyHtml = true,
                    Body       = string.Format(EmailBody, msg.Subject, body)
                };

                if (_settings.Email.HasValue())
                {
                    msg.From = new MailAddress(_settings.Email);
                }

                // if offline is permitted, email group members individually as we need the user id in the url
                if (emailType == EmailType.ApprovalRequest && _finalTask.UserGroup.OfflineApproval)
                {
                    string docTitle = instance.Node.Name;
                    string docUrl   = UrlHelpers.GetFullyQualifiedContentEditorUrl(instance.NodeId);

                    foreach (User2UserGroupPoco user in _finalTask.UserGroup.Users)
                    {
                        var msgBody = body + string.Format(EmailOfflineApprovalString, _settings.SiteUrl, instance.NodeId,
                                                           user.UserId, _finalTask.Id, instance.Guid);

                        msg.Body = string.Format(EmailBody, msg.Subject, msgBody);

                        msg.To.Clear();
                        msg.To.Add(user.User.Email);

                        client.Send(msg);
                    }
                }
                else
                {
                    msg.To.Add(string.Join(",", to.Distinct()));
                    client.Send(msg);
                }

                Log.Info($"Email notifications sent for task { _finalTask.Id }, to { msg.To }");
            }
            catch (Exception e)
            {
                Log.Error($"Error sending notifications for task { _finalTask.Id }", e);
            }

            return(msg.Body);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends an email notification out for the workflow process
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="emailType">the type of email to be sent</param>
        public async void Send(WorkflowInstancePoco instance, EmailType emailType)
        {
            WorkflowSettingsPoco settings = _settingsService.GetSettings();

            bool?doSend = settings.SendNotifications;

            if (doSend != true)
            {
                return;
            }

            if (!instance.TaskInstances.Any())
            {
                instance.TaskInstances = _tasksService.GetTasksWithGroupByInstanceGuid(instance.Guid);
            }

            try
            {
                string docTitle = instance.Node.Name;
                string docUrl   = UrlHelpers.GetFullyQualifiedContentEditorUrl(instance.NodeId);

                IOrderedEnumerable <WorkflowTaskInstancePoco> flowTasks = instance.TaskInstances.OrderBy(t => t.ApprovalStep);

                // always take get the emails for all previous users, sometimes they will be discarded later
                // easier to just grab em all, rather than doing so conditionally
                List <string> emailsForAllTaskUsers = new List <string>();

                // in the loop, also store the last task to a variable, and keep the populated group
                var taskIndex = 0;
                int taskCount = flowTasks.Count();

                WorkflowTaskInstancePoco finalTask = null;

                foreach (WorkflowTaskInstancePoco task in flowTasks)
                {
                    taskIndex += 1;

                    UserGroupPoco group = await _groupService.GetPopulatedUserGroupAsync(task.GroupId);

                    if (group == null)
                    {
                        continue;
                    }

                    emailsForAllTaskUsers.AddRange(group.PreferredEmailAddresses());
                    if (taskIndex != taskCount)
                    {
                        continue;
                    }

                    finalTask           = task;
                    finalTask.UserGroup = group;
                }

                List <string> to = new List <string>();
                string        systemEmailAddress = settings.Email;

                var body = "";

                switch (emailType)
                {
                case EmailType.ApprovalRequest:
                    to   = finalTask.UserGroup.PreferredEmailAddresses();
                    body = string.Format(EmailApprovalRequestString,
                                         to.Count > 1 ? "Umbraco user" : finalTask.UserGroup.Name, docUrl, docTitle, instance.AuthorComment,
                                         instance.AuthorUser.Name, instance.TypeDescription);

                    break;

                case EmailType.ApprovalRejection:
                    to = emailsForAllTaskUsers;
                    to.Add(instance.AuthorUser.Email);
                    body = string.Format(EmailRejectedString,
                                         "Umbraco user", docUrl, docTitle, finalTask.Comment,
                                         finalTask.ActionedByUser.Name, instance.TypeDescription.ToLower());

                    break;

                case EmailType.ApprovedAndCompleted:
                    to = emailsForAllTaskUsers;
                    to.Add(instance.AuthorUser.Email);

                    //Notify web admins
                    to.Add(systemEmailAddress);

                    if (instance.WorkflowType == WorkflowType.Publish)
                    {
                        IPublishedContent n = _utility.GetPublishedContent(instance.NodeId);
                        docUrl = UrlHelpers.GetFullyQualifiedSiteUrl(n.Url);
                    }

                    body = string.Format(EmailApprovedString,
                                         "Umbraco user", docUrl, docTitle,
                                         instance.TypeDescriptionPastTense.ToLower()) + "<br/>";

                    body += BuildProcessSummary(instance);

                    break;

                case EmailType.ApprovedAndCompletedForScheduler:
                    to = emailsForAllTaskUsers;
                    to.Add(instance.AuthorUser.Email);

                    body = string.Format(EmailApprovedString,
                                         "Umbraco user", docUrl, docTitle,
                                         instance.TypeDescriptionPastTense.ToLower()) + "<br/>";

                    body += BuildProcessSummary(instance);

                    break;

                case EmailType.WorkflowCancelled:
                    to = emailsForAllTaskUsers;

                    // include the initiator email
                    to.Add(instance.AuthorUser.Email);

                    body = string.Format(EmailCancelledString,
                                         "Umbraco user", instance.TypeDescription, docUrl, docTitle, finalTask.ActionedByUser.Name, finalTask.Comment);
                    break;

                case EmailType.SchedulerActionCancelled:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(emailType), emailType, null);
                }

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

                var client = new SmtpClient();
                var msg    = new MailMessage();

                if (!string.IsNullOrEmpty(systemEmailAddress))
                {
                    msg.From = new MailAddress(systemEmailAddress);
                }

                string subject = BuildEmailSubject(emailType, instance);

                msg.To.Add(string.Join(",", to.Distinct()));
                msg.Subject    = subject;
                msg.Body       = $"<!DOCTYPE HTML SYSTEM><html><head><title>{subject}</title></head><body><font face=\"verdana\" size=\"2\">{body}</font></body></html>";
                msg.IsBodyHtml = true;

                client.Send(msg);
            }
            catch (Exception e)
            {
                Log.Error("Error sending notifications", e);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Builds the email body based on the email type
        /// </summary>
        /// <param name="emailType"></param>
        /// <param name="instance"></param>
        /// <param name="useGenericName"></param>
        /// <param name="typeDescription"></param>
        /// <param name="errorDetail"></param>
        /// <returns></returns>
        private string GetBody(EmailType emailType, WorkflowInstancePoco instance, out string typeDescription, string errorDetail = "")
        {
            var body = "";

            typeDescription = instance.WorkflowType.Description(instance.ScheduledDate);
            string typeDescriptionPast = instance.WorkflowType.DescriptionPastTense(instance.ScheduledDate);
            string docTitle            = instance.Node.Name;
            string docUrl = UrlHelpers.GetFullyQualifiedContentEditorUrl(instance.NodeId);

            switch (emailType)
            {
            case EmailType.ApprovalRequest:
                body = string.Format(EmailApprovalRequestString, docUrl, docTitle, typeDescription.ToLower());

                break;

            case EmailType.ApprovalRejection:
                body = string.Format(EmailRejectedString, docUrl, docTitle, _finalTask.Comment,
                                     _finalTask.ActionedByUser.Name, typeDescription.ToLower());

                break;

            case EmailType.ApprovedAndCompleted:
                if (instance.WorkflowType == WorkflowType.Publish)
                {
                    IPublishedContent n = _utility.GetPublishedContent(instance.NodeId);
                    docUrl = UrlHelpers.GetFullyQualifiedSiteUrl(n.Url);
                }

                body = string.Format(EmailApprovedString, docUrl, docTitle,
                                     typeDescriptionPast.ToLower()) + "<br/>";

                break;

            case EmailType.ApprovedAndCompletedForScheduler:
                body = string.Format(EmailApprovedString, docUrl, docTitle,
                                     typeDescriptionPast.ToLower()) + "<br/>";

                break;

            case EmailType.WorkflowCancelled:
                body = string.Format(EmailCancelledString, typeDescription, docUrl, docTitle, _finalTask.ActionedByUser.Name);

                break;

            case EmailType.WorkflowErrored:
                body = string.Format(EmailErroredString, typeDescription, docUrl, docTitle, errorDetail);

                break;

            case EmailType.SchedulerActionCancelled:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(emailType), emailType, null);
            }

            body += instance.BuildProcessSummary();

            return(body);
        }