supermail 0.1.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 +7 -0
- data/.rspec +3 -0
- data/README.md +94 -0
- data/Rakefile +8 -0
- data/lib/supermail/version.rb +5 -0
- data/lib/supermail.rb +85 -0
- data/sig/supermail.rbs +4 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 53951b27c057ea4419f1ffbbc3666189712d30449af65e539148d7df306de7d3
|
4
|
+
data.tar.gz: 789c9f9844880bae6611b9c5cdb958a4fb6b2d7c59888db016c503ebabec3573
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4de9d3383587ea03cd2eb302c6db564d88972535f286ed3ed2181146a56cc815d893e2a84543435854f92f39b767598bd5cef348516865f6706d290961f38d27
|
7
|
+
data.tar.gz: 6364025594980a3ca8776970cd20be025cbe044cc69dc55766f89e874842f691e0d2b244a0dba773f92976ca6d69ab28bc0956233d25195e1afbbed67723d956
|
data/.rspec
ADDED
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Supermail
|
2
|
+
|
3
|
+
Supermail is a slightly more intuitive way of organizing Emails in a Rails application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
```bash
|
10
|
+
bundle add supermail
|
11
|
+
```
|
12
|
+
|
13
|
+
Then install it in Rails.
|
14
|
+
|
15
|
+
```bash
|
16
|
+
rails generate supermail:install
|
17
|
+
```
|
18
|
+
|
19
|
+
This creates the `app/emails/application_email.rb` file that you can customize as the base for all emails.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class ApplicationEmail < Supermail::Email
|
23
|
+
def from = "Supermail <noreply@supermail.com>"
|
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,\nThe Supermail Team"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
To generate a new email, run the following command:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
rails generate supermail:email User::Welcome
|
45
|
+
```
|
46
|
+
|
47
|
+
This will create a new email class in `app/mailers/user/welcome.rb`.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# ./app/email/user/welcome.rb
|
51
|
+
class User::Welcome < ApplicationEmail
|
52
|
+
def initialize(user:)
|
53
|
+
@user = user
|
54
|
+
end
|
55
|
+
|
56
|
+
def subject = "Welcome to Supermail!"
|
57
|
+
|
58
|
+
class HTML
|
59
|
+
def view_template
|
60
|
+
h1 { "Welcome, #{@user.name}!" }
|
61
|
+
p { "We're excited to have you on board." }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class Text
|
66
|
+
def view_template
|
67
|
+
"Welcome, #{@user.name}!\n\nWe're excited to have you on board."
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Then, to send the email.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
User::Welcome.new(user: User.first).deliver_now
|
77
|
+
```
|
78
|
+
|
79
|
+
If you want to tweak the message on the fly, you can modify the message, then deliver it.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
User::Welcome.new(user: User.first).message.tap do
|
83
|
+
it.to << "another@example.com"
|
84
|
+
end.deliver_now
|
85
|
+
|
86
|
+
## Development
|
87
|
+
|
88
|
+
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.
|
89
|
+
|
90
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rubymonolith/supermail.
|
data/Rakefile
ADDED
data/lib/supermail.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "supermail/version"
|
4
|
+
|
5
|
+
require 'mail'
|
6
|
+
require "supermail/version"
|
7
|
+
|
8
|
+
module Supermail
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
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 }
|
53
|
+
|
54
|
+
mail.subject = subject if subject
|
55
|
+
|
56
|
+
# Bodies
|
57
|
+
if text_body
|
58
|
+
mail.text_part = Mail::Part.new do
|
59
|
+
body text_body
|
60
|
+
end
|
61
|
+
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!
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/sig/supermail.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: supermail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Gessler
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-04-17 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: mail
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '2.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '2.0'
|
26
|
+
description: Build emails with plain ol' Ruby objects.
|
27
|
+
email:
|
28
|
+
- bradgessler@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".rspec"
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- lib/supermail.rb
|
37
|
+
- lib/supermail/version.rb
|
38
|
+
- sig/supermail.rbs
|
39
|
+
homepage: https://github.com/rubymonolith/supermail
|
40
|
+
licenses: []
|
41
|
+
metadata:
|
42
|
+
allowed_push_host: https://rubygems.org
|
43
|
+
homepage_uri: https://github.com/rubymonolith/supermail
|
44
|
+
source_code_uri: https://github.com/rubymonolith/supermail
|
45
|
+
changelog_uri: https://github.com/rubymonolith/supermail
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.1.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.6.2
|
61
|
+
specification_version: 4
|
62
|
+
summary: Build emails with plain ol' Ruby objects.
|
63
|
+
test_files: []
|