@@ -7,7 +7,18 @@ Email toolkit for CodeIgniter 4
77## Quick Start
88
991 . Install with Composer: ` > composer require tatter/outbox `
10- 2 . Migrate the database: ` > php spark migrate -all `
10+ 2 . Prepare the database: ` > php spark migrate -all && php spark db:seed "Tatter\Outbox\Database\Seeds\TemplateSeeder" `
11+ 3 . Send beautiful, dynamic email:
12+ ```
13+ model(TemplateModel::class)->findByName('Default')
14+ ->email([
15+ 'item' => 'Fancy Purse',
16+ 'cost' => '10 dollars',
17+ 'url' => site_url('items/show/' . $itemId),
18+ ])
19+ ->setTo($user->email)
20+ ->send();
21+ ```
1122
1223## Description
1324
@@ -35,48 +46,23 @@ Sending HTML email can be tricky, as support for HTML and CSS vary across displa
3546** Outbox** includes ` CssToInlineStyles ` , a module by * tijsverkoyen* to take any CSS and
3647inject it inline into an email template for maximum compatibility. This allows you to reuse
3748site stylesheets or write your own from scratch and use them across any of your templates.
38- Simply supply your display data to ` inline() ` , along with the optional template and CSS view
39- paths:
40- ```
41- use Tatter\Outbox\Outbox;
42-
43- ...
44-
45- $data = [
46- 'title' => 'First Email Ever',
47- 'preview' => 'Please do not think this is spam!',
48- 'main' => 'Hey there! Good of you to sign up.<br /> We would like to offer you...',
49- 50- 'unsubscribe' => '<a href="https://example.com/unsubscribe">Unsubscribe</a>',
51- ];
52- $message = Outbox::inline($data, 'EmailTemplates/MarketCampaign');
53- $this->email
54- ->setMessage($message)
55- ->setMailType('html')
56- ->send();
57- ```
58-
59- ### Tokenizing
60-
61- Sometimes instead of sending mail internally your application will work with an external
62- email service API. ** Outbox** provides a way of tokenizing your templates so they will work
63- with popular merge-style services. Simply set your configuration details and then supply the
64- list of variables to tokenize:
65-
66- $template = Outbox::tokenize(['title', 'main', 'unsubscribe']);
67-
68- ** Outbox** will return your template with the tokenize values ready for submission.
49+ Use the default styles from
50+ [ Responsive HTML Email Template] ( https://github.com/leemunroe/responsive-html-email-template ) ,
51+ supply your own as string parameters, or create a View file and add it to the configuration.
6952
7053## Templating
7154
72- ** Outbox** comes with a CodeIgniter-ready version of the
55+ ** Outbox** comes with a default template, a modified-for-CodeIgniter version of the
7356[ Responsive HTML Email Template] ( https://github.com/leemunroe/responsive-html-email-template ) .
7457This provides a solid basis for your emails so you can be sure they will display nicely on
75- any device.
58+ any device. Run the Template Seeder to begin using this as the default:
59+
60+ php spark db:seed "Tatter\Outbox\Database\Seeds\TemplateSeeder"
61+
62+ You may also write your own templates and seed them or use the provided MVC bundle for
63+ managing email templates in your database. To enable the Controller you will need to
64+ toggle ` $routeTemplates ` in the configuration, or add the following routes to ** app/Config/Routes.php** :
7665
77- You may also write your own templates to use with the rest of the features. ** Outbox** provides
78- migrations and an Entity, a Model, Views, and a Controller for managing email templates in your
79- database. To enable the Controller you will need to add the following routes to ** app/Config/Routes.php** :
8066```
8167// Routes to Email Templates
8268$routes->group('emails', ['namespace' => '\Tatter\Outbox\Controllers'], function ($routes)
@@ -88,34 +74,37 @@ $routes->group('emails', ['namespace' => '\Tatter\Outbox\Controllers'], function
8874});
8975```
9076
91- Of course you may provide your own interfaces as well, and should probably secure access to
92- these routes with a Filter either way.
77+ Be sure to secure appropriate access to these routes (e.g. with a Filter).
78+
79+ ### Tokens
80+
81+ Templates use View Parser "tokens" that will be passed through to add your data.
82+ The ` Template ` Entity can do this for you by passing in your data parameters:
9383
94- Templates use predefined "tokens" that will be passed through COdeIgniter's Parser to add
95- your data. The ` Template ` Entity can do this for you with the ` render($data = []) ` method
96- to get back a ready-to-go HTML email string:
9784```
98- $template = model(TemplateModel::class)->where('name', 'Newsletter')->first();
99- $email = service('Email');
85+ $template = model(TemplateModel::class)->findByName('Item Purchase');
10086
101- $email->setBody( $template->render (['title ' => 'Pumpkins are here!']) );
102- $email->send( );
87+ $subject = $template->renderSubject (['item ' => 'Fancy Purse'] );
88+ $body = $template->renderBody(['cost' => '10 dollars'] );
10389```
10490
105- If you want to take advantage of ` Outbox ` 's style inlining you can get a fully prepared
91+ ` renderBody() ` will take care of inlining any CSS you have provided and including your
92+ template in its parent (if defined).
93+
94+ If you do not need any other configuration you can get a fully prepared
10695version of the ` Email ` class with rendered and inlined content from the library:
10796```
108- $email = Outbox::fromTemplate( $template);
97+ $email = $template->email($data );
10998$email->setTo('[email protected] ')->send(); 11099```
111100
112101### Cascading Templates
113102
114- Each ` Template ` may also be entered with a "Parent Template". Parent templates need to have
103+ Each ` Template ` may also be created with a "Parent Template". Parent templates need to have
115104a ` {body} ` token which will receive the parsed content from its child. Additional tokens
116105in the parent template can be entered by defining them in the child.
117106
118107Cascading templates makes it easy to have a few "layouts" with many different variable
119108messages for each layout. For example, your app may send both newsletters and receipts
120- with their own layout (Parent Template) and then a myriad of different custom content
121- for different types of users .
109+ with their own layout (Parent Template) and then a myriad of different customizable
110+ messages for different occasions .
0 commit comments