dcidev_mailer 0.0.7 → 0.0.8
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/README.md +141 -32
- data/lib/dcidev_mailer/errors/invalid_body.rb +9 -0
- data/lib/dcidev_mailer/errors/invalid_recipients.rb +9 -0
- data/lib/dcidev_mailer/errors/invalid_template.rb +9 -0
- data/lib/dcidev_mailer/mandrill.rb +3 -5
- data/lib/dcidev_mailer/rails_mailer.rb +79 -0
- metadata +5 -2
- data/lib/dcidev_mailer/rails.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4c888484deeed90b4688c4b671c3dcce1c0cd2e6c8246ef60a7779cfb73bd9f
|
4
|
+
data.tar.gz: 4d90779c9e75604a863487d060db02b8170de0cbdd2785e61478d4b3c20cf0e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9eca49e59d46f4bb88c6870c3bcd5bb468d6c8ce44c3f346dfbfc29cada557840fb8f3f7af5f65478ee38cda297063e9725b298b7aef423dc37e342870ec9b5a
|
7
|
+
data.tar.gz: 53a043bb683096d6f2d992d49da00c636beb7548753fe3676bbd71a81dea5830f8284e8677657c128a47d7754768f7650d4de2ecc1124f9260f35096ef32bbd4
|
data/README.md
CHANGED
@@ -1,40 +1,149 @@
|
|
1
|
+
# Tldr
|
2
|
+
This gem uses both Mandrill API and ActionMailer to send email.
|
1
3
|
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
# Setup
|
5
|
+
|
6
|
+
Add this to your `.env`
|
7
|
+
```env
|
8
|
+
BASE_URL_FE=
|
9
|
+
DEFAULT_EMAIL_SENDER=
|
10
|
+
USE_MANDRILL_MAILER=
|
11
|
+
MANDRILL_SMTP_ADDRESS=
|
12
|
+
MANDRILL_SMTP_DOMAIN=
|
13
|
+
MANDRILL_SMTP_PASSWORD=
|
14
|
+
MANDRILL_SMTP_USERNAME=
|
15
|
+
MANDRILL_SMTP_PORT=
|
16
|
+
MANDRILL_SMTP_AUTO_TLS=
|
17
|
+
MANDRILL_SMTP_TLS=
|
18
|
+
|
19
|
+
MAILER_SMTP_ADDRESS=
|
20
|
+
MAILER_SMTP_DOMAIN=
|
21
|
+
MAILER_SMTP_PASSWORD=
|
22
|
+
MAILER_SMTP_USERNAME=
|
23
|
+
MAILER_SMTP_PORT=
|
24
|
+
MAILER_SMTP_AUTO_TLS=
|
25
|
+
MAILER_SMTP_TLS=
|
13
26
|
```
|
14
|
-
|
15
|
-
mandril
|
16
27
|
|
28
|
+
Add this to `config/initializer/mailer.rb`
|
29
|
+
```ruby
|
30
|
+
ActionMailer::Base.delivery_method = :smtp
|
31
|
+
|
32
|
+
# change it to your helper class name
|
33
|
+
ActionMailer::Base.add_delivery_method :mandrill_mailer, MandrillMailer
|
34
|
+
|
35
|
+
# uncomment if neccessary
|
36
|
+
smtp_settings = {
|
37
|
+
address: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_ADDRESS'] : ENV["MAILER_SMTP_ADDRESS"],
|
38
|
+
# authentication: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? 'plain' : nil,
|
39
|
+
domain: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_DOMAIN'] : ENV["MAILER_SMTP_DOMAIN"],
|
40
|
+
enable_starttls_auto: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? true : false,
|
41
|
+
tls: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? true : false,
|
42
|
+
# password: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_PASSWORD'] : ENV["MAILER_SMTP_PASSWORD"],
|
43
|
+
port: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_PORT'] : ENV["MAILER_SMTP_PORT"],
|
44
|
+
# user_name: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_USERNAME'] : ENV["MAILER_SMTP_USERNAME"],
|
45
|
+
# openssl_verify_mode: "none",}
|
46
|
+
|
47
|
+
if ENV["USE_MANDRILL_MAILER"].to_i == 1
|
48
|
+
smtp_settings[:authentication] = 'plain'
|
49
|
+
smtp_settings[:password] = ENV['MANDRILL_SMTP_PASSWORD']
|
50
|
+
smtp_settings[:user_name] = ENV['MANDRILL_SMTP_USERNAME']
|
51
|
+
end
|
52
|
+
|
53
|
+
ActionMailer::Base.smtp_settings = smtp_settings
|
54
|
+
|
55
|
+
ActionMailer::Base.default_url_options = { :host => ENV["BASE_URL_FE"] }
|
56
|
+
ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)
|
57
|
+
|
58
|
+
# Don't care if the mailer can't send.
|
59
|
+
ActionMailer::Base.raise_delivery_errors = true
|
60
|
+
ActionMailer::Base.perform_deliveries = true
|
61
|
+
ActionMailer::Base.perform_caching = false
|
62
|
+
|
63
|
+
MandrillMailer.configure do |config|
|
64
|
+
config.api_key = ENV["MANDRILL_SMTP_PASSWORD"]
|
65
|
+
config.deliver_later_queue_name = :default
|
66
|
+
end
|
17
67
|
```
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
68
|
+
|
69
|
+
|
70
|
+
# How to Use
|
71
|
+
|
72
|
+
### Mandrill Example
|
73
|
+
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class MandrillMailer
|
77
|
+
class << self
|
78
|
+
def send_email(customer: nil, email_template: nil, attachments: nil, description: nil)
|
79
|
+
raise "invalid customer" if customer.nil?
|
80
|
+
raise "invalid template" if email_template.nil?
|
81
|
+
begin
|
82
|
+
response = ::DcidevMailer::Mandrill.send_email(
|
83
|
+
subject: email_template.subject,
|
84
|
+
html_body: MailerHelper.format_wording(email_template.wording, customer),
|
85
|
+
header_url: email_template.header.try(:url),
|
86
|
+
footer_url: email_template.footer.try(:url),
|
87
|
+
to: customer.email,
|
88
|
+
cc: nil,
|
89
|
+
bcc: nil,
|
90
|
+
from: ENV['DEFAULT_EMAIL_SENDER'],
|
91
|
+
attachments: attachments,
|
92
|
+
email_template_path: "mail/blast.html.erb" # specify template file location
|
93
|
+
)
|
94
|
+
rescue => e
|
95
|
+
error_message = "[SEND EMAIL] " + e.try(:to_s)
|
96
|
+
ensure
|
97
|
+
EmailHistory.create(application: customer.application, template: email_template, status: response[0]["status"] == "sent" ? :sent : :failed, mail_provider_id: response[0]["_id"], form_cetak_attachment: attachments.present?, error_message: response[0]["reject_reason"]) if response.present?
|
98
|
+
ApplicationHistory.log(description: error_message || description, application: customer.application)
|
99
|
+
end
|
100
|
+
end endend
|
28
101
|
```
|
29
102
|
|
30
|
-
|
103
|
+
### Action Mailer Example
|
104
|
+
```ruby
|
105
|
+
class MortgageMailer
|
106
|
+
class << self
|
107
|
+
def email(customer: nil, template: nil, file_attachments: nil, description: nil)
|
108
|
+
begin
|
109
|
+
raise "invalid customer" if customer.nil?
|
110
|
+
raise "invalid template" if template.nil?
|
111
|
+
raise "email is empty" if customer.email.nil?
|
112
|
+
|
113
|
+
wording, _ = DcidevMailer.format_image_from_html(template.wording)
|
114
|
+
wording = MailerHelper.format_wording(wording,customer)
|
115
|
+
|
116
|
+
DcidevMailer::RailsMailer.email(
|
117
|
+
html_body: wording,
|
118
|
+
header_url: template.header.try(:url),
|
119
|
+
footer_url: template.footer.try(:url),
|
120
|
+
file_attachments: file_attachments,
|
121
|
+
to: customer.email,
|
122
|
+
cc: nil,
|
123
|
+
bcc: nil,
|
124
|
+
subject: template.subject,
|
125
|
+
from: ENV['DEFAULT_EMAIL_SENDER'],
|
126
|
+
template_path: "mail/blast.html.erb" # specify template file location
|
127
|
+
)
|
128
|
+
rescue => e
|
129
|
+
error_message = "[SEND EMAIL] " + e.try(:to_s)
|
130
|
+
ensure
|
131
|
+
EmailHistory.create(status: error_message.present? ? :failed : :sent, error_message: error_message, application: customer.application, template: template, form_cetak_attachment: file_attachments.present?)
|
132
|
+
ApplicationHistory.log(description: error_message || description, application: customer.application)
|
133
|
+
end
|
134
|
+
end end
|
135
|
+
end
|
31
136
|
```
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
137
|
+
|
138
|
+
### Helpers
|
139
|
+
```ruby
|
140
|
+
# convert all image URL in <img src="url"> to <img src="cid:xxxx">
|
141
|
+
DcidevMailer::format_image_from_html(html)
|
142
|
+
|
143
|
+
# format array of attachment files
|
144
|
+
DcidevMailer::format_attachments(attachments)
|
145
|
+
|
146
|
+
# format email header & footer url to be embedded to html body
|
147
|
+
# refer to class DcidevMailer::Mandrill or DcidevMailer::Rails to understand more of this method
|
148
|
+
DcidevMailer::format_header_footer(header_url: "", footer_url: "", locals: {}, images: {})
|
40
149
|
```
|
@@ -5,23 +5,21 @@ module DcidevMailer
|
|
5
5
|
default from: ENV['DEFAULT_EMAIL_SENDER']
|
6
6
|
|
7
7
|
class << self
|
8
|
-
def send_email(subject: "", html_body: "", to: nil,
|
8
|
+
def send_email(subject: "", html_body: "", to: nil, from: nil, attachments: nil, email_template_path: "", header_url: "", footer_url: "")
|
9
9
|
ac = ActionController::Base.new
|
10
10
|
wording, images = DcidevMailer.format_image_from_html(html_body)
|
11
11
|
locals = {wording: wording, header: nil, footer: nil}
|
12
12
|
locals, images = DcidevMailer.format_header_footer(header_url: header_url, footer_url: footer_url, locals: locals, images: images) if header_url.present? && footer_url.present?
|
13
13
|
html_body = ac.render_to_string(template: email_template_path, locals: locals)
|
14
14
|
attachments = DcidevMailer.format_attachments(attachments) if attachments.present?
|
15
|
-
self.send_mail(subject, to,
|
15
|
+
self.send_mail(subject, to, html_body, attachments, images, from).deliver_now
|
16
16
|
end
|
17
17
|
|
18
|
-
def send_mail(subject, to,
|
18
|
+
def send_mail(subject, to, html, attachments = nil, images = nil, from = nil)
|
19
19
|
mandrill_mail subject: subject,
|
20
20
|
from: from,
|
21
21
|
# to: "dev.puntodamar@gmail.com",
|
22
22
|
to: to,
|
23
|
-
cc: cc,
|
24
|
-
bcc: bcc,
|
25
23
|
# to: { email: invitation.email, name: 'Honored Guest' },
|
26
24
|
html: html,
|
27
25
|
view_content_link: true,
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# require 'mail'
|
2
|
+
require 'action_mailer'
|
3
|
+
require 'action_view'
|
4
|
+
require 'mail'
|
5
|
+
require 'dcidev_mailer/errors/invalid_recipients'
|
6
|
+
require 'dcidev_mailer/errors/invalid_body'
|
7
|
+
require 'dcidev_mailer/errors/invalid_template'
|
8
|
+
module DcidevMailer
|
9
|
+
class RailsMailer < ActionMailer::Base
|
10
|
+
|
11
|
+
def email(html_body: "", header_url: "", footer_url: "", file_attachments: nil, to: nil, cc: nil, bcc: nil, from: nil, subject: "", template_path: "")
|
12
|
+
raise DcidevMailer::Errors::InvalidRecipients unless to.present?
|
13
|
+
raise DcidevMailer::Errors::InvalidBody unless html_body.present? && html_body.is_a?(String)
|
14
|
+
raise DcidevMailer::Errors::InvalidTemplate unless template_path.present?
|
15
|
+
wording, images = DcidevMailer.format_image_from_html(html_body)
|
16
|
+
|
17
|
+
locals = { wording: wording, header: nil, footer: nil }
|
18
|
+
locals, images = DcidevMailer.format_header_footer(header_url: header_url, footer_url: footer_url, locals: locals, images: images) if header_url.present? && footer_url.present?
|
19
|
+
|
20
|
+
if file_attachments.present?
|
21
|
+
file_attachments.each do |a|
|
22
|
+
am.attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
attachments.inline['header'] = File.read(Utility.download_to_file(header_url)) rescue nil if header_url.present?
|
27
|
+
attachments.inline['footer'] = File.read(Utility.download_to_file(footer_url)) rescue nil if footer_url.present?
|
28
|
+
|
29
|
+
mail(
|
30
|
+
to: to,
|
31
|
+
cc: cc,
|
32
|
+
bcc: bcc,
|
33
|
+
subject: subject,
|
34
|
+
format: "text/html",
|
35
|
+
from: from,
|
36
|
+
# template_path: "dcidev_mailer/rails_mailer",
|
37
|
+
# template_name: 'a',
|
38
|
+
) do |format|
|
39
|
+
format.html {
|
40
|
+
render locals: locals, html: ActionController::Base.new.render_to_string(template: template_path, locals: locals)
|
41
|
+
|
42
|
+
}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
# def self.method_missing(method_name, html_body: "", header_url: "", footer_url: "", file_attachments: nil, to: nil, cc: nil, bcc: nil, from: nil, subject: "")
|
48
|
+
# raise DcidevMailer::Errors::InvalidRecipients unless to.present?
|
49
|
+
# raise DcidevMailer::Errors::InvalidBody unless html_body.present? && html_body.is_a?(String)
|
50
|
+
# wording, images = DcidevMailer.format_image_from_html(html_body)
|
51
|
+
#
|
52
|
+
# locals = { wording: wording, header: nil, footer: nil }
|
53
|
+
# locals, images = DcidevMailer.format_header_footer(header_url: header_url, footer_url: footer_url, locals: locals, images: images) if header_url.present? && footer_url.present?
|
54
|
+
#
|
55
|
+
# if file_attachments.present?
|
56
|
+
# file_attachments.each do |a|
|
57
|
+
# am.attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# attachments.inline['header'] = File.read(Utility.download_to_file(header_url)) rescue nil if header_url.present?
|
62
|
+
# attachments.inline['footer'] = File.read(Utility.download_to_file(footer_url)) rescue nil if footer_url.present?
|
63
|
+
#
|
64
|
+
# mail(
|
65
|
+
# to: to,
|
66
|
+
# cc: cc,
|
67
|
+
# bcc: bcc,
|
68
|
+
# subject: subject,
|
69
|
+
# format: "text/html",
|
70
|
+
# from: from,
|
71
|
+
# ) do |format|
|
72
|
+
# format.html {
|
73
|
+
# render locals: locals
|
74
|
+
# }
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dcidev_mailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Punto Damar P
|
@@ -47,8 +47,11 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- README.md
|
49
49
|
- lib/dcidev_mailer.rb
|
50
|
+
- lib/dcidev_mailer/errors/invalid_body.rb
|
51
|
+
- lib/dcidev_mailer/errors/invalid_recipients.rb
|
52
|
+
- lib/dcidev_mailer/errors/invalid_template.rb
|
50
53
|
- lib/dcidev_mailer/mandrill.rb
|
51
|
-
- lib/dcidev_mailer/
|
54
|
+
- lib/dcidev_mailer/rails_mailer.rb
|
52
55
|
homepage:
|
53
56
|
licenses: []
|
54
57
|
metadata: {}
|
data/lib/dcidev_mailer/rails.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'mail'
|
2
|
-
|
3
|
-
module DcidevMailer
|
4
|
-
class Rails < ActionMailer::Base
|
5
|
-
default from: ENV['DEFAULT_EMAIL_SENDER']
|
6
|
-
|
7
|
-
class << self
|
8
|
-
def email(html_body: "", header_url: "", footer_url: "", file_attachments: nil, to: "", from: nil, subject: "")
|
9
|
-
wording, images = DcidevMailer.format_image_from_html(html_body)
|
10
|
-
locals = {wording: wording, header: nil, footer: nil}
|
11
|
-
locals, images = DcidevMailer.format_header_footer(header_url: header_url, footer_url: footer_url, locals: locals, images: images) if header_url.present? && footer_url.present?
|
12
|
-
|
13
|
-
if file_attachments.present?
|
14
|
-
file_attachments.each do |a|
|
15
|
-
attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
attachments.inline['header'] = File.read(Utility.download_to_file(header_url)) rescue nil if header_url.present?
|
20
|
-
attachments.inline['footer'] = File.read(Utility.download_to_file(footer_url)) rescue nil if footer_url.present?
|
21
|
-
|
22
|
-
mail(to: to, subject: subject, format: "text/html", from: from) do |format|
|
23
|
-
format.html {
|
24
|
-
render locals: locals
|
25
|
-
}
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|