supermail 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5007ee69916bd3c91239105a78c65ac3fc6729adb206e914881166480cca8d63
4
- data.tar.gz: 903ba6a76b34ec3f64d8dc0f4bab2d8782a3ea90c727dbec5647d8e8b99a3376
3
+ metadata.gz: 4efb804624582618a2d1330b68b48030e27a0764f82bd8e5a99d01ba955d2174
4
+ data.tar.gz: 362d37bf770583b50c86e26cd280158cb8885e8725589ec68a4093ed5fabe79a
5
5
  SHA512:
6
- metadata.gz: d696046239e5c7bbb875b291ca99fc2533127f72babac2991ace61520522dc095788506857c1b8cc56c91f74e5c5f24de9db88a3046421ea10e9a677ac6fb4c1
7
- data.tar.gz: c2e1303de634498310018e9edbecbb1f0718b545698f56b378d172b4a1fc11389d572ab35019a6bf419faf345b8554b27f95181f4c2ec4e4b0ddca4c81da6c78
6
+ metadata.gz: d3d68858b41c793e45eda60b43f557bd0fc3e049f248d22352f8bd2e52544b10d2b374befca84fac46de283807352e59e2dbd8bb1e8c605fa7826b2b70789935
7
+ data.tar.gz: 799eca08e4b0997fda627688b0707442cf5a2d66dc8c42fdf10bfcef6d7a1a2d6e2b4641c00de898ffc1e4dbd78b2af50bd58de8926b6ffbe520c76058f2dc8d
data/README.md CHANGED
@@ -25,6 +25,13 @@ end
25
25
 
26
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!
27
27
 
28
+ ## Support this project
29
+
30
+ Learn how to build UI's out of Ruby classes and support this project by ordering the [Phlex on Rails video course](https://beautifulruby.com/phlex).
31
+
32
+ [![](https://immutable.terminalwire.com/hmM9jvv7yF89frBUfjikUfRmdUsTVZ8YvXc7OnnYoERXfLJLzDcj5dFM7qdfMG2bqQLuw633Zt1gl3O7z0zKmH6k8QmifN7z0kJo.png)](https://beautifulruby.com/phlex/forms/introduction)
33
+
34
+
28
35
  ## Installation
29
36
 
30
37
  Install the gem and add to the application's Gemfile by executing:
@@ -76,7 +83,7 @@ class User::WelcomeEmail < ApplicationEmail
76
83
  PLAIN
77
84
  end
78
85
  ```
79
-
86
+ You can customize the email by overriding the `to`, `from`, `subject`, and `body` methods.s
80
87
 
81
88
  ```ruby
82
89
  # ./app/email/user/welcome.rb
@@ -99,6 +106,8 @@ class User::WelcomeEmail < ApplicationEmail
99
106
  end
100
107
  ```
101
108
 
109
+ ### Send emails from the server
110
+
102
111
  Then, to send the email.
103
112
 
104
113
  ```ruby
@@ -113,6 +122,41 @@ User::Welcome.new(user: User.first).message.tap do
113
122
  end.deliver_now
114
123
  ```
115
124
 
125
+ ### Launch the user's email client
126
+
127
+ Supermail clases can be used to generate `mailto:` links with Rails helpers.
128
+
129
+ ```erb
130
+ <%= link_to Support::OrderEmail.new(
131
+ user: current_user,
132
+ order: @order
133
+ ).mail_to s%>
134
+ ```
135
+
136
+ This opens your users email client with prefilled information. A support email about an order might look like this:
137
+
138
+ ```ruby
139
+ class Support::OrderEmail < ApplicationEmail
140
+ def initialize(user:, order:)
141
+ @user = user
142
+ @order = order
143
+ end
144
+
145
+ def to = "support@example.com"
146
+ def from = @user.email
147
+ def subject = "Question about order #{@order.id}"
148
+ def body = <<~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
158
+ ```
159
+
116
160
  ## Development
117
161
 
118
162
  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.
@@ -3,7 +3,7 @@
3
3
  require 'rails/generators/named_base'
4
4
 
5
5
  module Supermail
6
- class EmailGenerator < Rails::Generators::NamedBase
6
+ class EmailGenerator < ::Rails::Generators::NamedBase
7
7
  source_root File.expand_path('templates', __dir__)
8
8
 
9
9
  desc "Generate a new email class"
@@ -22,4 +22,4 @@ module Supermail
22
22
  name.camelize
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -3,7 +3,7 @@
3
3
  require 'rails/generators/base'
4
4
 
5
5
  module Supermail
6
- class InstallGenerator < Rails::Generators::Base
6
+ class InstallGenerator < ::Rails::Generators::Base
7
7
  source_root File.expand_path('templates', __dir__)
8
8
 
9
9
  desc "Install Supermail in a Rails application"
@@ -16,4 +16,4 @@ module Supermail
16
16
  empty_directory 'app/emails'
17
17
  end
18
18
  end
19
- end
19
+ end
@@ -1,4 +1,4 @@
1
- class ApplicationEmail < Supermail::Base
1
+ class ApplicationEmail < Supermail::Rails::Base
2
2
  def from = "website@example.com"
3
3
 
4
4
  def body
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Supermail
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/supermail.rb CHANGED
@@ -1,30 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "supermail/version"
4
- require 'action_mailer'
4
+ require "action_mailer"
5
+ require "erb"
5
6
 
6
7
  module Supermail
7
8
  class Error < StandardError; end
8
9
 
9
- class Base
10
- delegate :deliver, :deliver_now, :deliver_later, to: :message_delivery
10
+ module Rails
11
+ class Base
12
+ delegate \
13
+ :deliver,
14
+ :deliver_now,
15
+ :deliver_later,
16
+ :message,
17
+ to: :action_mailer_base_mail
11
18
 
12
- def to = nil
13
- def from = nil
14
- def subject = nil
15
- def body = ""
19
+ def to = nil
20
+ def from = nil
21
+ def subject = nil
22
+ def cc = []
23
+ def bcc = []
24
+ def body = ""
16
25
 
17
- def message
18
- message_delivery.message
19
- end
26
+ # Generate a mailto: URL with appropriate escaping.
27
+ def mailto = MailTo.href(to:, from:, cc:, bcc:, subject:, body:)
28
+ alias :mail_to :mailto
20
29
 
21
- def message_delivery
22
- ActionMailer::Base.mail(to:, from:, subject:, body:)
30
+ private def action_mailer_base_mail
31
+ ActionMailer::Base.mail(to:, from:, cc:, bcc:, subject:, body:)
32
+ end
23
33
  end
24
34
  end
25
- end
26
35
 
27
- # Load generators only when Rails is available
28
- if defined?(::Rails)
29
- require 'rails'
36
+ module MailTo
37
+ extend self
38
+
39
+ def href(to:, **params)
40
+ q = query(**params)
41
+ q.empty? ? "mailto:#{to}" : "mailto:#{to}?#{q}"
42
+ end
43
+
44
+ def query(**params)
45
+ params
46
+ .compact # drop nils
47
+ .reject { |k, v| v.is_a?(Array) && v.empty? } # drop empty arrays
48
+ .map { |k, v| "#{k}=#{mailto_escape(v)}" }
49
+ .join("&")
50
+ end
51
+
52
+ private
53
+
54
+ def mailto_escape(str)
55
+ ERB::Util.url_encode(str.to_s).tr("+", "%20")
56
+ end
57
+ end
30
58
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supermail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-09-26 00:00:00.000000000 Z
10
+ date: 2025-09-29 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: actionmailer