3

I want to be able to send email notifications to list of subscribed users (<500 users) registered under membership provider as soon as a new document is published to content delivery. This can be achieved in a number of ways in Sitecore.

What would be the best way to send this notification without affecting/slowing over all performance of application/publish pipeline/document library.

  1. Use workflow
  2. Subscribe to publish-related events
  3. Add code into the 'publish' pipeline
0

1 Answer 1

4

I would use Workflow in your case.

You need to create and assign different workflow for document templates on their standard values to make this working.

There is an "Email action" OOTB that you can use in your workflow to send emails but you need to create custom class to send emails to your subscribed list.

Just add this action after Auto Publish to your workflow like this and fill in the fields:

enter image description here

Insert text into Message field. Something like:

We have new document for for! Check it out here: $itemUrl$

Change Type field especially to your custom class definition:

enter image description here

Use Your_namespace_goes_here.SubscribersEmailAction, Your_assembly_name_goes_here notation in Type field. SubscribersEmailAction is name of class below.

SubscribersEmailAction Class could be something similar to this code:

using System.Net.Mail;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Workflows.Simple;
using Sitecore.Links;

namespace Your_namespace_goes_here
{
    public class SubscribersEmailAction
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            var processorItem = args.ProcessorItem;
            if (processorItem == null)
                return;

            var commandItem = processorItem.InnerItem;
            var fullPath = commandItem.Paths.FullPath;
            var from = GetText(commandItem, "from", args);
            var to = GetText(commandItem, "to", args);
            var mailServer = GetText(commandItem, "mail server", args);
            var subject = GetText(commandItem, "subject", args);
            var message = GetText(commandItem, "message", args);

            Error.Assert(to.Length > 0, "The 'To' field is not specified in the mail action item: " + fullPath);
            Error.Assert(from.Length > 0, "The 'From' field is not specified in the mail action item: " + fullPath);
            Error.Assert(subject.Length > 0, "The 'Subject' field is not specified in the mail action item: " + fullPath);

            var smtpClient = string.IsNullOrWhiteSpace(mailServer)
                                        ? new SmtpClient()
                                        : new SmtpClient(mailServer); 
            smtpClient.Send(new MailMessage(from, to)
            {
                Subject = subject,
                Body = message
            });
        }

        public string GetText(Item commandItem, string field, WorkflowPipelineArgs args)
        {
            var text = commandItem[field];
            return text.Length > 0 ? ReplaceVariables(text, args) : string.Empty;
        }

        private static string ReplaceVariables(string text, WorkflowPipelineArgs args)
        {
            text = text.Replace("$itemUrl$", MediaUrl(args.DataItem));
            return text;
        }

        private static string MediaUrl(Item item)
        {
            if (item == null) return string.Empty;

            if (!item.Paths.IsMediaItem) return string.Empty;

            var theURL = Sitecore.Resources.Media.MediaManager.GetMediaUrl(item);
            mediaUrl = Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(theURL);
            return mediaUrl;
        }
    }
}

Code inspired by this blog post: https://jeffreyrondeau.wordpress.com/2015/02/25/extending-sitecore-workflow-email-action/

You need to tweak a bit the code because you need to replace "To" field with email addresses from your subscribers list.

2
  • 1
    SPE can also be used! Nov 8, 2018 at 11:19
  • 1
    I agree with @Peter. I had the same requirement and used a very similar approach.
    – Mikeyp
    Nov 9, 2018 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.