help.axcms.netAxinom Logo
Save Save Chapter Send Feedback

Newsletters and MailTemplates

 

Newsletter is a special kind of Page (derived from AxPageBase) which is, when published to LS, sent out as personalized mail message to the LS users categorized in the same categories as newsletter.

MailTemplate is a special kind of Page which can be used for sending out individual personalized mail message from custom code.

Sending out a mail template

To send out a mail based on mail template, you need:

  1. Load your mailtemplate, e.g.:
    AxPageBaseAdapter pageAdapter = new AxPageBaseAdapter(PageType.MailTemplate);
    AxMailTemplate mailTemplate = pageAdapter.Load(mailTemplateId);
  2. Load personalization data, which can be any instance of INewsletterSubscriber, e.g. AxUser:
    AxUserAdapter userAdapter = new AxUserAdapter();
    AxUser user = adapter.LoadByName("member");
  3. Create IMailSender instance (if you do not have access to MailCenter):
    IMailSender mailSender = new SmtpMailer(CMSConfigurationSettings.SmtpServer, 25);
    Or, if you have access to MailCenter, you can benefit from asynchronous send out. Then get IMailService, e.g:
    IMailService mailService = AxCmsWebApi.MailService;
  4. Send out the mail:
    mailTemplate.Send(mailService, user)
  5. If you need to use translation during personalization of your message, supply your IResourceManager instance to Send method.
  6. If you want to override CC and BCC values of the mail message, use IMailTemplateSubscriber instead of INewsletterSubscriber.

 You can also use AxMailTemplateSendingTask for asynchronous send out;

  1. Load your mailtemplate, e.g.:
    AxPageBaseAdapter pageAdapter = new AxPageBaseAdapter(PageType.MailTemplate);
    AxMailTemplate mailTemplate = pageAdapter.Load(mailTemplateId);
  2. Load personalization data, which can be any instance of INewsletterSubscriber, e.g. AxUser:
    AxUserAdapter userAdapter = new AxUserAdapter();
    AxUser user = adapter.LoadByName("member");
  3. Schedule the send out:
    ServiceTask.Schedule(new AxMailTemplateSendingTask(user, "LIVE"), mailTemplate);

Overriding newsletter subscribers

Since AxCMS.net 8.3, it is possible to override the logic for getting newsletter subscribers. To do it:

  1. Override AxNewsletter class with [OverridesAxNewsletter] attribute, same way as for AxPage (see Extending AxPage).
  2. If you need to change the logic completely, override GetSubscribers and GetSubscriberCount methods.
  3. If your change is only affecting the query used to load users, override GetSubscribersQuery method. Make sure that query works in both MS (for test send out) and LS (for real send out),
  4. If you use non-standard logic for subscribers list, you may also need to override Archive Overview page to support black listing and unsubscribing. To do that, override AxViewArchiveOverviewHelper as described here, and override UnsubscribeUser, CanBlackListUser, BlacklistUser, BlockUser methods, depending on your needs.

Modifying newsletter or mailtemplate mail message

If standard macro approach for newsletter / mailtemplate personalization does not fit you, it is possible to override the logic for creating the final mail message. To do it:

  1. Override AxNewsletter or AxMailTemplate class with [OverridesAxNewsletter] or [OverridesAxMailTemplate] attribute, same way as for AxPage (see Extending AxPage).
  2. Override method CreateTemplateMailMessage to change basic common properties (From, CC, etc) of the mail message being send out. Be sure to call base method before doing your changes.
  3. Override method PersonalizeMailMessage to change personalized properties (Body, Subject, etc) of the mail message being send out. Be sure to call base method before doing your changes.