Jump to main content Jump to doc navigation

The last lesson is the shortest: the component is already complete — all that remains is a script to send letters on a schedule.

The component workflow, for reference:

  1. Create a newsletter with a template or snippet.
  2. Add users (or let them subscribe through the site).
  3. Queue letters on the queue page, one per newsletter. The letter body is generated by the chosen snippet or template.
  4. Send them: manually from the manager, or by a scheduled script.

Only step 4 is missing.

Scheduled emails

Everything needed is already in the sxQueue class — the script only loads the component model, selects the records and calls send():

<?php
// This is for calling from the development directory.
if (file_exists(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/config.core.php')) {
    require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/config.core.php';
}
else {
    require_once dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/config.core.php';
}

// Get the config and call MODX
require_once MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
require_once MODX_CONNECTORS_PATH . 'index.php';

// Add a model
$modx->addPackage('sendex', MODX_CORE_PATH . 'components/sendex/model/');

// Choose 100 letters
$q = $modx->newQuery('sxQueue');
$q->limit($modx->getOption('sendex_queue_limit', null, 100, true));

// We send
$queue = $modx->getCollection('sxQueue', $q);
/** @var sxQueue $email */
foreach ($queue as $email) {
    $email->send();
}

A letter is deleted from the queue after sending, so the script can run every 2 minutes without loading the server.

The script lives in /core/components/sendex/cron/send.php — under core, so it cannot be called from the browser. Otherwise anyone could trigger mailings by guessing the URL.

Add it to cron — or to the scheduler in your hosting panel:

*/2  *  * * *   php /var/www/site/www/core/components/sendex/cron/send.php

Log in over SSH and run crontab -e, then paste the line into the editor. Every 2 minutes the server runs the script and the queue drains instantly. On the MODXCloud lab tariff SSH was unavailable, so the schedule had to be set from the panel.

Commit with the send.php file.

Adding emails via API

The interface cannot cover every case: new support tickets, news with template 5 from parent 13 — conditions like these cannot all be drawn as UI screens.

So automatic queue filling is left to the site developer. The component provides everything for it:

  1. Create a snippet or template that builds the letter body.
  2. Create a newsletter and point it at that snippet or template.
  3. Get the sxNewsletter object and call addQueues().
$modx->addPackage('sendex', MODX_CORE_PATH . 'components/sendex/model/');

/** @var sxNewsletter $newsletter */
if ($newsletter = $modx->getObject('sxNewsletter', array('name' => 'Newsletter'))) {
    $newsletter->addQueues();
}

Call it on page publish, on a schedule, or anywhere else. The snippet or template can select any documents under any conditions — the newsletter can carry any content.

Conclusion

The course ends here.

Ask questions; the code will be reviewed and refined. All changes are on GitHub.

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • A. Moreno
  • Alex
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • JT Skaggs
  • Anton Tarasov
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

$194 per month—let's make that $500!

Learn more