dcidev_mailer 0.0.12 → 0.0.15

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: 92cf0dc63352424606b22ecfccce05ea3daa10fce535898e1e37c85095735ff2
4
- data.tar.gz: 15ed559e1470ce64ed6f49b6dd7445ff27e7048b4445fb600e16392a18246430
3
+ metadata.gz: 0f69e448cc0003dd316e2da0bc13de431fb85d40b9c10df80cd638a3d4a6a236
4
+ data.tar.gz: d472293d8faa437e9d5451a18804336b6f3801735e08509f7bd9483bf49d18f2
5
5
  SHA512:
6
- metadata.gz: b27fd936d828b8f08b1a06f0f77bed785a96d6770fa9762dfe4de8d9ade9a02687f9eec3537e4ab40caa0c07fc62701f00cf73184fcfdc30a3c5139d4f71a0fa
7
- data.tar.gz: 424b1ab0c90d99a540fa508a1be07c04c50d1405285354793d33c5130db8395a466ea24ae17a250af2ae20562a87ea08ac29504f8f824916c215fb820267d3b2
6
+ metadata.gz: d9f77543d8eb46288c0e541e8ce2cf739fe237c564bdda80f428f66e20a52d54f371daaf95b61af821bb58170f6cee46b213d88b57421faf35c09eda301d4962
7
+ data.tar.gz: 393d7d3cd196b507df3d56346cf701260d4f8a32386ec9544bb492f86e74a57a945a1dc71d841c11d85ba3c45d9c78e055ca4b30c0066032f6deb1fd83f02ab4
data/README.md CHANGED
@@ -27,13 +27,12 @@ MAILER_SMTP_TLS=
27
27
 
28
28
  Add this to `config/initializer/mailer.rb`
29
29
  ```ruby
30
- ActionMailer::Base.delivery_method =: smtp
30
+ ActionMailer::Base.delivery_method = :smtp
31
31
 
32
32
  # change it to your helper class name
33
- ActionMailer::Base.add_delivery_method: mandrill_mailer, MandrillMailer
33
+ ActionMailer::Base.add_delivery_method :mandrill_mailer, MandrillMailer
34
34
 
35
- # uncomment
36
- if neccessary
35
+ # uncomment if neccessary
37
36
  smtp_settings = {
38
37
  address: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_ADDRESS'] : ENV["MAILER_SMTP_ADDRESS"],
39
38
  # authentication: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? 'plain' : nil,
@@ -47,15 +46,15 @@ smtp_settings = {
47
46
  }
48
47
 
49
48
  if ENV["USE_MANDRILL_MAILER"].to_i == 1
50
- smtp_settings[: authentication] = 'plain'
51
- smtp_settings[: password] = ENV['MANDRILL_SMTP_PASSWORD']
52
- smtp_settings[: user_name] = ENV['MANDRILL_SMTP_USERNAME']
49
+ smtp_settings[:authentication] = 'plain'
50
+ smtp_settings[:password] = ENV['MANDRILL_SMTP_PASSWORD']
51
+ smtp_settings[:user_name] = ENV['MANDRILL_SMTP_USERNAME']
53
52
  end
54
53
 
55
54
  ActionMailer::Base.smtp_settings = smtp_settings
56
55
 
57
56
  ActionMailer::Base.default_url_options = {
58
- : host => ENV["BASE_URL_FE"]
57
+ host: ENV["BASE_URL_FE"]
59
58
  }
60
59
  ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)
61
60
 
@@ -64,9 +63,9 @@ ActionMailer::Base.raise_delivery_errors = true
64
63
  ActionMailer::Base.perform_deliveries = true
65
64
  ActionMailer::Base.perform_caching = false
66
65
 
67
- MandrillMailer.configure do |config |
66
+ MandrillMailer.configure do |config|
68
67
  config.api_key = ENV["MANDRILL_SMTP_PASSWORD"]
69
- config.deliver_later_queue_name =: default
68
+ config.deliver_later_queue_name :default
70
69
  end
71
70
  ```
72
71
 
