Email marketing


List of examples:

Email marketing templates

Creating an email template




// Creates a new template object
EmailTemplateInfo newTemplate = new EmailTemplateInfo()
{
    // Sets the basic template properties
    TemplateDisplayName = "New email template",
    TemplateName = "NewEmailTemplate",
    TemplateSiteID = SiteContext.CurrentSiteID,

    // Sets the template type (template for marketing email content in this case)
    TemplateType = EmailTemplateType.Issue,
    // Other possible template type values: EmailTemplateType.Subscription, EmailTemplateType.Unsubscription, EmailTemplateType.DoubleOptIn

    // Defines the content of the template
    TemplateHeader = @"<html xmlns=""http://www.w3.org/1999/xhtml"">
                       <head>
                         <title>Newsletter</title>
                         <meta http-equiv=""content-type"" content=""text/html; charset=UTF-8"" />
                       </head>
                       <body>",
    TemplateBody = "Email template content",
    TemplateFooter = "</body></html>"
};

// Saves the new template to the database
EmailTemplateInfoProvider.SetEmailTemplateInfo(newTemplate);


> Back to list of examples

Updating an email template




// Gets the email template
EmailTemplateInfo updateTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("NewEmailTemplate", SiteContext.CurrentSiteID);

if (updateTemplate != null)
{
    // Updates the template properties
    updateTemplate.TemplateDisplayName = updateTemplate.TemplateDisplayName.ToLower();

    // Saves the updated template to the database
    EmailTemplateInfoProvider.SetEmailTemplateInfo(updateTemplate);
}


> Back to list of examples

Updating multiple email templates




// Gets all email marketing templates of the "marketing email" type whose code name starts with 'New'
var templates = EmailTemplateInfoProvider.GetEmailTemplates()
                                            .WhereEquals("TemplateType", EmailTemplateType.Issue)
                                            .WhereStartsWith("TemplateName", "New");

// Loops through individual email templates
foreach (EmailTemplateInfo template in templates)
{
    // Updates the template properties
    template.TemplateDisplayName = template.TemplateDisplayName.ToUpper();

    // Saves the updated template to the database
    EmailTemplateInfoProvider.SetEmailTemplateInfo(template);
}


> Back to list of examples

Deleting an email template




// Gets the email template
EmailTemplateInfo deleteTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("NewEmailTemplate", SiteContext.CurrentSiteID);

if (deleteTemplate != null)
{
    // Deletes the email template
    EmailTemplateInfoProvider.DeleteEmailTemplateInfo(deleteTemplate);
}


> Back to list of examples

Email feeds

Creating a template-based newsletter




// Gets templates for the newsletter's emails
EmailTemplateInfo subscriptionTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleSubscriptionEmailTemplate", SiteContext.CurrentSiteID);
EmailTemplateInfo unsubscriptionTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleUnsubscriptionEmailTemplate", SiteContext.CurrentSiteID);
EmailTemplateInfo emailTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleEmailTemplate", SiteContext.CurrentSiteID);

if ((subscriptionTemplate != null) && (unsubscriptionTemplate != null) && (emailTemplate != null))
{
    // Creates a new email feed object
    NewsletterInfo newTemplateNewsletter = new NewsletterInfo()
    {
        // Sets the email feed properties to configure a template-based newsletter
        NewsletterType = EmailCommunicationTypeEnum.Newsletter,
        NewsletterSource = NewsletterSource.TemplateBased,
        NewsletterDisplayName = "New templated newsletter",
        NewsletterName = "NewTemplateNewsletter",
        NewsletterSenderName = "Sender name",
        NewsletterSenderEmail = "sender@localhost.local",
        NewsletterSiteID = SiteContext.CurrentSiteID,

        // Assigns email templates to the newsletter
        NewsletterSubscriptionTemplateID = subscriptionTemplate.TemplateID,
        NewsletterUnsubscriptionTemplateID = unsubscriptionTemplate.TemplateID,
        NewsletterTemplateID = emailTemplate.TemplateID
    };

    // Saves the new email feed to the database
    NewsletterInfoProvider.SetNewsletterInfo(newTemplateNewsletter);
}


> Back to list of examples

Creating a page-based newsletter




// Gets templates for the newsletter's subscription messages
EmailTemplateInfo subscriptionTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleSubscriptionEmailTemplate", SiteContext.CurrentSiteID);
EmailTemplateInfo unsubscriptionTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleUnsubscriptionEmailTemplate", SiteContext.CurrentSiteID);

