dcidev_mailer 0.0.13 → 0.0.14

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: 5a56a9c54429232d7fd7d01cc3f657534537ca554886d5d77aa30c8f7ae93567
4
- data.tar.gz: 7404b2af018fe76212836484eb364b42ec5375c84d4f9a00844ae20ad2201c62
3
+ metadata.gz: 94b1d88c260563186badc0465bfc8890f3402f5f6b6cf334b432ea7a0f0186d5
4
+ data.tar.gz: 264d2847649172adefe30a6ab9c4ff61ed02460fb52d049a4700083dfa74431c
5
5
  SHA512:
6
- metadata.gz: db523c536b6164e8830f4de07d94ccb00d27c8fa6341748747830dcb45e2d7a1e147a3226837457da585c2942caeea818ee39e25bd2d5623ce53d2947bc6fdbc
7
- data.tar.gz: 85ac24510c11d94287baffe97b5a728badfb9ab8040297908a03061fc42c9db209cd04e8ee3c62391c6e7ff83bf11ff1db2123b7577b7099f27dc1584814f499
6
+ metadata.gz: c5a9251a9254bebc521d0613b0e5d9b5fb660a2d158ae032879f8bca9185c6a88035df082dc07e9b5bc868c078cad8c714ca78de600d27a59506f9faacc75fa8
7
+ data.tar.gz: 82fa420736919b8bfc686827b8643b9c1b7a83a39e0f6e3ced387517aa2405f4f3344fa9f09cfc55ca862dbd3dceb338ebc2db67b41d0b758a9e626b4e599669
data/README.md CHANGED
@@ -30,7 +30,7 @@ Add this to `config/initializer/mailer.rb`
30
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
35
  # uncomment if neccessary
36
36
  smtp_settings = {
@@ -63,9 +63,9 @@ ActionMailer::Base.raise_delivery_errors = true
63
63
  ActionMailer::Base.perform_deliveries = true
64
64
  ActionMailer::Base.perform_caching = false
65
65
 
66
- MandrillMailer.configure do |config |
66
+ MandrillMailer.configure do |config|
67
67
  config.api_key = ENV["MANDRILL_SMTP_PASSWORD"]
68
- config.deliver_later_queue_name =: default
68
+ config.deliver_later_queue_name :default
69
69
  end
70
70
  ```
71
71
 
@@ -82,25 +82,62 @@ class MandrillMailer
82
82
  class << self
83
83
  def send_email(customer: nil, email_template: nil, attachments: nil, description: nil)
84
84
  raise "invalid customer"if customer.nil?
85
- raise "invalid template" if email_template.nil ?
85
+ raise "invalid template" if email_template.nil?
86
86
  begin
87
87
  response = ::DcidevMailer::Mandrill.send_email(
88
88
  subject: email_template.subject,
89
89
  html_body: MailerHelper.format_wording(email_template.wording, customer),
90
- header_url: email_template.header.try(: url),
91
- footer_url: email_template.footer.try(: url),
92
- 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"}],
93
94
  cc: nil, # can be a string / array
94
95
  bcc: nil, # can be a string / array
95
96
  from: ENV['DEFAULT_EMAIL_SENDER'],
97
+ from_name: ENV['DEFAULT_EMAIL_SENDER_NAME'],
96
98
  attachments: attachments,
97
99
  email_template_path: "mail/blast.html.erb"
98
100
  # specify template file location
99
101
  )
100
102
  rescue => e
101
- error_message = "[SEND EMAIL] " + e.try(: to_s)
103
+ error_message = "[SEND EMAIL] " + e.try(:to_s)
102
104
  ensure
103
- 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
+ # specify template file location
136
+ )
137
+ rescue => e
138
+ error_message = "[SEND EMAIL] " + e.try(:to_s)
139
+ ensure
140
+ 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?
104
141
  ApplicationHistory.log(description: error_message || description, application: customer.application)
105
142
  end
106
143
  end
@@ -133,7 +170,7 @@ class MortgageMailer
133
170
  subject: template.subject,
134
171
  from: ENV['DEFAULT_EMAIL_SENDER'],
135
172
  template_path: "mail/blast.html.erb" # specify template file location
136
- ).deliver_now
173
+ ).deliver_now!
137
174
  rescue => e
138
175
  error_message = "[SEND EMAIL] " + e.try(:to_s)
139
176
  ensure
@@ -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
data/lib/dcidev_mailer.rb CHANGED
@@ -1,52 +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
- filetype = MimeMagic.by_magic(a[:file]).type.to_s
22
- content = a[:file].read
23
- name = "#{a[:filename]}.#{filetype.split("/")[1]}"
24
- att = {
25
- content: content,
26
- name: name,
27
- }
28
-
29
- att[:type] = filetype if ENV["USE_MANDRILL_MAILER"].to_i == 0
30
- formatted << att
31
- end
32
- formatted
33
- end
34
-
35
- def format_header_footer(header_url: "", footer_url: "", locals: {}, images: {})
36
- [{name: "header", url: header_url}, {name: "footer", url: footer_url}].each do |i|
37
- return if i[:url].nil?
38
- begin
39
- extension, encoded, _ = DcidevUtility.file_url_to_base64(i[:url])
40
- locals[i[:name].to_sym] = "cid:#{i[:name]}"
41
- images.push({content: encoded, encoded_content: encoded, type: extension, name: i[:name]})
42
- rescue => _
43
-
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
25
+ end
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
44
42
  end
45
- end
46
- 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
+
47
58
  end
48
59
 
49
- end
50
-
51
60
  end
52
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.13
4
+ version: 0.0.14
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-17 00:00:00.000000000 Z
11
+ date: 2022-03-21 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: []