Note! You are looking at documentation for an old version of the package. Go here for the latest documentation.

There is a lot of things that can be done using the services and repositories that Newsletter Studio provides. You can explore these APIs using the "GlobalFactory" which is a service locator that the package uses internally.

For example, you can access the Mailing List Repository like this:

NewsletterStudio.Infrastucture.GlobalFactory.Current.MailingListRepository

 

Creating a newsletter programmatically

You can use the GlobalFactory to access the NewsletterRepository to create a Newsletter from your own code. In this example we create a Newsletter and set the ScheduledSendDate so that the automatic background-job for sending will pick it up and send it the next time it fires.

 

public bool CreateNewsletter()
{
    List<int> mailingListIdsToSendTo = new List<int>();
    mailingListIdsToSendTo.Add(7);
    mailingListIdsToSendTo.Add(8);

    var email = new Newsletter();
    email.CreatedByUserId = 0;
    email.Name = "Email from API";
    email.SenderName = "Sender Name";
    email.EmailFrom = "sender@test.com";
    email.EmailSubject = "The Subject";
    email.MessageBody = "<p>This is the body</p>";
    email.SubscriptionAlias = string.Join(",",mailingListIdsToSendTo.Select(x=>$"NewsletterStudioSubscriptionProvider_{x}"));
    // Setting scheduled date to 1 min from now to automatically send
    email.ScheduledSendDate = DateTime.Now.AddMinutes(1);

    GlobalFactory.Current.NewsletterRepository.Save(email);

    return true;
}

This would save the Newsletter to the database and the background-job that sends scheduled emails would pick this up and send the Newsletter the next time it runs. To trigger this job manually, just go to this URL on your domain: /umbraco/NewsletterStudio/Actions/NewsletterCheckForScheduledSendOut

Also note, you might want to double check /config/umbracoSettings.config to make sure that the URLs to the scheduled jobs is correct, URLs might differ in development, stage and production. They should look something like this:

<scheduledTasks>
    <task log="true" alias="ScheduledNewsletterSend" interval="300" url="https://www.your-site.com/umbraco/NewsletterStudio/Actions/NewsletterCheckForScheduledSendOut" />
    <task log="true" alias="ScheduledNewsletterBounce" interval="840" url="https://www.your-site.com/App_Plugins/NewsletterStudio/Pages/HandleBounces.aspx" />
</scheduledTasks>

 

More examples

Also, check out our Contrib-project on GitHub where you can find more examples.

 

 




More articles on "Develop with Newsletter Studio"

Control content with Render Tasks

Using a render task an developer can hook into the rendering process of the email and make changes to the message body/subject. This can be done both on an overall level and on each individual email (ie. personalization). This feature could for example be used to replace [sometext] inside the messag…

Read article

Subscription Providers

Can be used to hook into other systems or data sources. Newsletter Studio will use the providers to fetch the needed information about the receivers. All current subscriptions options (Newsletter Studios native mailing list and Umbraco members) are build with providers and it's easy to implement you…

Read article

Frontend API

To interact with Newsletter Studio from the front end we provide a very simple API in the static class NewsletterStudio.Api. This class has several methods for adding and removing subscribers both from the built in subscriptions and from Umbraco's member section (it won't delete anything but it chan…

Read article

Events

Since version 1.4.5 and 2.1 Newletter Studio contains some events that you as a developer can use when you develop your solutions.  Event name v1.4.5+ v2.1+ NewsletterStudio.Services.SendNewsletterService.SendningInitializing Yes Yes NewsletterStudio.Services.SendNewsletterService.Sent Yes Ye…

Read article

Custom data fields

The default property fields for receivers in Newsletter Studio are: name, e-mail, subscribed date and status. But some clients want to add custom data to their contacts.Since version 2.1 we've decided to prepare the underlying data layer for this by adding a new property called “Data” on the Receive…

Read article