Skip to content

Commit 75465bf

Browse files
committed
Add baes Supermail::Rails::Base
1 parent cc5fed7 commit 75465bf

File tree

5 files changed

+143
-141
lines changed

5 files changed

+143
-141
lines changed

Gemfile.lock

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,84 @@ PATH
22
remote: .
33
specs:
44
supermail (0.1.0)
5-
mail (~> 2.0)
5+
actionmailer (~> 7.0)
66

77
GEM
88
remote: https://rubygems.org/
99
specs:
10+
actionmailer (7.2.2.2)
11+
actionpack (= 7.2.2.2)
12+
actionview (= 7.2.2.2)
13+
activejob (= 7.2.2.2)
14+
activesupport (= 7.2.2.2)
15+
mail (>= 2.8.0)
16+
rails-dom-testing (~> 2.2)
17+
actionpack (7.2.2.2)
18+
actionview (= 7.2.2.2)
19+
activesupport (= 7.2.2.2)
20+
nokogiri (>= 1.8.5)
21+
racc
22+
rack (>= 2.2.4, < 3.2)
23+
rack-session (>= 1.0.1)
24+
rack-test (>= 0.6.3)
25+
rails-dom-testing (~> 2.2)
26+
rails-html-sanitizer (~> 1.6)
27+
useragent (~> 0.16)
28+
actionview (7.2.2.2)
29+
activesupport (= 7.2.2.2)
30+
builder (~> 3.1)
31+
erubi (~> 1.11)
32+
rails-dom-testing (~> 2.2)
33+
rails-html-sanitizer (~> 1.6)
34+
activejob (7.2.2.2)
35+
activesupport (= 7.2.2.2)
36+
globalid (>= 0.3.6)
37+
activesupport (7.2.2.2)
38+
base64
39+
benchmark (>= 0.3)
40+
bigdecimal
41+
concurrent-ruby (~> 1.0, >= 1.3.1)
42+
connection_pool (>= 2.2.5)
43+
drb
44+
i18n (>= 1.6, < 2)
45+
logger (>= 1.4.2)
46+
minitest (>= 5.1)
47+
securerandom (>= 0.3)
48+
tzinfo (~> 2.0, >= 2.0.5)
49+
base64 (0.3.0)
50+
benchmark (0.4.1)
51+
bigdecimal (3.2.3)
52+
builder (3.3.0)
53+
concurrent-ruby (1.3.5)
54+
connection_pool (2.5.4)
55+
crass (1.0.6)
1056
date (3.4.1)
11-
diff-lcs (1.6.1)
12-
io-console (0.8.0)
13-
irb (1.15.1)
57+
diff-lcs (1.6.2)
58+
drb (2.2.3)
59+
erb (5.0.2)
60+
erubi (1.13.1)
61+
globalid (1.3.0)
62+
activesupport (>= 6.1)
63+
i18n (1.14.7)
64+
concurrent-ruby (~> 1.0)
65+
io-console (0.8.1)
66+
irb (1.15.2)
1467
pp (>= 0.6.0)
1568
rdoc (>= 4.0.0)
1669
reline (>= 0.4.2)
70+
logger (1.7.0)
71+
loofah (2.24.1)
72+
crass (~> 1.0.2)
73+
nokogiri (>= 1.12.0)
1774
mail (2.8.1)
1875
mini_mime (>= 0.1.1)
1976
net-imap
2077
net-pop
2178
net-smtp
2279
mini_mime (1.1.5)
23-
net-imap (0.5.6)
80+
mini_portile2 (2.8.9)
81+
minitest (5.25.5)
82+
net-imap (0.5.10)
2483
date
2584
net-protocol
2685
net-pop (0.1.2)
@@ -29,32 +88,56 @@ GEM
2988
timeout
3089
net-smtp (0.5.1)
3190
net-protocol
91+
nokogiri (1.18.10)
92+
mini_portile2 (~> 2.8.2)
93+
racc (~> 1.4)
94+
nokogiri (1.18.10-arm64-darwin)
95+
racc (~> 1.4)
3296
pp (0.6.2)
3397
prettyprint
3498
prettyprint (0.2.0)
35-
psych (5.2.3)
99+
psych (5.2.6)
36100
date
37101
stringio
38-
rake (13.2.1)
39-
rdoc (6.13.0)
102+
racc (1.8.1)
103+
rack (3.1.16)
104+
rack-session (2.1.1)
105+
base64 (>= 0.1.0)
106+
rack (>= 3.0.0)
107+
rack-test (2.2.0)
108+
rack (>= 1.3)
109+
rails-dom-testing (2.3.0)
110+
activesupport (>= 5.0.0)
111+
minitest
112+
nokogiri (>= 1.6)
113+
rails-html-sanitizer (1.6.2)
114+
loofah (~> 2.21)
115+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
116+
rake (13.3.0)
117+
rdoc (6.14.2)
118+
erb
40119
psych (>= 4.0.0)
41-
reline (0.6.0)
120+
reline (0.6.2)
42121
io-console (~> 0.5)
43-
rspec (3.13.0)
122+
rspec (3.13.1)
44123
rspec-core (~> 3.13.0)
45124
rspec-expectations (~> 3.13.0)
46125
rspec-mocks (~> 3.13.0)
47-
rspec-core (3.13.3)
126+
rspec-core (3.13.5)
48127
rspec-support (~> 3.13.0)
49-
rspec-expectations (3.13.3)
128+
rspec-expectations (3.13.5)
50129
diff-lcs (>= 1.2.0, < 2.0)
51130
rspec-support (~> 3.13.0)
52-
rspec-mocks (3.13.2)
131+
rspec-mocks (3.13.5)
53132
diff-lcs (>= 1.2.0, < 2.0)
54133
rspec-support (~> 3.13.0)
55-
rspec-support (3.13.2)
56-
stringio (3.1.6)
134+
rspec-support (3.13.6)
135+
securerandom (0.4.1)
136+
stringio (3.1.7)
57137
timeout (0.4.3)
138+
tzinfo (2.0.6)
139+
concurrent-ruby (~> 1.0)
140+
useragent (0.16.11)
58141

