Skip to content

Instantly share code, notes, and snippets.

@JRondeau16
Last active October 8, 2015 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JRondeau16/5e71dcf35cb8bdfc4ee5 to your computer and use it in GitHub Desktop.
Save JRondeau16/5e71dcf35cb8bdfc4ee5 to your computer and use it in GitHub Desktop.
Custom Workflow Email Action
using System.Net.Mail;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Workflows.Simple;
using Sitecore.Links;
namespace YOURPROJECTNAMESPACE
{
public class EmailAdmin
{
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("$itemPath$", args.DataItem.Paths.FullPath);
text = text.Replace("$itemLanguage$", args.DataItem.Language.ToString());
text = text.Replace("$itemVersion$", args.DataItem.Version.ToString());
text = text.Replace("$itemUrl$", AbsoluteUrl(args.DataItem, true)
.Replace("/sitecore", string.Empty)
.Replace("/shell", string.Empty));
return text;
}
private static string AbsoluteUrl(Item item, bool alwaysIncludeServer = false)
{
if (item == null) return string.Empty;
var options = (UrlOptions)UrlOptions.DefaultOptions.Clone();
options.AlwaysIncludeServerUrl = alwaysIncludeServer;
options.SiteResolving = true;
return LinkManager.GetItemUrl(item, options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment