dcidev_mailer 0.0.14 → 0.0.17
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 +39 -5
- data/lib/dcidev_mailer/mandrill.rb +5 -4
- data/lib/dcidev_mailer/mandrill_template.rb +5 -4
- data/lib/dcidev_mailer/rails_mailer.rb +19 -36
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f17117f9022e28dd492ea92310d25c16211f02a04376178027b069d1fc61ad1
|
4
|
+
data.tar.gz: fed2751d446175a98589df0e56bc22a7eb3181fab2bac922b35e3d63cd9fc285
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a873c97823a43f7fe38e8bbde83e77f6061baaa443b91b9412594f59c9e75ce2a0ac78b07c2b72c307e08bd6fcfffc8e69df3141c53330ce2359f6ce4ad594c0
|
7
|
+
data.tar.gz: 2577fce86abfe175086a91e6d6e245a3aa4af91bd1e64b065f5470f4f6f8ec63138417aa91116353f9eaaf9df7a3e50ca062464b24448f4d1d5443d06e96d376
|
data/README.md
CHANGED
@@ -3,7 +3,12 @@ This gem uses both Mandrill API and ActionMailer to send email.
|
|
3
3
|
|
4
4
|
# Setup
|
5
5
|
|
6
|
-
|
6
|
+
Install mandrill mailer gem
|
7
|
+
```
|
8
|
+
gem 'mandrill_mailer'
|
9
|
+
```
|
10
|
+
|
11
|
+
Add `.env` configurations
|
7
12
|
```env
|
8
13
|
BASE_URL_FE=
|
9
14
|
DEFAULT_EMAIL_SENDER=
|
@@ -132,7 +137,6 @@ class MandrillMailer
|
|
132
137
|
from: ENV['DEFAULT_EMAIL_SENDER'],
|
133
138
|
from_name: ENV['DEFAULT_EMAIL_SENDER_NAME'],
|
134
139
|
attachments: attachments,
|
135
|
-
# specify template file location
|
136
140
|
)
|
137
141
|
rescue => e
|
138
142
|
error_message = "[SEND EMAIL] " + e.try(:to_s)
|
@@ -164,11 +168,11 @@ class MortgageMailer
|
|
164
168
|
header_url: template.header.try(:url),
|
165
169
|
footer_url: template.footer.try(:url),
|
166
170
|
file_attachments: file_attachments,
|
167
|
-
to: customer.email,
|
171
|
+
to: "#{customer.name} <#{customer.email}>",
|
168
172
|
cc: nil, # can be a string / array
|
169
|
-
bcc:
|
173
|
+
bcc: ["John Doe <john.doe@gmail.com>", "Michael <michael@gmail.com>"], # can be a string / array
|
170
174
|
subject: template.subject,
|
171
|
-
from: ENV['DEFAULT_EMAIL_SENDER'],
|
175
|
+
from: "#{ENV['DEFAULT_EMAIL_SENDER_NAME']} <#{ENV['DEFAULT_EMAIL_SENDER']}>",
|
172
176
|
template_path: "mail/blast.html.erb" # specify template file location
|
173
177
|
).deliver_now!
|
174
178
|
rescue => e
|
@@ -188,6 +192,36 @@ The attachment is an array of hashes containing attachment file and filename
|
|
188
192
|
attachments = [{file: DcidevUtility.download_to_file(self.ktp.url), filename: self.reference_number}]
|
189
193
|
```
|
190
194
|
|
195
|
+
### Inline Images
|
196
|
+
`MandrillMailer` supports sending images in the html body. As for `RailsMailer`, it currently only supports header & footer image. Feel free to contact me if you need such feature.
|
197
|
+
|
198
|
+
Since gmail do not support link-based image, you have to format the images as inline attachments and use CID (Content-ID) to display each of them. The gem automatically takes care of the programming. You only have to specify a valid link as the parameter.
|
199
|
+
|
200
|
+
The example to set the image in the template is shown below. This method works for both `RailsMailer` and `MandrillMailer` so you only need to code once.
|
201
|
+
```html
|
202
|
+
<%
|
203
|
+
header = attachments['header'].try(:url) || header
|
204
|
+
footer = attachments['footer'].try(:url) || footer
|
205
|
+
%>
|
206
|
+
|
207
|
+
<% if header.present? %>
|
208
|
+
<div id="header-section-img">
|
209
|
+
|
210
|
+
<img
|
211
|
+
src="<%= header %>"
|
212
|
+
alt="Header Image"
|
213
|
+
style="border-radius: 10px"
|
214
|
+
width="100%"
|
215
|
+
/>
|
216
|
+
|
217
|
+
</div>
|
218
|
+
<% end %>
|
219
|
+
```
|
220
|
+
|
221
|
+
### Development tips for custom SMTP server
|
222
|
+
Some clients dont allow our server to access their SMTP server. To avoid blind coding, you can develop using google SMTP server to make sure that your program woks perfectly.
|
223
|
+
Refer to https://dev.to/morinoko/sending-emails-in-rails-with-action-mailer-and-gmail-35g4 for the tutorial.
|
224
|
+
|
191
225
|
### Helpers
|
192
226
|
```ruby
|
193
227
|
# convert all image URL in <img src="url"> to <img src="cid:xxxx">
|
@@ -5,17 +5,17 @@ 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, cc: nil, bcc: nil, from: nil, from_name: nil, attachments: nil, email_template_path: '', header_url: '', footer_url: '')
|
8
|
+
def send_email(subject: '', html_body: '', to: nil, cc: nil, bcc: nil, from: nil, from_name: nil, attachments: nil, email_template_path: '', header_url: '', footer_url: '', preserve_recipients: false)
|
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, cc, bcc, html_body, attachments, images, from, from_name).deliver_now
|
15
|
+
self.send_mail(subject, to, cc, bcc, html_body, attachments, images, from, from_name, preserve_recipients).deliver_now
|
16
16
|
end
|
17
17
|
|
18
|
-
def send_mail(subject, to, cc, bcc, html, attachments = nil, images = nil, from = nil, from_name = nil)
|
18
|
+
def send_mail(subject, to, cc, bcc, html, attachments = nil, images = nil, from = nil, from_name = nil, preserve_recipients)
|
19
19
|
mandrill_mail subject: subject,
|
20
20
|
from: from,
|
21
21
|
from_name: from_name,
|
@@ -29,7 +29,8 @@ module DcidevMailer
|
|
29
29
|
important: true,
|
30
30
|
inline_css: true,
|
31
31
|
attachments: attachments,
|
32
|
-
images: images
|
32
|
+
images: images,
|
33
|
+
preserve_recipients: cc.present? || preserve_recipients
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
@@ -5,14 +5,14 @@ module DcidevMailer
|
|
5
5
|
default from: ENV['DEFAULT_EMAIL_SENDER']
|
6
6
|
|
7
7
|
class << self
|
8
|
-
def send_email(subject: '', to: nil, cc: nil, bcc: nil, from: nil, from_name: nil, attachments: nil, vars: nil, template_name: nil, images: nil)
|
8
|
+
def send_email(subject: '', to: nil, cc: nil, bcc: nil, from: nil, from_name: nil, attachments: nil, vars: nil, template_name: nil, images: nil, preserve_recipients: false)
|
9
9
|
raise DcidevMailer::Errors::InvalidTemplate unless template_name.present?
|
10
10
|
images = DcidevMailer.format_images(images) if images.present?
|
11
11
|
attachments = DcidevMailer.format_attachments(attachments) if attachments.present?
|
12
|
-
self.send_mail(subject, to, cc, bcc, attachments, images, from, from_name, template_name, vars).deliver_now
|
12
|
+
self.send_mail(subject, to, cc, bcc, attachments, images, from, from_name, template_name, vars, preserve_recipients).deliver_now
|
13
13
|
end
|
14
14
|
|
15
|
-
def send_mail(subject, to, cc, bcc, attachments = nil, images = nil, from = nil, from_name = nil, template_name = nil, vars = nil)
|
15
|
+
def send_mail(subject, to, cc, bcc, attachments = nil, images = nil, from = nil, from_name = nil, template_name = nil, vars = nil, preserve_recipients)
|
16
16
|
mandrill_mail subject: subject,
|
17
17
|
from: from,
|
18
18
|
from_name: from_name,
|
@@ -24,7 +24,8 @@ module DcidevMailer
|
|
24
24
|
attachments: attachments,
|
25
25
|
images: images,
|
26
26
|
template_name: template_name,
|
27
|
-
vars: vars
|
27
|
+
vars: vars,
|
28
|
+
preserve_recipients: cc.present? || preserve_recipients
|
28
29
|
end
|
29
30
|
end
|
30
31
|
end
|
@@ -5,6 +5,7 @@ require 'mail'
|
|
5
5
|
require 'dcidev_mailer/errors/invalid_recipients'
|
6
6
|
require 'dcidev_mailer/errors/invalid_body'
|
7
7
|
require 'dcidev_mailer/errors/invalid_template'
|
8
|
+
|
8
9
|
module DcidevMailer
|
9
10
|
class RailsMailer < ActionMailer::Base
|
10
11
|
|
@@ -15,7 +16,7 @@ module DcidevMailer
|
|
15
16
|
wording, images = DcidevMailer.format_image_from_html(html_body)
|
16
17
|
|
17
18
|
locals = { wording: wording, header: nil, footer: nil }
|
18
|
-
locals,
|
19
|
+
locals, _ = 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
|
|
20
21
|
file_attachments = DcidevMailer.format_attachments(file_attachments) if file_attachments.present?
|
21
22
|
if file_attachments.present?
|
@@ -24,8 +25,23 @@ module DcidevMailer
|
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
begin
|
29
|
+
header_file = File.read(DcidevUtility.download_to_file(header_url))
|
30
|
+
header_mime = MimeMagic.by_magic(header_file)
|
31
|
+
attachments.inline['header'] = header_file
|
32
|
+
attachments.inline['header']['content-type'] = header_mime
|
33
|
+
attachments.inline['header']['content-id'] = '<header>'
|
34
|
+
rescue => _
|
35
|
+
end
|
36
|
+
|
37
|
+
begin
|
38
|
+
footer_file = File.read(DcidevUtility.download_to_file(footer_url))
|
39
|
+
footer_mime = MimeMagic.by_magic(footer_file)
|
40
|
+
attachments.inline['footer'] = footer_file
|
41
|
+
attachments.inline['footer']['content-type'] = footer_mime
|
42
|
+
attachments.inline['footer']['content-id'] = '<footer>'
|
43
|
+
rescue => _
|
44
|
+
end
|
29
45
|
|
30
46
|
mail(
|
31
47
|
to: to,
|
@@ -43,38 +59,5 @@ module DcidevMailer
|
|
43
59
|
}
|
44
60
|
end
|
45
61
|
end
|
46
|
-
|
47
|
-
|
48
|
-
# def self.method_missing(method_name, html_body: "", header_url: "", footer_url: "", file_attachments: nil, to: nil, cc: nil, bcc: nil, from: nil, subject: "")
|
49
|
-
# raise DcidevMailer::Errors::InvalidRecipients unless to.present?
|
50
|
-
# raise DcidevMailer::Errors::InvalidBody unless html_body.present? && html_body.is_a?(String)
|
51
|
-
# wording, images = DcidevMailer.format_image_from_html(html_body)
|
52
|
-
#
|
53
|
-
# locals = { wording: wording, header: nil, footer: nil }
|
54
|
-
# 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?
|
55
|
-
#
|
56
|
-
# if file_attachments.present?
|
57
|
-
# file_attachments.each do |a|
|
58
|
-
# am.attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
|
59
|
-
# end
|
60
|
-
# end
|
61
|
-
#
|
62
|
-
# attachments.inline['header'] = File.read(Utility.download_to_file(header_url)) rescue nil if header_url.present?
|
63
|
-
# attachments.inline['footer'] = File.read(Utility.download_to_file(footer_url)) rescue nil if footer_url.present?
|
64
|
-
#
|
65
|
-
# mail(
|
66
|
-
# to: to,
|
67
|
-
# cc: cc,
|
68
|
-
# bcc: bcc,
|
69
|
-
# subject: subject,
|
70
|
-
# format: "text/html",
|
71
|
-
# from: from,
|
72
|
-
# ) do |format|
|
73
|
-
# format.html {
|
74
|
-
# render locals: locals
|
75
|
-
# }
|
76
|
-
# end
|
77
|
-
# end
|
78
|
-
|
79
62
|
end
|
80
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Punto Damar P
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mimemagic
|
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '0'
|
73
73
|
requirements: []
|
74
|
-
rubygems_version: 3.
|
74
|
+
rubygems_version: 3.1.2
|
75
75
|
signing_key:
|
76
76
|
specification_version: 4
|
77
77
|
summary: Commonly used email codes
|