if ((subscriptionTemplate != null) && (unsubscriptionTemplate != null))
{
    // Creates a new email feed object
    NewsletterInfo newPageNewsletter = new NewsletterInfo()
    {
        // Sets the email feed properties to configure a page-based newsletter
        NewsletterType = EmailCommunicationTypeEnum.Newsletter,
        NewsletterSource = NewsletterSource.Dynamic,
        NewsletterDisplayName = "New page newsletter",
        NewsletterName = "NewPageNewsletter",
        NewsletterSenderName = "Sender name",
        NewsletterSenderEmail = "sender@localhost.local",
        NewsletterDynamicURL = "http://www.MyWebsite.com/NewsletterContent",
        NewsletterDynamicSubject = "Newsletter issue",
        NewsletterSiteID = SiteContext.CurrentSiteID,

        // Assigns subscription email templates to the newsletter
        NewsletterSubscriptionTemplateID = subscriptionTemplate.TemplateID,
        NewsletterUnsubscriptionTemplateID = unsubscriptionTemplate.TemplateID
    };

    // Saves the new email feed to the database
    NewsletterInfoProvider.SetNewsletterInfo(newPageNewsletter);
}


> Back to list of examples

Creating an email campaign




// Gets templates for the email campaign
EmailTemplateInfo subscriptionTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleSubscriptionEmailTemplate", SiteContext.CurrentSiteID);
EmailTemplateInfo unsubscriptionTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleUnsubscriptionEmailTemplate", SiteContext.CurrentSiteID);
EmailTemplateInfo emailTemplate = EmailTemplateInfoProvider.GetEmailTemplateInfo("SampleEmailTemplate", SiteContext.CurrentSiteID);

if ((subscriptionTemplate != null) && (unsubscriptionTemplate != null) && (emailTemplate != null))
{
    // Creates a new email feed object
    NewsletterInfo newEmailCampaign = new NewsletterInfo()
    {
        // Sets the email feed properties to configure an email campaign
        NewsletterType = EmailCommunicationTypeEnum.EmailCampaign,
        NewsletterSource = NewsletterSource.TemplateBased,
        NewsletterDisplayName = "New email campaign",
        NewsletterName = "NewEmailCampaign",
        NewsletterSenderName = "Sender name",
        NewsletterSenderEmail = "sender@localhost.local",
        NewsletterSiteID = SiteContext.CurrentSiteID,

        // Assigns templates to the email campaign
        NewsletterSubscriptionTemplateID = subscriptionTemplate.TemplateID,
        NewsletterUnsubscriptionTemplateID = unsubscriptionTemplate.TemplateID,
        NewsletterTemplateID = emailTemplate.TemplateID
    };

    // Saves the new email feed to the database
    NewsletterInfoProvider.SetNewsletterInfo(newEmailCampaign);
}


> Back to list of examples

Updating an email feed (any type)




// Gets the email feed
NewsletterInfo updateEmailFeed = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (updateEmailFeed != null)
{
    // Updates the email feed properties
    updateEmailFeed.NewsletterDisplayName = updateEmailFeed.NewsletterDisplayName.ToLower();

    // Saves the updated email feed to the database
    NewsletterInfoProvider.SetNewsletterInfo(updateEmailFeed);
}


> Back to list of examples

Updating multiple email feeds (any type)




// Gets all email feeds on the current site whose code name starts with 'New'
var emailFeeds = NewsletterInfoProvider.GetNewsletters()
                                                .WhereEquals("NewsletterSiteID", SiteContext.CurrentSiteID)
                                                .WhereStartsWith("NewsletterName", "New");

// Loops through individual email feeds
foreach (NewsletterInfo emailFeed in emailFeeds)
{
    // Updates the email feed properties
    emailFeed.NewsletterDisplayName = emailFeed.NewsletterDisplayName.ToUpper();

    // Saves the updated email feed to the database
    NewsletterInfoProvider.SetNewsletterInfo(emailFeed);
}


> Back to list of examples

Deleting an email feed (any type)




// Gets the email feed
NewsletterInfo deleteEmailFeed = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (deleteEmailFeed != null)
{
    // Deletes the email feed
    NewsletterInfoProvider.DeleteNewsletterInfo(deleteEmailFeed);
}


> Back to list of examples

Marketing emails

Creating template-based marketing emails




