Personalized Emails with Kentico EMS

Personalized Emails with Kentico EMS

We had a few scenarios where Email had to be personalized. The situation is pretty simple. The Marketing team is responsible for sending out Emails but Sales team should be contacted first. So we have the option of sending out Emails with a generic sales contact which is fine if we don’t have a sales team. What if we want the client to contact their sales representative? How to personalize that Email so it doesn’t look generic?

Our idea is pretty simple - we want  every email that goes out to look like that it was sent by responsible sales person. The Email should be sent from a sales email (example: and on the bottom of the email content we want to add the contact details (example: John Doe, +1 022 333 444).

For this example, we will use the Kentico Dancing Goat site installed on your local machine.

Add new user (Sales person)

Open Users in Kentico and create a new user with following information:

Full Name: John Doe
Enabled: No
Role: Administrator

You might be asking why the user is disabled - this is optional, because our sales person won’t use our Kentico site, therefore we can disable him…. :)

Add some contacts (Subscriber aka Customer)

We will handle our Email subscribers using Kentico’s contact management application. Go to Contact management and add a new Contact to the Dancing Goat site:

First Name: Anna
Last Name: Green
Email address:
Contact Owner:

This part, where we’re assigning a contact owner is important because it forms our relationship between a customer and the sales representative.

Setup newsletter

Go to Email marketing and create a new Email campaign. If you’re not familiar with Email marketing in Kentico, please see the Kentico documentation here:

Here are the parameters for the Newsletter:

Email campaign display name: Ninja Sales Mega Test
Sender name: General Sales
Sender email address

We are using the Dancing Goat site for this example, so we can leave the default settings for Email campaign template, Subscribe and Unsubscribe.

After our email campaign is created, let’s create a new email with the following content:

Subject: Mega Personalization
Title: Mega Personalization
Text: Dear
{% FirstName %}

we want to offer you…
{{% Subscriber.Contact.ContactOwnerUser.FullName #%}

Territory sales
+ 1 002 333 444
{{% Subscriber.Contact.ContactOwnerUser.Email #%}

As you can see from my Text content I’m using 3 macros to get relevant data.  Macros can be inserted by clicking on the {#} icon.

Here is explanation of used Macros:

{% FirstName %} 
– this will bring subscriber first name
{{% Subscriber.Contact.ContactOwnerUser.FullName #%}
– this will return contact owner full name
{{% Subscriber.Contact.ContactOwnerUser.Email #%}
– this will return contact owner email

As you can see I have no added macros for Position and Phone because I didn’t populate that data but you can add it. This would be a great practice.

The last remaining thing is to add a subscriber. Go to Email marketing, select your Ninja Sales Mega Test and on right side click on Subscribers. Then you can click on Add contact Anna Green.

Add some code

Open your Kentico installation folder and look for the App_code folder. Copy the following code and paste it inside or download code from here, unzip it and paste it.

Code that you downloaded or copied to App_code is following:

using CMS.Base;
using CMS.DataEngine;
using CMS.EmailEngine;
using CMS.EventLog;
using CMS.Membership;
using CMS.OnlineMarketing;

public partial class CMSModuleLoader
    private class EmailEvents : CMSLoaderAttribute
        public override void Init()
			//This attaches an event that fires before the email gets inserted in the queue
			EmailInfo.TYPEINFO.Events.Insert.Before += Insert_Before;
        private void Insert_Before(object sender, ObjectEventArgs e)
			//Get the email that is about to be inserted
			var emailObject = e.Object as EmailInfo;
            ContactInfo recipient = ContactInfoProvider.GetContacts().Columns("ContactOwnerUserID", "ContactEmail").WhereEquals("ContactEmail", emailObject.EmailTo).FirstObject;
            UserInfo contactOwner = UserInfoProvider.GetUserInfo(recipient.ContactOwnerUserID);

            if (contactOwner != null)
			//Set the contact owner as the From email
			emailObject.EmailFrom = contactOwner.Email;
          EventLogProvider.LogInformation("EMAIL", "ERROR", "Email not sent because contactOwner is empty: " + recipient.ContactEmail);

This code will intercept any outgoing e-mail in queue and it will replace the default email sender address ( the email address of our sales representative: (


We need to test our code and our new functionality. I prefer to do that on my development instance (localhost). For that purpose, we need some localhost SMTP that can capture our outgoing emails. I like using Papercut and you can download it from here:

After you install and run PaperCut go back to Kentico Email Marketing, open your email Mega Personalization and click on Send button.

Now your e-mail is waiting to be sent in the email queue. You can go there and hit resend to speed up your test.

Now you should see your e-mail in PaperCut. If you did it correctly, the email is sent from and on bottom you should see the resolved contact details.

How it works?

Kentico will send out the email that will end up in the email queue. Our code will insert our John Doe’s e-mail on matching contact. This will work for any contact that has a contact owner assigned .

Please note that this customization could have performance impact!

Additional ideas for personalization

Here are a few ideas that you can try out yourself.

  • Sales Photo
  • Sales Center map
  • Sales Phone
  • Sales working hours

However, for these examples you will need to customize the user and add some HTML code in your newsletter.

Feel free to leave feedback and comments below.