hanami-mailer 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/LICENSE.md +22 -0
- data/README.md +390 -8
- data/hanami-mailer.gemspec +18 -12
- data/lib/hanami-mailer.rb +1 -0
- data/lib/hanami/mailer.rb +311 -2
- data/lib/hanami/mailer/configuration.rb +307 -0
- data/lib/hanami/mailer/dsl.rb +288 -0
- data/lib/hanami/mailer/rendering/template_name.rb +52 -0
- data/lib/hanami/mailer/rendering/templates_finder.rb +133 -0
- data/lib/hanami/mailer/template.rb +40 -0
- data/lib/hanami/mailer/version.rb +2 -1
- metadata +84 -13
- data/.gitignore +0 -9
- data/Gemfile +0 -4
- data/Rakefile +0 -2
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb739b22196267893812b43fcf2c2e7b37185e3f
|
4
|
+
data.tar.gz: d0686236004144e85a9a1d1bac67447b3f344acf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 418c209b8b97286b1076579537bccb7cec64bf51abfaeb881a10ff374601bb564630aea514b1eb17b176427c5a51d1723d9833edf8cba03f348a9f8427cb7803
|
7
|
+
data.tar.gz: d495d3926cb1afe2f1fbb5769e262db19daa7296647c00ebf15232d156f6f8fb3b39c81f8116cd7d34ec8d1bc055b8b76c15742f807e23e2e62d7749cee5c307
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Hanami::Router
|
2
|
+
Mail for Ruby applications
|
3
|
+
|
4
|
+
## v0.2.0 - 2016-01-22
|
5
|
+
### Changed
|
6
|
+
- [Luca Guidi] Renamed the project
|
7
|
+
|
8
|
+
## v0.1.0 - 2015-09-30
|
9
|
+
### Added
|
10
|
+
- [Ines Coelho & Rosa Faria & Luca Guidi] Email delivery
|
11
|
+
- [Ines Coelho & Rosa Faria & Luca Guidi] Attachments
|
12
|
+
- [Ines Coelho & Rosa Faria & Luca Guidi] Multipart rendering
|
13
|
+
- [Ines Coelho & Rosa Faria & Luca Guidi] Configuration
|
14
|
+
- [Ines Coelho & Rosa Faria & Luca Guidi] Official support for Ruby 2.0
|
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright © 2015-2016 Luca Guidi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,8 +1,28 @@
|
|
1
1
|
# Hanami::Mailer
|
2
2
|
|
3
|
-
|
3
|
+
Mail for Ruby applications.
|
4
4
|
|
5
|
-
|
5
|
+
## Status
|
6
|
+
|
7
|
+
[](https://badge.fury.io/rb/hanami-mailer)
|
8
|
+
[](https://travis-ci.org/hanami/mailer?branch=master)
|
9
|
+
[](https://coveralls.io/r/hanami/mailer)
|
10
|
+
[](https://codeclimate.com/github/hanami/mailer)
|
11
|
+
[](https://gemnasium.com/hanami/mailer)
|
12
|
+
[](http://inch-ci.org/github/hanami/mailer)
|
13
|
+
|
14
|
+
## Contact
|
15
|
+
|
16
|
+
* Home page: http://hanamirb.org
|
17
|
+
* Mailing List: http://hanamirb.org/mailing-list
|
18
|
+
* API Doc: http://rdoc.info/gems/hanami-mailer
|
19
|
+
* Bugs/Issues: https://github.com/hanami/mailer/issues
|
20
|
+
* Support: http://stackoverflow.com/questions/tagged/hanami
|
21
|
+
* Chat: http://chat.hanamirb.org
|
22
|
+
|
23
|
+
## Rubies
|
24
|
+
|
25
|
+
__Hanami::Mailer__ supports Ruby (MRI) 2.2+.
|
6
26
|
|
7
27
|
## Installation
|
8
28
|
|
@@ -22,15 +42,377 @@ Or install it yourself as:
|
|
22
42
|
|
23
43
|
## Usage
|
24
44
|
|
25
|
-
|
45
|
+
### Conventions
|
46
|
+
|
47
|
+
* Templates are searched under `Hanami::Mailer.configuration.root`, set this value according to your app structure (eg. `"app/templates"`).
|
48
|
+
* A mailer will look for a template with a file name that is composed by its full class name (eg. `"articles/index"`).
|
49
|
+
* A template must have two concatenated extensions: one for the format and one for the engine (eg. `".html.erb"`).
|
50
|
+
* The framework must be loaded before rendering the first time: `Hanami::Mailer.load!`.
|
51
|
+
|
52
|
+
### Mailers
|
53
|
+
|
54
|
+
A simple mailer looks like this:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
require 'hanami/mailer'
|
58
|
+
|
59
|
+
class InvoiceMailer
|
60
|
+
include Hanami::Mailer
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
A mailer with `.to` and `.from` addresses and mailer delivery:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
require 'hanami/mailer'
|
68
|
+
|
69
|
+
Hanami::Mailer.configuration do
|
70
|
+
delivery_method :smtp,
|
71
|
+
address: "smtp.gmail.com",
|
72
|
+
port: 587,
|
73
|
+
domain: "example.com",
|
74
|
+
user_name: ENV['SMTP_USERNAME'],
|
75
|
+
password: ENV['SMTP_PASSWORD'],
|
76
|
+
authentication: "plain",
|
77
|
+
enable_starttls_auto: true
|
78
|
+
end.load!
|
79
|
+
|
80
|
+
class WelcomeMailer
|
81
|
+
include Hanami::Mailer
|
82
|
+
|
83
|
+
from 'noreply@sender.com'
|
84
|
+
to 'noreply@recipient.com'
|
85
|
+
subject 'Welcome'
|
86
|
+
end
|
87
|
+
|
88
|
+
WelcomeMailer.deliver
|
89
|
+
```
|
90
|
+
|
91
|
+
### Locals
|
92
|
+
|
93
|
+
The set of objects passed in the `deliver` call are called `locals` and are avaliable inside the mailer and the template.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
require 'hanami/mailer'
|
97
|
+
|
98
|
+
User = Struct.new(:name, :username)
|
99
|
+
user = User.new('Luca', 'jodosha')
|
100
|
+
|
101
|
+
Hanami::Mailer.load!
|
102
|
+
|
103
|
+
class WelcomeMailer
|
104
|
+
include Hanami::Mailer
|
105
|
+
|
106
|
+
from 'noreply@sender.com'
|
107
|
+
subject 'Welcome'
|
108
|
+
to :recipient
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
def recipient
|
113
|
+
user.email
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
InvoiceMailer.deliver(user: luca)
|
118
|
+
```
|
119
|
+
|
120
|
+
The corresponding `erb` file:
|
121
|
+
|
122
|
+
```erb
|
123
|
+
Hello <%= user.name %>!
|
124
|
+
```
|
125
|
+
|
126
|
+
### Scope
|
127
|
+
|
128
|
+
All public methods defined in the mailer are accessible from the template:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
require 'hanami/mailer'
|
132
|
+
|
133
|
+
class WelcomeMailer
|
134
|
+
include Hanami::Mailer
|
135
|
+
|
136
|
+
from 'noreply@sender.com'
|
137
|
+
to 'noreply@recipient.com'
|
138
|
+
subject 'Welcome'
|
139
|
+
|
140
|
+
def greeting
|
141
|
+
'Ahoy'
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
```erb
|
147
|
+
<h2><%= greeting %></h2>
|
148
|
+
```
|
149
|
+
|
150
|
+
### Template
|
151
|
+
|
152
|
+
The template file must be located under the relevant `root` and must match the inflected snake case of the mailer class name.
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
# Given this root
|
156
|
+
Hanami::Mailer.configuration.root # => #<Pathname:app/templates>
|
157
|
+
|
158
|
+
# For InvoiceMailer, it looks for:
|
159
|
+
# * app/templates/invoice_mailer.html.erb
|
160
|
+
# * app/templates/invoice_mailer.txt.erb
|
161
|
+
```
|
162
|
+
|
163
|
+
If we want to specify a different template, we can do:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
class InvoiceMailer
|
167
|
+
include Hanami::Mailer
|
168
|
+
|
169
|
+
template 'invoice'
|
170
|
+
end
|
171
|
+
|
172
|
+
# It will look for:
|
173
|
+
# * app/templates/invoice.html.erb
|
174
|
+
# * app/templates/invoice.txt.erb
|
175
|
+
```
|
176
|
+
|
177
|
+
### Engines
|
178
|
+
|
179
|
+
The builtin rendering engine is [ERb](http://en.wikipedia.org/wiki/ERuby).
|
180
|
+
|
181
|
+
This is the list of the supported engines.
|
182
|
+
They are listed in order of **higher precedence**, for a given extension.
|
183
|
+
For instance, if [ERubis](http://www.kuwata-lab.com/erubis/) is loaded, it will be preferred over ERb to render `.erb` templates.
|
184
|
+
|
185
|
+
<table>
|
186
|
+
<tr>
|
187
|
+
<th>Engine</th>
|
188
|
+
<th>Extensions</th>
|
189
|
+
</tr>
|
190
|
+
<tr>
|
191
|
+
<td>Erubis</td>
|
192
|
+
<td>erb, rhtml, erubis</td>
|
193
|
+
</tr>
|
194
|
+
<tr>
|
195
|
+
<td>ERb</td>
|
196
|
+
<td>erb, rhtml</td>
|
197
|
+
</tr>
|
198
|
+
<tr>
|
199
|
+
<td>Redcarpet</td>
|
200
|
+
<td>markdown, mkd, md</td>
|
201
|
+
</tr>
|
202
|
+
<tr>
|
203
|
+
<td>RDiscount</td>
|
204
|
+
<td>markdown, mkd, md</td>
|
205
|
+
</tr>
|
206
|
+
<tr>
|
207
|
+
<td>Kramdown</td>
|
208
|
+
<td>markdown, mkd, md</td>
|
209
|
+
</tr>
|
210
|
+
<tr>
|
211
|
+
<td>Maruku</td>
|
212
|
+
<td>markdown, mkd, md</td>
|
213
|
+
</tr>
|
214
|
+
<tr>
|
215
|
+
<td>BlueCloth</td>
|
216
|
+
<td>markdown, mkd, md</td>
|
217
|
+
</tr>
|
218
|
+
<tr>
|
219
|
+
<td>Asciidoctor</td>
|
220
|
+
<td>ad, adoc, asciidoc</td>
|
221
|
+
</tr>
|
222
|
+
<tr>
|
223
|
+
<td>Builder</td>
|
224
|
+
<td>builder</td>
|
225
|
+
</tr>
|
226
|
+
<tr>
|
227
|
+
<td>CSV</td>
|
228
|
+
<td>rcsv</td>
|
229
|
+
</tr>
|
230
|
+
<tr>
|
231
|
+
<td>CoffeeScript</td>
|
232
|
+
<td>coffee</td>
|
233
|
+
</tr>
|
234
|
+
<tr>
|
235
|
+
<td>WikiCloth</td>
|
236
|
+
<td>wiki, mediawiki, mw</td>
|
237
|
+
</tr>
|
238
|
+
<tr>
|
239
|
+
<td>Creole</td>
|
240
|
+
<td>wiki, creole</td>
|
241
|
+
</tr>
|
242
|
+
<tr>
|
243
|
+
<td>Etanni</td>
|
244
|
+
<td>etn, etanni</td>
|
245
|
+
</tr>
|
246
|
+
<tr>
|
247
|
+
<td>Haml</td>
|
248
|
+
<td>haml</td>
|
249
|
+
</tr>
|
250
|
+
<tr>
|
251
|
+
<td>Less</td>
|
252
|
+
<td>less</td>
|
253
|
+
</tr>
|
254
|
+
<tr>
|
255
|
+
<td>Liquid</td>
|
256
|
+
<td>liquid</td>
|
257
|
+
</tr>
|
258
|
+
<tr>
|
259
|
+
<td>Markaby</td>
|
260
|
+
<td>mab</td>
|
261
|
+
</tr>
|
262
|
+
<tr>
|
263
|
+
<td>Nokogiri</td>
|
264
|
+
<td>nokogiri</td>
|
265
|
+
</tr>
|
266
|
+
<tr>
|
267
|
+
<td>Plain</td>
|
268
|
+
<td>html</td>
|
269
|
+
</tr>
|
270
|
+
<tr>
|
271
|
+
<td>RDoc</td>
|
272
|
+
<td>rdoc</td>
|
273
|
+
</tr>
|
274
|
+
<tr>
|
275
|
+
<td>Radius</td>
|
276
|
+
<td>radius</td>
|
277
|
+
</tr>
|
278
|
+
<tr>
|
279
|
+
<td>RedCloth</td>
|
280
|
+
<td>textile</td>
|
281
|
+
</tr>
|
282
|
+
<tr>
|
283
|
+
<td>Sass</td>
|
284
|
+
<td>sass</td>
|
285
|
+
</tr>
|
286
|
+
<tr>
|
287
|
+
<td>Scss</td>
|
288
|
+
<td>scss</td>
|
289
|
+
</tr>
|
290
|
+
<tr>
|
291
|
+
<td>Slim</td>
|
292
|
+
<td>slim</td>
|
293
|
+
</tr>
|
294
|
+
<tr>
|
295
|
+
<td>String</td>
|
296
|
+
<td>str</td>
|
297
|
+
</tr>
|
298
|
+
<tr>
|
299
|
+
<td>Yajl</td>
|
300
|
+
<td>yajl</td>
|
301
|
+
</tr>
|
302
|
+
</table>
|
303
|
+
|
304
|
+
|
305
|
+
### Configuration
|
306
|
+
|
307
|
+
__Hanami::Mailer__ can be configured with a DSL that determines its behavior.
|
308
|
+
It supports a few options:
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
require 'hanami/mailer'
|
312
|
+
|
313
|
+
Hanami::Maler.configure do
|
314
|
+
# Set the root path where to search for templates
|
315
|
+
# Argument: String, Pathname, #to_pathname, defaults to the current directory
|
316
|
+
#
|
317
|
+
root '/path/to/root'
|
318
|
+
|
319
|
+
# Set the default charset for emails
|
320
|
+
# Argument: String, defaults to "UTF-8"
|
321
|
+
#
|
322
|
+
default_charset 'iso-8859'
|
323
|
+
|
324
|
+
# Set the delivery method
|
325
|
+
# Argument: Symbol
|
326
|
+
# Argument: Hash, optional configurations
|
327
|
+
delivery_method :stmp
|
328
|
+
```
|
329
|
+
|
330
|
+
### Attachments
|
331
|
+
|
332
|
+
Attachments can be added with the following API:
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
class InvoiceMailer
|
336
|
+
include Hanami::Mailer
|
337
|
+
# ...
|
338
|
+
|
339
|
+
def prepare
|
340
|
+
mail.attachments['invoice.pdf'] = '/path/to/invoice.pdf'
|
341
|
+
# or
|
342
|
+
mail.attachments['invoice.pdf'] = File.read('/path/to/invoice.pdf')
|
343
|
+
end
|
344
|
+
end
|
345
|
+
```
|
346
|
+
|
347
|
+
### Delivery Method
|
348
|
+
|
349
|
+
The global delivery method is defined through the __Hanami::Mailer__ configuration, as:
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
Hanami::Mailer.configuration do
|
353
|
+
delivery_method :smtp
|
354
|
+
end
|
355
|
+
```
|
356
|
+
|
357
|
+
```ruby
|
358
|
+
Hanami::Mailer.configuration do
|
359
|
+
delivery_method :smtp, address: "localhost", port: 1025
|
360
|
+
end
|
361
|
+
```
|
362
|
+
|
363
|
+
Builtin options are:
|
364
|
+
|
365
|
+
* Exim (`:exim`)
|
366
|
+
* Sendmail (`:sendmail`)
|
367
|
+
* SMTP (`:smtp`, for local installations)
|
368
|
+
* SMTP Connection (`:smtp_connection`, via `Net::SMTP` - for remote installations)
|
369
|
+
* Test (`:test`, for testing purposes)
|
370
|
+
|
371
|
+
### Custom Delivery Method
|
372
|
+
|
373
|
+
Developers can specify their own custom delivery policy:
|
374
|
+
|
375
|
+
```ruby
|
376
|
+
require 'hanami/mailer'
|
377
|
+
|
378
|
+
class MandrillDeliveryMethod
|
379
|
+
def initialize(options)
|
380
|
+
@options = options
|
381
|
+
end
|
382
|
+
|
383
|
+
def deliver!(mail)
|
384
|
+
# ...
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
Hanami::Mailer.configure do
|
389
|
+
delivery_method MandrillDeliveryMethod,
|
390
|
+
username: ENV['MANDRILL_USERNAME'],
|
391
|
+
password: ENV['MANDRILL_API_KEY']
|
392
|
+
end.load!
|
393
|
+
```
|
394
|
+
|
395
|
+
The class passed to `.delivery_method` must accept an optional set of options
|
396
|
+
with the constructor (`#initialize`) and respond to `#deliver!`.
|
397
|
+
|
398
|
+
### Multipart Delivery
|
399
|
+
|
400
|
+
All the email are sent as multipart messages by default.
|
401
|
+
For a given mailer, the framework looks up for associated text (`.txt`) and `HTML` (`.html`) templates and render them.
|
402
|
+
|
403
|
+
```ruby
|
404
|
+
InvoiceMailer.deliver # delivers both text and html templates
|
405
|
+
InvoiceMailer.deliver(format: :txt) # delivers only text template
|
406
|
+
```
|
26
407
|
|
27
|
-
|
408
|
+
Please note that **they aren't both mandatory, but at least one of them MUST** be present.
|
28
409
|
|
29
|
-
|
410
|
+
## Versioning
|
30
411
|
|
31
|
-
|
412
|
+
__Hanami::Mailer__ uses [Semantic Versioning 2.0.0](http://semver.org)
|
32
413
|
|
33
|
-
##
|
414
|
+
## Copyright
|
34
415
|
|
35
|
-
|
416
|
+
Copyright © 2015-2016 Luca Guidi – Released under MIT License
|
36
417
|
|
418
|
+
This project was formerly known as Lotus (`lotus-mailer`).
|