// Gets the email feed (Template-based newsletter or Email campaign)
NewsletterInfo emailFeed = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (emailFeed != null)
{
    // Creates a new email object
    IssueInfo newIssue = new IssueInfo()
    {
        // Sets the email properties
        IssueSubject = "New issue",
        IssueNewsletterID = emailFeed.NewsletterID,
        IssueSiteID = SiteContext.CurrentSiteID,
        IssueStatus = IssueStatusEnum.Idle,
        IssueUnsubscribed = 0,
        IssueSentEmails = 0,
        IssueTemplateID = emailFeed.NewsletterTemplateID,
        IssueShowInNewsletterArchive = false,
        IssueUseUTM = false,

        // Defines the email content
        // The ID attributes of <region> elements must match the IDs of regions used in the given email template
        IssueText = "<?xml version=\"1.0\" encoding=\"utf-16\"?><content><region id=\"text\">Issue text</region></content>"
    };

    // Saves the marketing email to the database
    IssueInfoProvider.SetIssueInfo(newIssue);
}


> Back to list of examples

Creating page-based newsletter emails




// Gets the page-based newsletter
NewsletterInfo pageNewsletter = NewsletterInfoProvider.GetNewsletterInfo("NewPageNewsletter", SiteContext.CurrentSiteID);

if (pageNewsletter != null)
{
    // Generates a new dynamic issue (email)
    EmailQueueManager.GenerateDynamicIssue(pageNewsletter.NewsletterID);
}


> Back to list of examples

Updating marketing emails




// Gets the email feed
NewsletterInfo emailFeed = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (emailFeed != null)
{
    // Gets all of the feed's emails that have not been sent yet
    var issues = IssueInfoProvider.GetIssues()
                                        .WhereEquals("IssueNewsletterID", emailFeed.NewsletterID)
                                        .WhereNull("IssueStatus");

    // Loops through individual emails
    foreach (IssueInfo issue in issues)
    {
        // Updates the email properties
        issue.IssueSubject = issue.IssueSubject.ToUpper();

        // Saves the modified email to the database
        IssueInfoProvider.SetIssueInfo(issue);
    }
}


> Back to list of examples

Deleting marketing emails




// Gets the email feed
NewsletterInfo emailFeed = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (emailFeed != null)
{
    // Gets all of the feed's emails that were already sent
    var issues = IssueInfoProvider.GetIssues()
                                        .WhereEquals("IssueNewsletterID", emailFeed.NewsletterID)
                                        .WhereEquals("IssueStatus", IssueStatusEnum.Finished);

    // Loops through individual emails
    foreach (IssueInfo deleteIssue in issues)
    {
        // Deletes the email
        IssueInfoProvider.DeleteIssueInfo(deleteIssue);
    }
}


> Back to list of examples

Recipients

Adding newsletter recipients by email address




// Prepares the email address of the new recipient
string emailAddress = "subscriber@localhost.local";

// Gets the newsletter
NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (newsletter != null)
{
    // Prepares instances of the Kentico contact provider and email feed subscription service
    IContactProvider contactProvider = CMS.Core.Service<IContactProvider>.Entry();
    ISubscriptionService subscriptionService = CMS.Core.Service<ISubscriptionService>.Entry();

    // Either gets an existing contact by email address or creates a new contact with the given email address
    ContactInfo contact = contactProvider.GetContactForSubscribing(emailAddress);

    // Adds the contact as a recipient of the newsletter
    subscriptionService.Subscribe(contact, newsletter, new SubscribeSettings
    {
        AllowOptIn = true, // Allows double opt-in subscription for newsletters that have it enabled
        SendConfirmationEmail = true, // Allows sending of confirmation emails for the subscription

        // Removes the email address from the opt-out list for all marketing emails on the given site (if present)
        RemoveAlsoUnsubscriptionFromAllNewsletters = true,
    });
}


> Back to list of examples

Adding contacts as newsletter recipients




// Gets all contacts whose last name is 'Smith'
var contacts = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith");

// Gets the newsletter
NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (newsletter != null)
{
    // Prepares an instance of the Kentico email feed subscription service
    ISubscriptionService subscriptionService = CMS.Core.Service<ISubscriptionService>.Entry();

    // Loops through the contacts
    foreach (ContactInfo contact in contacts)
    {
        // Adds the contact as a recipient of the newsletter
        subscriptionService.Subscribe(contact, newsletter, new SubscribeSettings
        {
            AllowOptIn = true, // Allows double opt-in subscription for newsletters that have it enabled
            SendConfirmationEmail = true, // Allows sending of confirmation emails for the subscription

            // Removes the contact from the opt-out list for all marketing emails on the given site (if present)
            RemoveAlsoUnsubscriptionFromAllNewsletters = true,
        });
    }
}


