mikejestes / ornamental
A library to send templated transactional emails, and easily test.
Installs: 1 382
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 4
Forks: 1
Open Issues: 1
Requires
- php: >=5.3.0
- mustache/mustache: 2.6.*
- phpmailer/phpmailer: @dev
- psr/log: ~1.0
- symfony/yaml: 2.4.*
Requires (Dev)
- phpunit/phpunit: 4.0.*
README
A library to send templated transactional emails. Plugins for your choice of template language and mail sending library. Configuration through YAML.
Installation
Add to your composer.json
file:
"require": { "mikejestes/ornamental": "*" }
Then download and run composer:
curl -s https://getcomposer.org/installer | php
php composer.phar install
Defining Messages
Yaml files configure each email message type. The filename minus .yaml
is the key when constructing a \Ornamental\Message
object.
from: service@example.com fromName: "{{ company }}" to: "{{user.email}}" subject: "Welcome to Ornamental, {{user.name}}" layout: welcome template: user_welcome required: - user
Layouts
Layout files can be used to separate themes. The body of each individual message is injected with the body
variable, which should be unescaped in mustache ({{{body}}}
).
Configuration
$setup = \Ornamental\Settings::getInstance(); $setup->templateDir = __DIR__ . '/templates/'; $setup->layoutDir = __DIR__ . '/layouts/'; $setup->messageDir = __DIR__ . '/messages/'; // optionally prefix emails with a string $setup->subjectPrefix = '[test] '; // set default variables across all messages $setup->defaults = array( 'website_url' => 'http://example.com/', ); // add phpmailer sender $phpmailer = new \Ornamental\Sender\Phpmailer(); $phpmailer->smtpHost = 'smtp.example.com'; $phpmailer->smtpUsername = 'root'; $phpmailer->smtpPassword = 'password'; $setup->addSender($phpmailer); // \Ornamental\Sender\Logger is a sender that logs to a PSR logger class $setup->addSender(new \Ornamental\Sender\Logger($logger));
Usage
Data as passed to each template by setting properties on the \Ornamental\Message
object:
$email = new \Ornamental\Message('user_welcome'); $email->user = array('name' => "Joe User"); $email->send();
Current Plugins
Templating:
- mustache (install
mustache/mustache
from composer) - php
Mail Transport:
- phpmailer (install
phpmailer/phpmailer
) - log file
Integration with unit tests
Unit tests can inspect the send log:
$deliveries = \Ornamental\Deliveries::getInstance(); $this->assertEquals(2, count($deliveries->log));