@@ -83,25 +82,61 @@ class MandrillMailer
83
82
  class << self
84
83
  def send_email(customer: nil, email_template: nil, attachments: nil, description: nil)
85
84
  raise "invalid customer"if customer.nil?
86
- raise "invalid template" if email_template.nil ?
85
+ raise "invalid template" if email_template.nil?
87
86
  begin
88
87
  response = ::DcidevMailer::Mandrill.send_email(
89
88
  subject: email_template.subject,
90
89
  html_body: MailerHelper.format_wording(email_template.wording, customer),
91
- header_url: email_template.header.try(: url),
92
- footer_url: email_template.footer.try(: url),
93
- to: customer.email,
90
+ header_url: email_template.header.try(:url),
91
+ footer_url: email_template.footer.try(:url),
92
+ # to: customer.email / can also accept string
93
+ to: [{name: "Punto Damar P", type: "to", email: "punto@privyid.tech"}],
94
94
  cc: nil, # can be a string / array
95
95
  bcc: nil, # can be a string / array
96
96
  from: ENV['DEFAULT_EMAIL_SENDER'],
97
+ from_name: ENV['DEFAULT_EMAIL_SENDER_NAME'],
97
98
  attachments: attachments,
98
99
  email_template_path: "mail/blast.html.erb"
99
100
  # specify template file location
100
101
  )
101
102
  rescue => e
102
- error_message = "[SEND EMAIL] " + e.try(: to_s)
103
+ error_message = "[SEND EMAIL] " + e.try(:to_s)
103
104
  ensure
104
- 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 ?
105
+ 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?
106
+ ApplicationHistory.log(description: error_message || description, application: customer.application)
107
+ end
108
+ end
109
+ end
110
+ ```
111
+
112
+
113
+ ### Mandrill Template Example
114
+ ```ruby
115
+ require 'dcidev_mailer/mandrill_template'
116
+
117
+ class MandrillMailer
118
+ class << self
119
+ def send_email(customer: nil, email_template: nil, attachments: nil, description: nil)
120
+ raise "invalid customer"if customer.nil?
121
+ raise "invalid template" if email_template.nil?
122
+ begin
123
+ response = ::DcidevMailer::MandrillTemplate.send_email(
124
+ subject: email_template.subject,
125
+ header_url: email_template.header.try(:url),
126
+ footer_url: email_template.footer.try(:url),
127
+ template_name: 'customer blast',
128
+ to: [{name: "Punto Damar P", type: "to", email: "punto@privyid.tech"}],
129
+ vars: {customer_name: "Punto Damar P", bank_name: "Bang Jago"}, # template variable name configurable from mandrill dashboard
130
+ cc: nil, # can be a string / array
131
+ bcc: nil, # can be a string / array
132
+ from: ENV['DEFAULT_EMAIL_SENDER'],
133
+ from_name: ENV['DEFAULT_EMAIL_SENDER_NAME'],
134
+ attachments: attachments,
135
+ )
136
+ rescue => e
137
+ error_message = "[SEND EMAIL] " + e.try(:to_s)
138
+ ensure
139
+ 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?
105
140
  ApplicationHistory.log(description: error_message || description, application: customer.application)
106
141
  end
107
142
  end
@@ -134,7 +169,7 @@ class MortgageMailer
134
169
  subject: template.subject,
135
170
  from: ENV['DEFAULT_EMAIL_SENDER'],
136
171
  template_path: "mail/blast.html.erb" # specify template file location
137
- )
172
+ ).deliver_now!
138
173
  rescue => e
139
174
  error_message = "[SEND EMAIL] " + e.try(:to_s)
140
175
  ensure
@@ -161,6 +196,6 @@ DcidevMailer::format_image_from_html(html)
161
196
  DcidevMailer::format_attachments(attachments)
162
197
 
163
198
  # format email header & footer url to be embedded to html body
164
- # refer to class DcidevMailer::Mandrill or DcidevMailer::Rails to understand more of this method
199
+ # refer to class DcidevMailer::Mandrill or DcidevMailer::Rails for more details about the usage
165
200
  DcidevMailer::format_header_footer(header_url: "", footer_url: "", locals: {}, images: {})
