Valid for Sitecore 5.1.1 and 5.2
1.  Implementing Extended Mail Action Using NVelocity

Extended mail action extends the functionality of the standard Sitecore workflow mail notification by using the NVelocity template engine. Standard Sitecore email action supports three hard-coded macro expansions: $itemPath$, $itemLanguage$ and $itemVersion$. So instead of sending a static mail notification saying that something had been changed somewhere, it is possible to create a message saying, for example, "Hello. $item.Name item (located at $item.Paths.Path) had been changed".

The NVelocity template engine allows to implement the functionality mentioned above. Below you can see a sample extended message template: 

Time: $time

State of the $item.Name item (located at $item.Paths.Path) had been changed from '$state.DisplayName' to '$nextState' by $user.Name ($user.Email).

Item workflow history:

#foreach ($historyItem in $history)

Date: $historyItem.Date | User: $historyItem.User | Action: $historyItem.Text

#end

The resulting message is shown below:

Time: 2/14/2006 3:57:47 PM
State of the Sample Item item (located at /sitecore/content/Home/Test Item/Sample Item) had been changed from 'Reviewing' to 'Final' by Admin ().
Item workflow history:
Date: 2/14/2006 12:09:44 PM | User: sitecore\Admin | Action: Item created
Date: 2/14/2006 12:10:28 PM | User: sitecore\Admin | Action: no comment
Date: 2/14/2006 12:10:41 PM | User: sitecore\Admin | Action: no comments
Date: 2/14/2006 3:57:35 PM | User: sitecore\Admin | Action: no comments

The item was last updated on 2/14/2006 3:57:02 PM.

At first sight it may look like an additional number of hardcoded macros, but the features NVelocity templates provide are much more powerful: using them you can code against the Sitecore API using the Sitecore client as long as the method/property evaluates to string. So now you don't have to start Visual Studio every time you need to change your custom template.

Below is the step-by-step instruction on how to implement Extended Mail Action in your solution.

Select one of the links below to download the ready-made package and source code example for this article:

1.1.  Create a Custom Workflow Action

Please refer to the article below for a detailed explanation of creating workflow action:

How to cause the workflow to invoke an action

The mailaction sample package contains all Sitecore Items required. The Extended Mail action is placed under the System/Workflows Item by the packager; you should place it under a certain command to make use of this action.

1.2.  Create a Class for Generating and Sending Emails

You can download the full source code for this example.

Below is a code snippet which describes how to define the substitutions for the NVelocity template:

      /// <summary>
      
/// Populates the velocity template context. Only the objects that were
      
/// added in this method will be accessible in the mail template.
      
/// </summary>
      
/// <remarks>Override this to add your own data to the context</remarks>
      protected virtual void PopulateContext(WorkflowPipelineArgs args)
      {
         velocityContext.Put(
"args", args);
         velocityContext.Put(
"item", args.DataItem);
         velocityContext.Put(
"processor", args.ProcessorItem);
         velocityContext.Put(
"user", Sitecore.Context.User);
         velocityContext.Put(
"history", args.DataItem.State.GetWorkflow().GetHistory(args.DataItem));
         velocityContext.Put(
"state", args.DataItem.State.GetWorkflowState());
         velocityContext.Put(
"nextState", GetNextState(args));
         velocityContext.Put(
"site", Sitecore.Context.Site);
         velocityContext.Put(
"time", DateTime.Now);
      }

1.3.  Mail Action at Work

You should follow the instructions given in this article to set up a custom workflow action.

/upload/sdn5/shared library/api/using nvelocity/mail action at work.png

 

After the action is set up, you should fill the fields of the Data section with appropriate values. You can use the substitutions defined in the PopulateContext method in the subject field and message body.

The Type field is used for referencing the Mail Action library.

/upload/sdn5/shared library/api/using nvelocity/mail action at work 02.png