Exemple #1
0
        public async Task <ICommandResult <EmailMessage> > SendSecurityTokenAsync(Models.SignUp signUp)
        {
            // Get reset password email
            var culture = await _contextFacade.GetCurrentCultureAsync();

            var email = await _localeStore.GetFirstOrDefaultByKeyAsync <LocaleEmail>(culture, "SignUpSecurityToken");

            if (email != null)
            {
                var subject = string.Format(email.Subject, signUp.SecurityToken);
                var body    = string.Format(email.Message, signUp.SecurityToken);

                var message = new MailMessage()
                {
                    Subject    = subject,
                    Body       = WebUtility.HtmlDecode(body),
                    IsBodyHtml = true
                };

                message.To.Add(signUp.Email);

                // send email
                return(await _emailManager.SaveAsync(message));
            }

            var result = new CommandResult <EmailMessage>();

            return(result.Failed("An error occurred whilst attempting to send the security token email."));
        }
Exemple #2
0
        public async Task <ICommandResult <Idea> > SendAsync(INotificationContext <Idea> context)
        {
            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(EmailNotifications.NewLabel.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <Idea>();

            // Get email template
            const string templateId = "NewIdeaLabel";

            // Tasks run in a background thread and don't have access to HttpContext
            // Create a dummy principal to represent the user so we can still obtain
            // the current culture for the email
            var principal = await _claimsPrincipalFactory.CreateAsync((User)context.Notification.To);

            var culture = await _contextFacade.GetCurrentCultureAsync(principal.Identity);

            var email = await _localeStore.GetFirstOrDefaultByKeyAsync <LocaleEmail>(culture, templateId);

            if (email != null)
            {
                // Build topic url
                var baseUri = await _capturedRouterUrlHelper.GetBaseUrlAsync();

                var url = _capturedRouterUrlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
                {
                    ["area"]       = "Plato.Ideas",
                    ["controller"] = "Home",
                    ["action"]     = "Display",
                    ["opts.id"]    = context.Model.Id,
                    ["opts.alias"] = context.Model.Alias
                });

                // Build message from template
                var message = email.BuildMailMessage();
                message.Body = string.Format(
                    email.Message,
                    context.Notification.To.DisplayName,
                    context.Model.Title,
                    baseUri + url);
                message.IsBodyHtml = true;
                message.To.Add(new MailAddress(context.Notification.To.Email));

                // Send message
                var emailResult = await _emailManager.SaveAsync(message);

                if (emailResult.Succeeded)
                {
                    return(result.Success(context.Model));
                }

                return(result.Failed(emailResult.Errors?.ToArray()));
            }

            return(result.Failed($"No email template with the Id '{templateId}' exists within the 'locales/{culture}/emails.json' file!"));
        }
Exemple #3
0
        public async Task <ICommandResult <EmailMessage> > SendPasswordResetTokenAsync(IUser user)
        {
            // Get reset password email
            var culture = await _contextFacade.GetCurrentCultureAsync();

            var email = await _localeStore.GetFirstOrDefaultByKeyAsync <LocaleEmail>(culture, "ResetPassword");

            if (email != null)
            {
                // Build reset password link
                var baseUrl = await _contextFacade.GetBaseUrlAsync();

                var callbackUrl = baseUrl + _contextFacade.GetRouteUrl(new RouteValueDictionary()
                {
                    ["area"]       = "Plato.Users",
                    ["controller"] = "Account",
                    ["action"]     = "ResetPassword",
                    ["code"]       = user.ResetToken
                });

                var body = string.Format(email.Message, user.DisplayName, callbackUrl);

                var message = new MailMessage()
                {
                    Subject    = email.Subject,
                    Body       = WebUtility.HtmlDecode(body),
                    IsBodyHtml = true
                };

                message.To.Add(user.Email);

                // send email
                return(await _emailManager.SaveAsync(message));
            }

            var result = new CommandResult <EmailMessage>();

            return(result.Failed("An error occurred whilst attempting to send the password reset token email."));
        }
