Mailers

A mailer, on Newsletter terminology, is a class able to send emails and that extends a base class with some predefined methods.

A mailer implementation can be registered and then used by Newsletter both during newsletters delivery and while sending service messages (like the activation and welcome emails).

Newsletter can use only one mailer, so even if more than one is registered only the latest is used.

To add a mailer to Newsletter you have two choices:

  1. create just the mailer and listen for a specific event to register it
  2. create a full addon using some classes provided by Newsletter (the same classes used by our addons!) that help reducing the code needed

Using our full addon example

We start with the second option since the only thing you need to do is to jump to our GitHub repo where a full mailer is already implemented and you can simply add the delivery code. Some important steps:

  1. Choose a folder name (do not use “newsletter-dummymailer” it not professional at all) for your addon
  2. Please, do not start the folder name with “newsletter-” to avoid conflicts, select your own name (it’s a good practice to use a prefix in attempt to create a unique name)
  3. Rename the “dummymailer.php” file, which is the main plugin file to something else
  4. Rename the NewsletterDummyMailerAddon to something more convenient (it is localed in the file plugin.php) and realted to you
  5. in your main plugin file (see point 3) change “new NewsletterDummyMailerAddon(...)” with your class name
  6. on plugin.php, rename the class “NewsletterDummyMailer” to something else
  7. change the new NewsletterDummyMailer(...) on get_mailer(...) method to the name select on previous point

YES, WE KNOW, WE SHOULD HAVE USED NAMESPACES! NEXT TIME?

Creating only the mailer

From our GitHub repo you can extract the mailer implementation (the class named NewsletterDummyMailer) and (changing its name!) implement your own version.

Then you should add a piece of code to listen to the action newsletter_register_mailer

add_action('newsletter_register_mailer', 'my_newsletter_register_mailer');

and when the action is fired, register your mail implementation:

function my_newsletter_register_mailer() {
  $mailer = new MyMailer(...);
  Newsletter::instance()->register_mailer($mailer);
}

That is fully optimized by Newsletter which fires the action only when really needed. We aim to be as green as possible, even in the code.

Please share with us your mailers!