59142
PLATFORMS
60143
arm64-darwin-24

README.md

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@ Then install it in Rails.
1616
rails generate supermail:install
1717
```
1818

19-
This creates the `app/emails/application_email.rb` file that you can customize as the base for all emails.
19+
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.
2020

2121
```ruby
22-
class ApplicationEmail < Supermail::Email
23-
def from = "Supermail <[email protected]>"
24-
25-
class HTML
26-
def after_template
27-
p { "Best, The Supermail Team" }
28-
end
29-
end
30-
31-
class Text
32-
def after_template = <<~_
33-
Best,
34-
The Supermail Team"
35-
_
22+
class ApplicationEmail < Supermail::Rails::Base
23+
def from = "[email protected]"
24+
def to = nil
25+
def subject = nil
26+
def body
27+
<<~_
28+
#{yield if block_given?}
29+
30+
Best,
31+
32+
The Example.com Team
33+
_
3634
end
3735
end
3836
```
@@ -45,30 +43,35 @@ To generate a new email, run the following command:
4543
rails generate supermail:email User::Welcome
4644
```
4745

48-
This will create a new email class in `app/mailers/user/welcome.rb`.
46+
This will create a new email class in `app/mailers/user/welcome_email.rb`.
4947

5048
```ruby
5149
# ./app/email/user/welcome.rb
52-
class User::Welcome < ApplicationEmail
53-
def initialize(user:)
54-
@user = user
55-
end
50+
class User::WelcomeEmail < ApplicationEmail
51+
def body = <<~PLAIN
52+
Hello there!
53+
PLAIN
54+
end
55+
```
5656

57-
def subject = "Welcome to Supermail!"
5857