166
201
  ```
@@ -5,19 +5,20 @@ 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, 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: '')
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).deliver_now
15
+ self.send_mail(subject, to, cc, bcc, html_body, attachments, images, from, from_name).deliver_now
16
16
  end
17
17
 
18
- def send_mail(subject, to, cc, bcc, html, attachments = nil, images = nil, from = nil)
18
+ def send_mail(subject, to, cc, bcc, html, attachments = nil, images = nil, from = nil, from_name = nil)
19
19
  mandrill_mail subject: subject,
20
20
  from: from,
21
+ from_name: from_name,
21
22
  # to: "dev.puntodamar@gmail.com",
22
23
  to: to,
23
24
  cc: cc,
@@ -0,0 +1,31 @@
1
+ require 'mandrill'
2
+
3
+ module DcidevMailer
4
+ class MandrillTemplate < MandrillMailer::TemplateMailer
5
+ default from: ENV['DEFAULT_EMAIL_SENDER']
6
+
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)
9
+ raise DcidevMailer::Errors::InvalidTemplate unless template_name.present?
10
+ images = DcidevMailer.format_images(images) if images.present?
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
13
+ end
14
+
15
+ def send_mail(subject, to, cc, bcc, attachments = nil, images = nil, from = nil, from_name = nil, template_name = nil, vars = nil)
16
+ mandrill_mail subject: subject,
17
+ from: from,
18
+ from_name: from_name,
19
+ to: to,
20
+ cc: cc,
21
+ bcc: bcc,
22
+ important: true,
23
+ inline_css: true,
24
+ attachments: attachments,
25
+ images: images,
26
+ template_name: template_name,
27
+ vars: vars
28
+ end
29
+ end
30
+ end
31
+ end
@@ -24,8 +24,8 @@ module DcidevMailer
24
24
  end
25
25
  end
26
26
 
27
- attachments.inline['header'] = File.read(DcidevUtility.download_to_file(header_url)) rescue nil if header_url.present?
28
- attachments.inline['footer'] = File.read(DcidevUtility.download_to_file(footer_url)) rescue nil if footer_url.present?
27
+ # attachments.inline['header'] = File.read(DcidevUtility.download_to_file(header_url)) rescue nil if header_url.present?
28
+ # attachments.inline['footer'] = File.read(DcidevUtility.download_to_file(footer_url)) rescue nil if footer_url.present?
29
29
 
30
30
  mail(
31
31
  to: to,
data/lib/dcidev_mailer.rb CHANGED
@@ -1,56 +1,61 @@
1
1
  module DcidevMailer
2
- class << self
3
- def format_image_from_html(html)
4
-
5
- images = []
6
- body_html = Nokogiri::HTML(html)
7
- temp = body_html
8
- if ENV["USE_MANDRILL_MAILER"].to_i == 1
9
- temp.search("img").each_with_index do |img, i|
10
- cid = "body_image_#{i}"
11
- images.push({content: nil, encoded_content: DcidevUtility.base64_encoded_string(img["src"]), type: DcidevUtility.base64_extension(img["src"]), name: cid})
12
- body_html.search("img")[i]["src"] = 'cid:' + cid
2
+ class << self
3
+ def format_image_from_html(html)
4
+
5
+ images = []
6
+ body_html = Nokogiri::HTML(html)
7
+ temp = body_html
8
+ if ENV["USE_MANDRILL_MAILER"].to_i == 1
9
+ temp.search("img").each_with_index do |img, i|
10
+ cid = "body_image_#{i}"
11
+ images.push({ content: nil, encoded_content: DcidevUtility.base64_encoded_string(img["src"]), type: DcidevUtility.base64_extension(img["src"]), name: cid })
12
+ body_html.search("img")[i]["src"] = 'cid:' + cid
13
+ end
14
+ end
15
+ [body_html.search("body")[0].children.to_html, images]
13
16
  end
14
- end
15
- [body_html.search("body")[0].children.to_html, images]
16
- end
17
-
18
- def format_attachments(attachments)
19
- formatted = []
20
- attachments.each do |a|
21
- begin
22
- filetype = MimeMagic.by_magic(a[:file]).type.to_s
23
- content = a[:file].read
24
- name = "#{a[:filename]}.#{filetype.split("/")[1]}"
25
- att = {
26
- content: content,
27
- name: name,
28
- }
29
-
30
- att[:type] = filetype if ENV["USE_MANDRILL_MAILER"].to_i == 0
31
- formatted << att
32
- rescue => _
33
-
17
+
18
+ def format_images(images)
19
+ formatted = []
20
+ images.each do |i|
21
+ base64 = Base64.encode64(i[:image])
22
+ formatted << {encoded_content: DcidevUtility.base64_encoded_string(base64), type: DcidevUtility.base64_extension(base64), name: i[:name]}
23
+ end
24
+ return formatted
34
25
  end
35
- end
36
- formatted
37
- end
38
-
39
- def format_header_footer(header_url: "", footer_url: "", locals: {}, images: {})
40
- [{name: "header", url: header_url}, {name: "footer", url: footer_url}].each do |i|
41
- return if i[:url].nil?
42
- begin
43
- extension, encoded, _ = DcidevUtility.file_url_to_base64(i[:url])
44
- locals[i[:name].to_sym] = "cid:#{i[:name]}"
45
- images.push({content: encoded, encoded_content: encoded, type: extension, name: i[:name]})
46
- rescue => _
47
-
26
+
27
+ def format_attachments(attachments)
28
+ formatted = []
29
+ attachments.each do |a|
30
+ filetype = MimeMagic.by_magic(a[:file]).type.to_s
31
+ content = a[:file].read
32
+ name = "#{a[:filename]}.#{filetype.split("/")[1]}"
33
+ att = {
34
+ content: content,
35
+ name: name,
36
+ }
37
+
38
+ att[:type] = filetype if ENV["USE_MANDRILL_MAILER"].to_i == 0
39
+ formatted << att
40
+ end
41
+ formatted
48
42
  end
49
- end
50
- return [locals, images]
43
+
44
+ def format_header_footer(header_url: "", footer_url: "", locals: {}, images: {})
45
+ [{ name: "header", url: header_url }, { name: "footer", url: footer_url }].each do |i|
46
+ return if i[:url].nil?
47
+ begin
48
+ extension, encoded, _ = DcidevUtility.file_url_to_base64(i[:url])
49
+ locals[i[:name].to_sym] = "cid:#{i[:name]}"
50
+ images.push({ content: encoded, encoded_content: encoded, type: extension, name: i[:name] })
51
+ rescue => _
52
+
53
+ end
54
+ end
55
+ return [locals, images]
56
+ end
57
+
51
58
  end
52
59
 
53
- end
54
-
55
60
  end
56
61
 
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.12
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Punto Damar P
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-12 00:00:00.000000000 Z
11
+ date: 2022-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mimemagic
@@ -51,11 +51,12 @@ files:
51
51
  - lib/dcidev_mailer/errors/invalid_recipients.rb
52
52
  - lib/dcidev_mailer/errors/invalid_template.rb
53
53
  - lib/dcidev_mailer/mandrill.rb
54
+ - lib/dcidev_mailer/mandrill_template.rb
54
55
  - lib/dcidev_mailer/rails_mailer.rb
55
- homepage:
56
+ homepage:
56
57
  licenses: []
57
58
  metadata: {}
58
- post_install_message:
59
+ post_install_message:
59
60
  rdoc_options: []
60
61
  require_paths:
61
62
  - lib
@@ -70,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
71
  - !ruby/object:Gem::Version
71
72
  version: '0'
72
73
  requirements: []
73
- rubygems_version: 3.0.6
74
- signing_key:
74
+ rubygems_version: 3.0.3.1
75
+ signing_key:
75
76
  specification_version: 4
76
77
  summary: Commonly used email codes
77
78
  test_files: []