Widgets in Mails

3 downloads 1600 Views 289KB Size Report
Different ways of using Mail Widgets: - As layouter inside a cms ... As a layouter inside an old mail layouter ... Reference to it in cms text and html version like.
Widgets in Mails

Darijusch Behbahani

Key benefits of Widgets in Mails: - More Modularity - Better testing

- Easier implementation of A/B Testing

Different ways of using Mail Widgets: - As layouter inside a cms content mail - As a layouter inside an old mail layouter

- As a complete new widget mail

Creating a MailModule: - Extend AbstractMailModule - Save your mailmodule under modules\modulename\mails\classname.class.php - Templates are under modules\modulename\mails\templates - classname.html - classname.txt

Creating a MailModule: class TestMail extends AbstractMailModule { public static $CLASS = __CLASS__; public $account; public function collect() { return new \rg\core\pow\requirements\RequirementCollection(array( new EntityRequirement( 'account', AccountImpl::$CLASS, array('accountId' => 16214) ) )); } public function getData() { return array( 'firstname' => $this->account->getFirstname(), 'lastname' => $this->account->getLastname() ); } }

Using a MailModule in a cms content mail: Extend your MailModule from AbstractMailModule Implement the collect method and retrieve and handle all your payload data

Create both templates, the html and txt version Reference to it in cms text and html version like {#\rg\modules\invitations\actions\MailInstitutionInvitation}

In the queueworker/cli script, create your mail and pass over your payload that gets then prefilled in your MailModule

Concept of a Widget Mail:

BaseMail

HeaderWidget

AdditionalWidgets

ContentWidget

AdditionalWidgets

FooterWidget

AdditionalWidgets

Creating a WidgetMail: class WidgetMail extends AbstractMail { public static $CLASS = __CLASS__; public $accountId; public function getContentWidget() { return WidgetMailContent::$CLASS; } public function getDisableAction() { return 'go.Out.newsletter.html'; } public function getChannel() { return 'reg'; } public function getEvent() { return 're666_x_'; } public function getSubject() { return $this->mailContent->getSubject(); } public function getPayload() { return array('accountId'); } }

Creating the Content: class WidgetMailContent extends AbstractMailModule { public static $CLASS = __CLASS__; public $accountId; public $account; public function collect() { $that = $this; return new RequirementCollection(array( new RequestDataRequirement('accountId ', 'accountId ') ), function() use ($that) { return new RequirementCollection(array( new EntityRequirement('acount', AccountImpl::$CLASS, array('accountId ' => $that->accountId) )); }); } public function getSubject() { return 'Welcome to ResearchGate ' . $this->account->getFullname(); } public function getData() { return array('firstname' => $this->account->getFirstname, 'lastname' => $this->account->getLastname()); } }

Conditional Mail: class WidgetMailContent extends AbstractMailModule implements IHandlesBeforeRendered { public $profileItems = array(); public function collect() { // collect multiple Profile Widgets into $profileItems } public function getData() { return array('profileItems' => $this->profileItems); }

public function beforeRendered() { foreach($this->profileItems as $profileItem) { if ($profileItem->isRenderable()) { return; } } return $this->deactivateWidget(new CustomException()); } }

Some things you should keep in mind: - The send function throws a MailException or an other exception (defined in your widget on disable widget) - Try to make MailModules more abstract don‘t hardcode event codes or other stuff so you can use them in other mails as well - If you have a cli script which send out a lot of mails, try to retrieve the data outside of the widget and pass it over as payload, and after each mail or mail batch call „clear“ on the PhpCache - Avoid usage of html in your getData return values, the same data will be used in the Text Renderer. Use javascript helper functions instead or two different fields like htmlAccounts and textAccounts.

Questions?