59-
class HTML
60-
def view_template
61-
h1 { "Welcome, #{@user.name}!" }
62-
p { "We're excited to have you on board." }
63-
end
58+
```ruby
59+
# ./app/email/user/welcome.rb
60+
class User::WelcomeEmail < ApplicationEmail
61+
def initialize(person:)
62+
@person = person
6463
end
6564

66-
class Text
67-
def view_template = <<~_
68-
Welcome, #{@user.name}!
65+
def to = @person.email
66+
def subject = "Welcome to the website"
67+
def body
68+
super do
69+
<<~_
70+
Hi #{@person.name},
6971
70-
We're excited to have you on board.
71-
_
72+
Welcome to the website We're excited to have you on board.
73+
_
74+
end
7275
end
7376
end
7477
```

lib/supermail.rb

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,23 @@
11
# frozen_string_literal: true
22

33
require_relative "supermail/version"
4-
5-
require 'mail'
6-
require "supermail/version"
4+
require "action_mailer"
75

86
module Supermail
97
class Error < StandardError; end
108

11-
# A simple Email builder that wraps Mail::Message
12-
class Email
13-
# Hook to ensure default initialization runs before any subclass initialize
14-
module Defaults
15-
def initialize(*args, &block)
16-
# default fields
17-
@cc = []
18-
@bcc = []
19-
@headers = {}
20-
@attachments = []
21-
super
22-
end
23-
end
24-
25-
# Whenever subclassing, prepend Defaults to ensure it wraps initialize
26-
def self.inherited(subclass)
27-
subclass.prepend Defaults
28-
super
29-
end
30-
31-
attr_accessor :from, :to, :cc, :bcc, :subject,
32-
:reply_to, :return_path, :date, :message_id,
33-
:in_reply_to, :references, :headers,
34-
:text_body, :html_body, :attachments
35-
36-
# Builds a Mail::Message from this Email's attributes
37-
# @return [Mail::Message]
38-
def message
39-
mail = Mail.new
40-
mail.from = Array(from) if from
41-
mail.to = Array(to)
42-
mail.cc = cc if cc.any?
43-
mail.bcc = bcc if bcc.any?
44-
mail.reply_to = Array(reply_to) if reply_to
45-
mail.return_path = return_path if return_path
46-
mail.date = date if date
47-
mail.message_id = message_id if message_id
48-
mail.in_reply_to = in_reply_to if in_reply_to
49-
mail.references = references if references
50-
51-
# Custom headers
52-
headers.each { |key, value| mail.header[key] = value }
9+
module Rails
10+
class Base
11+
delegate :deliver, :deliver_now, :deliver_later, to: :message
5312

54-
mail.subject = subject if subject
13+
def to = nil
14+
def from = nil
15+
def subject = nil
16+
def body = ""
5517

56-
# Bodies
57-
if text_body
58-
mail.text_part = Mail::Part.new do
59-
body text_body
60-
end
18+
def message
19+
ActionMailer::Base.mail(to:, from:, subject:, body:)
6120
end
62-
63-
if html_body
64-
mail.html_part = Mail::Part.new do
65-
content_type 'text/html; charset=UTF-8'
66-
body html_body
67-
end
68-
end
69-
70-
# Attachments (each as a hash: { filename:, content: })
71-
attachments.each do |att|
72-
mail.add_file filename: att[:filename], content: att[:content]
73-
end
74-
75-
mail
76-
end
77-
78-
# Delivers the built Mail::Message via its configured delivery_method
79-
# @return [Mail::Message]
80-
def deliver
81-
raise Error, "`to` address is required" unless to
82-
message.deliver!
8321
end
8422
end
8523
end

spec/supermail_spec.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

supermail.gemspec

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,5 @@ Gem::Specification.new do |spec|
3333
spec.require_paths = ["lib"]
3434

3535
# Uncomment to register a new dependency of your gem
36-
spec.add_dependency "mail", "~> 2.0"
37-
38-
# For more information and examples about making a new gem, check out our
39-
# guide at: https://bundler.io/guides/creating_gem.html
36+
spec.add_dependency "actionmailer", ">= 7.0"
4037
end

0 commit comments

Comments
 (0)