You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Organize emails with plain 'ol Ruby objects in a Rails application, like this:
3
+
Organize emails with plain 'ol Ruby objects in a Rails application:
4
4
5
5
```ruby
6
-
# ./app/email/user/welcome.rb
7
6
classUser::WelcomeEmail < ApplicationEmail
8
7
definitialize(user:)
9
8
@user= user
10
9
end
11
10
12
11
defto=@user.email
13
12
defsubject="Welcome to Beautiful Ruby"
14
-
defbody
15
-
superdo
16
-
<<~_
17
-
Hi #{@user.name},
18
-
19
-
You're going to learn a ton at https://beautifulruby.com.
20
-
_
21
-
end
13
+
14
+
defplain=<<~TEXT
15
+
Hi #{@user.name},
16
+
17
+
You're going to learn a ton at https://beautifulruby.com.
18
+
TEXT
19
+
20
+
defhtml= safe <<~HTML
21
+
<h1>Hi #{@user.name},</h1>
22
+
<p>You're going to learn a ton at <ahref="https://beautifulruby.com">Beautiful Ruby</a>.</p>
23
+
HTML
24
+
end
25
+
```
26
+
27
+
Or use ERB templates like Rails:
28
+
29
+
```ruby
30
+
# app/emails/welcome_email.rb
31
+
classWelcomeEmail < ApplicationEmail
32
+
includeSupermail::Rails::Rendering
33
+
34
+
definitialize(user:)
35
+
@user= user
22
36
end
37
+
38
+
defto=@user.email
39
+
defsubject="Welcome!"
40
+
# Templates automatically resolved from app/emails/welcome_email.html.erb and .text.erb
23
41
end
24
42
```
25
43
26
-
Contrast that with rails ActionMailer, where you will spend 20 minutes trying to figure out how to send an email. I created this gem because I got tired of digging through Rails docs to understand how to intialize an email and send it. PORO's FTW!
44
+
```erb
45
+
<!-- app/emails/welcome_email.html.erb -->
46
+
<h1>Hi <%= @user.name %>,</h1>
47
+
<p>Welcome to the app!</p>
48
+
```
49
+
50
+
Contrast that with Rails ActionMailer, where you will spend 20 minutes trying to figure out how to send an email. I created this gem because I got tired of digging through Rails docs to understand how to initialize an email and send it. PORO's FTW!
27
51
28
52
## Support this project
29
53
@@ -46,117 +70,112 @@ Then install it in Rails.
46
70
rails generate supermail:install
47
71
```
48
72
49
-
This creates the `ApplicationEmail` class at `app/emails/application_email.rb` where you can customize the base for all emails, including setting defaults like the `from` address.
73
+
This creates the `ApplicationEmail` class at `app/emails/application_email.rb`.
50
74
51
-
```ruby
52
-
classApplicationEmail < Supermail::Rails::Base
53
-
deffrom="website@example.com"
54
-
defto=nil
55
-
defsubject=nil
56
-
defbody
57
-
<<~_
58
-
#{yieldifblock_given?}
75
+
## Usage
76
+
77
+
### Basic emails
59
78
60
-
Best,
79
+
Define emails by overriding `to`, `from`, `subject`, `plain`, and `html` methods:
61
80
62
-
The Example.com Team
63
-
_
81
+
```ruby
82
+
classWelcomeEmail < ApplicationEmail
83
+
definitialize(user:)
84
+
@user= user
64
85
end
86
+
87
+
defto=@user.email
88
+
defsubject="Welcome!"
89
+
defplain="Welcome to the app, #{@user.name}!"
90
+
defhtml= safe "<h1>Welcome to the app, #{@user.name}!</h1>"
65
91
end
66
92
```
67
93
68
-
## Usage
94
+
- If both `plain` and `html` are defined, a multipart email is sent
95
+
- If only `plain` is defined, a text-only email is sent
96
+
- If only `html` is defined, an HTML-only email is sent
69
97
70
-
To generate a new email, run the following command:
98
+
### Wrapping content with hooks
71
99
72
-
```bash
73
-
rails generate supermail:email User::Welcome
100
+
Use `before_*` and `after_*` hooks in your base class to wrap all emails:
101
+
102
+
```ruby
103
+
classApplicationEmail < Supermail::Rails::Base
104
+
deffrom="website@example.com"
105
+
106
+
defbefore_html="<html><body>"
107
+
defafter_html=<<~HTML
108
+
<p>Best,<br>The Example.com Team</p>
109
+
</body></html>
110
+
HTML
111
+
112
+
defafter_plain=<<~TEXT
113
+
114
+
Best,
115
+
The Example.com Team
116
+
TEXT
117
+
end
74
118
```
75
119
76
-
This will create a new email class in `app/mailers/user/welcome_email.rb`.
120
+
Child emails just define their content - the hooks handle the wrapping:
77
121
78
122
```ruby
79
-
# ./app/email/user/welcome.rb
80
-
classUser::WelcomeEmail < ApplicationEmail
81
-
defbody=<<~PLAIN
82
-
Hello there!
83
-
PLAIN
123
+
classWelcomeEmail < ApplicationEmail
124
+
defsubject="Welcome!"
125
+
defplain="Welcome to the app!"
126
+
defhtml= safe "<h1>Welcome!</h1>"
84
127
end
85
128
```
86
-
You can customize the email by overriding the `to`, `from`, `subject`, and `body` methods.s
87
129
88
-
```ruby
89
-
# ./app/email/user/welcome.rb
90
-
classUser::WelcomeEmail < ApplicationEmail
91
-
definitialize(user:)
92
-
@user= user
93
-
end
130
+
### Using Phlex components
94
131
95
-
defto=@user.email
96
-
defsubject="Welcome to the website"
97
-
defbody
98
-
superdo
99
-
<<~_
100
-
Hi #{@user.name},
101
-
102
-
Welcome to the website We're excited to have you on board.
103
-
_
104
-
end
105
-
end
132
+
Return any object that responds to `#call` from `html`:
133
+
134
+
```ruby
135
+
classWelcomeEmail < ApplicationEmail
136
+
defhtml=WelcomeEmailView.new(user:@user)
106
137
end
107
138
```
108
139
109
-
### Send emails from the server
140
+
### CSS inlining
110
141
111
-
Then, to send the email.
142
+
Override `html_body` for post-processing like CSS inlining:
This opens your users email client with prefilled information. A support email about an order might look like this:
169
+
## Generators
137
170
138
-
```ruby
139
-
classSupport::OrderEmail < ApplicationEmail
140
-
definitialize(user:, order:)
141
-
@user= user
142
-
@order= order
143
-
end
171
+
Generate a new email:
144
172
145
-
defto="support@example.com"
146
-
deffrom=@user.email
147
-
defsubject="Question about order #{@order.id}"
148
-
defbody=<<~BODY
149
-
Hi Support,
150
-
151
-
I need help with my order #{@order.id}.
152
-
153
-
Thanks,
154
-
155
-
#{@user.name}
156
-
BODY
157
-
end
173
+
```bash
174
+
rails generate supermail:email User::Welcome
158
175
```
159
176
177
+
Creates `app/emails/user/welcome_email.rb`.
178
+
160
179
## Development
161
180
162
181
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
0 commit comments