Exemple #4
0
        public async Task <ICommandResult <ReportSubmission <Answer> > > SendAsync(INotificationContext <ReportSubmission <Answer> > context)
        {
            // Ensure correct notification provider
            if (!context.Notification.Type.Name.Equals(EmailNotifications.AnswerReport.Name, StringComparison.Ordinal))
            {
                return(null);
            }

            // Create result
            var result = new CommandResult <ReportSubmission <Answer> >();

            // Get email template
            const string templateId = "NewAnswerReport";

            // Tasks run in a background thread and don't have access to HttpContext
            // Create a dummy principal to represent the user so we can still obtain
            // the current culture for the email
            var principal = await _claimsPrincipalFactory.CreateAsync((User)context.Notification.To);

            var culture = await _contextFacade.GetCurrentCultureAsync(principal.Identity);

            var email = await _localeStore.GetFirstOrDefaultByKeyAsync <LocaleEmail>(culture, templateId);

            if (email == null)
            {
                return(result.Failed(
                           $"No email template with the Id '{templateId}' exists within the 'locales/{culture}/emails.json' file!"));
            }

            // Get topic for reply
            var topic = await _entityStore.GetByIdAsync(context.Model.What.EntityId);

            // We need an topic for the reply
            if (topic == null)
            {
                return(result.Failed(
                           $"No entity with id '{context.Model.What.EntityId}' exists. Failed to send reply spam email notification."));
            }

            // Build topic url
            var baseUri = await _capturedRouterUrlHelper.GetBaseUrlAsync();

            var url = _capturedRouterUrlHelper.GetRouteUrl(baseUri, new RouteValueDictionary()
            {
                ["area"]         = "Plato.Questions",
                ["controller"]   = "Home",
                ["action"]       = "Reply",
                ["opts.id"]      = topic.Id,
                ["opts.alias"]   = topic.Alias,
                ["opts.replyId"] = context.Model.What.Id
            });

            // Reason given text
            var reasonText = S["None Provided"];

            if (ReportReasons.Reasons.ContainsKey(context.Model.Why))
            {
                reasonText = S[ReportReasons.Reasons[context.Model.Why]];
            }

            // Build message from template
            var message = email.BuildMailMessage();

            message.Body = string.Format(
                email.Message,
                context.Notification.To.DisplayName,
                topic.Title,
                reasonText.Value,
                context.Model.Who.DisplayName,
                context.Model.Who.UserName,
                baseUri + url);

            message.IsBodyHtml = true;
            message.To.Add(new MailAddress(context.Notification.To.Email));

            // Send message
            var emailResult = await _emailManager.SaveAsync(message);

            if (emailResult.Succeeded)
            {
                return(result.Success(context.Model));
            }

            return(result.Failed(emailResult.Errors?.ToArray()));
        }
        public async Task <ICommandResult <FileInvite> > SendAttachmentInviteAsync(FileInvite invite)
        {
            if (invite == null)
            {
                throw new ArgumentException(nameof(invite));
            }

            // Create result
            var result = new CommandResult <FileInvite>();

            if (invite.FileId <= 0)
            {
                return(result.Failed(T["A file is required to share"].Value));
            }

            if (string.IsNullOrEmpty(invite.Email))
            {
                return(result.Failed(T["An email address is required"].Value));
            }

            // Get file
            var file = await _fileStore.GetByIdAsync(invite.FileId);

            // Ensure we found the file
            if (file == null)
            {
                return(result.Failed(T["The file could not be found"].Value));
            }

            // Get email template
            const string templateId = "ShareFileAttachment";
            var          culture    = await _contextFacade.GetCurrentCultureAsync();

            var email = await _localeStore.GetFirstOrDefaultByKeyAsync <LocaleEmail>(culture, templateId);

            if (email != null)
            {
                // Build message
                var message = email.BuildMailMessage();
                message.Subject = string.Format(
                    email.Subject,
                    invite.CreatedBy.DisplayName);
                message.Body = string.Format(
                    email.Message,
                    invite.CreatedBy.DisplayName);
                message.IsBodyHtml = true;
                message.To.Add(new MailAddress(invite.Email.Trim()));
                message.Attachments.Add(file.ToAttachment());

                // Send message
                var emailResult = await _emailManager.SaveAsync(message);

                if (emailResult.Succeeded)
                {
                    return(result.Success(invite));
                }

                return(result.Failed(emailResult.Errors?.ToArray()));
            }

            return(result.Failed($"No email template with the Id '{templateId}' exists within the 'locales/{culture}/emails.json' file!"));
        }