> Back to list of examples

Adding newsletter recipients via contact groups




// Gets the contact group
ContactGroupInfo contactGroup = ContactGroupInfoProvider.GetContactGroupInfo("GroupName");

// Gets the newsletter
NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if ((contactGroup != null) && (newsletter != null))
{
    // Prepares an instance of the Kentico email feed subscription service
    ISubscriptionService subscriptionService = CMS.Core.Service<ISubscriptionService>.Entry();

    // Adds the contact group as a recipient of the newsletter
    subscriptionService.Subscribe(contactGroup, newsletter);
}


> Back to list of examples

Unsubscribing an email address from a newsletter




// Prepares the email address that you wish to unsubscribe
string emailAddress = "subscriber@localhost.local";

// Gets the newsletter
NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if (newsletter != null)
{
    // Prepares an instance of the Kentico email feed subscription service
    ISubscriptionService subscriptionService = CMS.Core.Service<ISubscriptionService>.Entry();

    // Ensures that the email address no longer receives the newsletter's emails
    // Leaves the email address in the newsletter's list of recipients, but with the "Opted out" status
    subscriptionService.UnsubscribeFromSingleNewsletter(emailAddress, newsletter.NewsletterID, null, sendConfirmationEmail: true);
}


> Back to list of examples

Removing a newsletter recipient




// Gets the subscriber that you wish to remove from the list of recipients (by email address)
SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberByEmail("subscriber@localhost.local", SiteContext.CurrentSiteID);

// Gets the newsletter
NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("NewTemplateNewsletter", SiteContext.CurrentSiteID);

if ((subscriber != null) && (newsletter != null))
{
    // Prepares an instance of the Kentico email feed subscription service
    ISubscriptionService subscriptionService = CMS.Core.Service<ISubscriptionService>.Entry();

    // Removes the subscriber from the newsletter's recipients
    subscriptionService.RemoveSubscription(subscriber.SubscriberID, newsletter.NewsletterID, sendConfirmationEmail : true);
}


> Back to list of examples

Adding email campaign recipients (contact groups)




// Gets the contact group
ContactGroupInfo contactGroup = ContactGroupInfoProvider.GetContactGroupInfo("GroupName");

// Gets the email campaign
NewsletterInfo emailCampaign = NewsletterInfoProvider.GetNewsletterInfo("NewEmailCampaign", SiteContext.CurrentSiteID);

if ((contactGroup != null) && (emailCampaign != null))
{
    // Gets the campaign's email with the subject "New issue"
    IssueInfo campaignEmail = IssueInfoProvider.GetIssues()
                                        .WhereEquals("IssueNewsletterID", emailCampaign.NewsletterID)
                                        .WhereEquals("IssueSubject", "New issue")
                                        .FirstObject;

    if (campaignEmail != null)
    {
        // Creates an object representing a relationship between the campaign email and contact group
        var emailContactGroupBinding = new IssueContactGroupInfo
        {
            ContactGroupID = contactGroup.ContactGroupID,
            IssueID = campaignEmail.IssueID
        };

        // Assigns the contact group as a recipient for the campaign email
        IssueContactGroupInfoProvider.SetIssueContactGroupInfo(emailContactGroupBinding);
    }
}


> Back to list of examples

Removing email campaign recipients (contact groups)




// Gets the contact group
ContactGroupInfo contactGroup = ContactGroupInfoProvider.GetContactGroupInfo("GroupName");

// Gets the email campaign
NewsletterInfo emailCampaign = NewsletterInfoProvider.GetNewsletterInfo("NewEmailCampaign", SiteContext.CurrentSiteID);

if ((contactGroup != null) && (emailCampaign != null))
{
    // Gets the campaign's email with the subject "New issue"
    IssueInfo campaignEmail = IssueInfoProvider.GetIssues()
                                        .WhereEquals("IssueNewsletterID", emailCampaign.NewsletterID)
                                        .WhereEquals("IssueSubject", "New issue")
                                        .FirstObject;

    if (campaignEmail != null)
    {
        // Gets the object representing the relationship between the campaign email and contact group
        IssueContactGroupInfo emailContactGroupBinding =
            IssueContactGroupInfoProvider.GetIssueContactGroupInfo(campaignEmail.IssueID, contactGroup.ContactGroupID);

        if (emailContactGroupBinding != null)
        {
            // Removes the contact group from the campaign email's recipient list
            IssueContactGroupInfoProvider.DeleteIssueContactGroupInfo(emailContactGroupBinding);
        }
    }
}


> Back to list of examples