Skip to content

Commit 3457cb5

Browse files
authored
Merge pull request #7 from tattersoftware/refactor
Refactor
2 parents a5333e7 + 8968145 commit 3457cb5

File tree

18 files changed

+280
-378
lines changed

18 files changed

+280
-378
lines changed

README.md

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ Email toolkit for CodeIgniter 4
77
## Quick Start
88

99
1. 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
3647
inject it inline into an email template for maximum compatibility. This allows you to reuse
3748
site 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-
'contact' => '[email protected]',
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).
7457
This 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
10695
version 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
115104
a `{body}` token which will receive the parsed content from its child. Additional tokens
116105
in the parent template can be entered by defining them in the child.
117106

118107
Cascading templates makes it easy to have a few "layouts" with many different variable
119108
messages 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.

examples/Outbox.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,16 @@ class Outbox extends \Tatter\Outbox\Config\Outbox
2727
public $routeTemplates = false;
2828

2929
/**
30-
* Layout to use for template management.
30+
* Layout to use for Template management.
3131
*
3232
* @var string
3333
*/
3434
public $layout = 'Tatter\Outbox\Views\layout';
3535

3636
/**
37-
* Default view for email templating.
37+
* View path for the default CSS styles to inline.
3838
*
3939
* @var string
4040
*/
41-
public $template = 'Tatter\Outbox\Views\template';
42-
43-
/**
44-
* Default CSS style view to apply to the template.
45-
*
46-
* @var string
47-
*/
48-
public $styles = 'Tatter\Outbox\Views\styles';
41+
public $styles = 'Tatter\Outbox\Views\Defaults\styles';
4942
}

src/Config/Outbox.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,16 @@ class Outbox extends BaseConfig
1919
public $routeTemplates = false;
2020

2121
/**
22-
* Layout to use for template management.
22+
* Layout to use for Template management.
2323
*
2424
* @var string
2525
*/
2626
public $layout = 'Tatter\Outbox\Views\layout';
2727

2828
/**
29-
* Default view for email templating.
29+
* View path for the default CSS styles to inline.
3030
*
3131
* @var string
3232
*/
33-
public $template = 'Tatter\Outbox\Views\template';
34-
35-
/**
36-
* Default CSS style view to apply to the template.
37-
*
38-
* @var string
39-
*/
40-
public $styles = 'Tatter\Outbox\Views\styles';
33+
public $styles = 'Tatter\Outbox\Views\Defaults\styles';
4134
}

src/Controllers/Templates.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function index(): string
9797
*/
9898
public function show($templateId = null): string
9999
{
100-
return $this->getTemplate($templateId)->render();
100+
return $this->getTemplate($templateId)->renderBody();
101101
}
102102

103103
/**
@@ -114,7 +114,7 @@ public function edit($templateId = null): string
114114
return view('Tatter\Outbox\Views\Templates\form', [
115115
'method' => 'Edit',
116116
'template' => $this->getTemplate($templateId),
117-
'templates' => $this->model->orderBy('name')->findAll(),
117+
'templates' => $this->model->where('id !=', $templateId)->orderBy('name')->findAll(),
118118
]);
119119
}
120120

@@ -195,7 +195,7 @@ public function send_commit($templateId = null): RedirectResponse
195195
return redirect()->back()->withInput()->with('error', implode('. ', $this->validator->getErrors()));
196196
}
197197

198-
$email = Outbox::fromTemplate($this->getTemplate($templateId), $this->request->getPost());
198+
$email = $this->getTemplate($templateId)->email($this->request->getPost());
199199

200200
$email->setFrom($this->request->getPost('fromEmail'), $this->request->getPost('fromName'));
201201
$email->setTo($this->request->getPost('recipients'));

src/Database/Migrations/2020-10-17-195118_create_outbox_templates.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public function up()
1010
'name' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
1111
'subject' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
1212
'body' => ['type' => 'text', 'null' => true],
13-
'tokens' => ['type' => 'text', 'null' => true],
1413
'created_at' => ['type' => 'datetime', 'null' => true],
1514
'updated_at' => ['type' => 'datetime', 'null' => true],
1615
'deleted_at' => ['type' => 'datetime', 'null' => true],

src/Database/Seeds/TemplateSeeder.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,11 @@ public function run()
1212
return;
1313
}
1414

15-
// Prep the parser tokens
16-
$tokens = ['subject', 'title', 'preview', 'body', 'contact', 'unsubscribe'];
17-
$data = [];
18-
foreach ($tokens as $token)
19-
{
20-
$data[$token] = '{' . $token . '}';
21-
}
22-
23-
// Render the view into a parsable version and add it to the database
15+
// Add the Default Template to the database
2416
model(TemplateModel::class)->insert([
2517
'name' => 'Default',
2618
'subject' => '{subject}',
27-
'body' => view(config('Outbox')->template, $data, ['debug' => false]),
28-
'tokens' => implode(',', $tokens),
19+
'body' => view('Tatter\Outbox\Views\Defaults\template'),
2920
]);
3021
}
3122
}

0 commit comments

Comments
 (0)