The HST allows you to easily subscribe users to so called mail groups, which can be seen as areas of interest. For example, one user might subscribe to the "JSP" mail group, while another subscribes to the "Web Development" group. It's then possible to configure which user receives which newsletters in the CMS.
To subscribe users to a mail group, you can make use of the NewsletterSubscriber class in the HST. This class takes care of handling the configuration specific to your CMS and has methods for adding and removing subscriptions.
You will most likely use the NewsletterSubscriber in something like a serlvet or a Struts action class. It's constructor requires a ServletContext object, in order for it to read the configuration specific to your web application.
The NewsletterSubscriber can be used to subscribe and unsubscribe users to mail groups. It can also be used to get information about existing mailgroups.
Warning: the NewsletterSubsciber class is only usable when using the Hippo Community Apps (HCA). It depends on classes written in the HCA to function correctly.
final int OUR_DOMAIN = 1;
NewsletterSubscriber newsletterSubscriber =
new NewsletterSubscriber(OUR_DOMAIN, getServlet().getServletContext());
Subscriber subscriber = newsletterSubscriber.getSubscriber("emailAddress");
if (subscriber == null) { // Create a new subscriber
/**
* First get the mail groups to subscribe to.
* In this example, we"ll just take all mail groups
* of the current domain.
*/
List mailGroups = newsletterSubscriber.getMailGroupsByDomain(OUR_DOMAIN);
/**
* Now we"ll say that the user is already known by the
* application. Just not as a subscriber. So, we will first instantiate
* a new User object, and use that to create a new subscriber.
*
* After that, we use the NewsletterSubscriber to register this
* subscriber to the list of mail groups. The type of mail this user
* will receive is HTML.
*/
User user = newsLetterSubscriber.getUser(email);
subscriber = newsLetterSubscriber.makeSubscriber(user, "html");
newsletterSubscriber.doSubscribe(subscriber, mailGroups);
}