dcidev_mailer 0.0.7 → 0.0.11

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: 3a094879390e6716b70f8229366ba24f1a84c0b1aac5f7565035258326ce071f
4
- data.tar.gz: bece3752e7a948a9ad67e1687d8b06be4ac5e54eb90859e6eb802280c14e18b3
3
+ metadata.gz: bb6173e1193c0846051ef2e44ad344a96e2f63d9933033301018de3a2d4222ce
4
+ data.tar.gz: 1ce7c2291785904996735afbff6fb30375342b2443af29edd9142c90e4b9ce57
5
5
  SHA512:
6
- metadata.gz: 707e59fb856b39ee66f8081998167e7df4e2e5307416e8fb3e0c41c5c480334e475639a3c5965e3031997edffb5fb7b6eab005e89891912c14f4918f58290f6c
7
- data.tar.gz: edbefefe835b479c602dc4538200fa3ef52246ecd081586ae77bae98ed861c28f53e53ca3e932421c50fe33471e4ae355dc6345f886cd2c7da182899b1164536
6
+ metadata.gz: 3c098fb341715fba1baef58db3b37aca8e8cb5c4351b9c1f6b419a437c29778fd895d9d66234f2cf828fdc2f67026567cb0ee5999b92ac09b3c259c207276ce9
7
+ data.tar.gz: '058ec2bfb05d41dcfb7f4928abaa1ea633a45e2bee6353383616d0f57d7b1216ac2152b42da543dd3df290b232537f87b59bca2d760fe3c6d32888efb7be0fee'
data/README.md CHANGED
@@ -1,40 +1,166 @@
1
+ # Tldr
2
+ This gem uses both Mandrill API and ActionMailer to send email.
1
3
 
2
- ENV :
3
- ```
4
- DEFAULT_EMAIL_SENDER
5
- USE_MANDRILL_MAILER
6
- [EMAIL_PROVIDER]_SMTP_ADDRESS
7
- [EMAIL_PROVIDER]_SMTP_DOMAIN
8
- [EMAIL_PROVIDER]_SMTP_PASSWORD
9
- [EMAIL_PROVIDER]_SMTP_USERNAME
10
- [EMAIL_PROVIDER]_SMTP_PORT
11
- [EMAIL_PROVIDER]_SMTP_AUTO_TLS
12
- [EMAIL_PROVIDER]_SMTP_TLS
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
  ```
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
36
+ if neccessary
37
+ smtp_settings = {
38
+ address: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_ADDRESS'] : ENV["MAILER_SMTP_ADDRESS"],
39
+ # authentication: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? 'plain' : nil,
40
+ domain: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_DOMAIN'] : ENV["MAILER_SMTP_DOMAIN"],
41
+ enable_starttls_auto: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? true : false,
42
+ tls: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? true : false,
43
+ # password: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_PASSWORD'] : ENV["MAILER_SMTP_PASSWORD"],
44
+ port: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_PORT'] : ENV["MAILER_SMTP_PORT"],
45
+ # user_name: ENV["USE_MANDRILL_MAILER"].to_i == 1 ? ENV['MANDRILL_SMTP_USERNAME'] : ENV["MAILER_SMTP_USERNAME"],
46
+ # openssl_verify_mode: "none",
47
+ }
48
+
49
+ 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']
53
+ end
54
+
55
+ ActionMailer::Base.smtp_settings = smtp_settings
56
+
57
+ ActionMailer::Base.default_url_options = {
58
+ : host => ENV["BASE_URL_FE"]
59
+ }
60
+ ActionMailer::Base.register_preview_interceptor(ActionMailer::InlinePreviewInterceptor)
61
+
62
+ # Don 't care if the mailer can't send.
63
+ ActionMailer::Base.raise_delivery_errors = true
64
+ ActionMailer::Base.perform_deliveries = true
65
+ ActionMailer::Base.perform_caching = false
66
+
67
+ MandrillMailer.configure do |config |
68
+ config.api_key = ENV["MANDRILL_SMTP_PASSWORD"]
69
+ config.deliver_later_queue_name =: default
70
+ end
71
+ ```
14
72
 
15
- mandril
16
73
 
74
+ # How to Use
75
+
76
+ ### Mandrill Example
77
+
78
+
79
+ ```ruby
80
+ require 'dcidev_mailer/mandrill'
81
+
82
+ class MandrillMailer
83
+ class << self
84
+ def send_email(customer: nil, email_template: nil, attachments: nil, description: nil)
85
+ raise "invalid customer"if customer.nil?
86
+ raise "invalid template" if email_template.nil ?
87
+ begin
88
+ response = ::DcidevMailer::Mandrill.send_email(
89
+ subject: email_template.subject,
90
+ 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,
94
+ cc: nil, # can be a string / array
95
+ bcc: nil, # can be a string / array
96
+ from: ENV['DEFAULT_EMAIL_SENDER'],
97
+ attachments: attachments,
98
+ email_template_path: "mail/blast.html.erb"
99
+ # specify template file location
100
+ )
101
+ rescue => e
102
+ error_message = "[SEND EMAIL] " + e.try(: to_s)
103
+ 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
+ ApplicationHistory.log(description: error_message || description, application: customer.application)
106
+ end
107
+ end
108
+ end
17
109
  ```
18
- response = ::DcidevMailer::Mandrill.send_email(
19
- subject: email_template.subject,
20
- html_body: email_template.wording,
21
- header_url: email_template.header.try(:url),
22
- footer_url: email_template.footer.try(:url),
23
- to: customer.email,
24
- # from: "from@gmail.com",
25
- attachments: attachments,
26
- email_template_path: "cimb_mailer/email.html.erb"
27
- )
110
+
111
+ ### Action Mailer Example
112
+ ```ruby
113
+ require 'dcidev_mailer/rails_mailer'
114
+
115
+ class MortgageMailer
116
+ class << self
117
+ def email(customer: nil, template: nil, file_attachments: nil, description: nil)
118
+ begin
119
+ raise "invalid customer" if customer.nil?
120
+ raise "invalid template" if template.nil?
121
+ raise "email is empty" if customer.email.nil?
122
+
123
+ wording, _ = DcidevMailer.format_image_from_html(template.wording)
124
+ wording = MailerHelper.format_wording(wording,customer)
125
+
126
+ DcidevMailer::RailsMailer.email(
127
+ html_body: wording,
128
+ header_url: template.header.try(:url),
129
+ footer_url: template.footer.try(:url),
130
+ file_attachments: file_attachments,
131
+ to: customer.email,
132
+ cc: nil, # can be a string / array
133
+ bcc: nil, # can be a string / array
134
+ subject: template.subject,
135
+ from: ENV['DEFAULT_EMAIL_SENDER'],
136
+ template_path: "mail/blast.html.erb" # specify template file location
137
+ )
138
+ rescue => e
139
+ error_message = "[SEND EMAIL] " + e.try(:to_s)
140
+ ensure
141
+ EmailHistory.create(status: error_message.present? ? :failed : :sent, error_message: error_message, application: customer.application, template: template, form_cetak_attachment: file_attachments.present?)
142
+ ApplicationHistory.log(description: error_message || description, application: customer.application)
143
+ end
144
+ end
145
+ end
146
+ end
28
147
  ```
29
148
 
30
- action mailer
149
+ ### Sending Attachments
150
+ The attachment is an array of hashes containing attachment file and filename
151
+ ```ruby
152
+ attachments = [{file: DcidevUtility.download_to_file(self.ktp.url), filename: self.reference_number}]
31
153
  ```
32
- DcidevMailer::Rails.email(
33
- html_body: template.wording,
34
- header_url: template.header.try(:url),
35
- footer_url: template.footer.try(:url),
36
- file_attachments: file_attachments,
37
- to: customer.email,
38
- subject: template.subject
39
- )
154
+
155
+ ### Helpers
156
+ ```ruby
157
+ # convert all image URL in <img src="url"> to <img src="cid:xxxx">
158
+ DcidevMailer::format_image_from_html(html)
159
+
160
+ # format array of attachment files
161
+ DcidevMailer::format_attachments(attachments)
162
+
163
+ # 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
165
+ DcidevMailer::format_header_footer(header_url: "", footer_url: "", locals: {}, images: {})
40
166
  ```
@@ -0,0 +1,9 @@
1
+ module DcidevMailer
2
+ module Errors
3
+ class InvalidBody < StandardError
4
+ def to_s
5
+ "Email body must be HTML string"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module DcidevMailer
2
+ module Errors
3
+ class InvalidRecipients < StandardError
4
+ def to_s
5
+ "Must have at lease one recipient"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module DcidevMailer
2
+ module Errors
3
+ class InvalidTemplate < StandardError
4
+ def to_s
5
+ "Missing email template"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,23 +1,23 @@
1
- require "mandrill"
1
+ require 'mandrill'
2
2
 
3
3
  module DcidevMailer
4
4
  class Mandrill < MandrillMailer::MessageMailer
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, 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
- locals = {wording: wording, header: nil, footer: nil}
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).deliver_now
16
16
  end
17
17
 
18
18
  def send_mail(subject, to, cc, bcc, html, attachments = nil, images = nil, from = nil)
19
19
  mandrill_mail subject: subject,
20
- from: from,
20
+ from: from,
21
21
  # to: "dev.puntodamar@gmail.com",
22
22
  to: to,
23
23
  cc: cc,
@@ -0,0 +1,80 @@
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
+ file_attachments = DcidevMailer.format_attachments(file_attachments) if file_attachments.present?
21
+ if file_attachments.present?
22
+ file_attachments.each do |a|
23
+ attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
24
+ end
25
+ end
26
+
27
+ attachments.inline['header'] = File.read(Utility.download_to_file(header_url)) rescue nil if header_url.present?
28
+ attachments.inline['footer'] = File.read(Utility.download_to_file(footer_url)) rescue nil if footer_url.present?
29
+
30
+ mail(
31
+ to: to,
32
+ cc: cc,
33
+ bcc: bcc,
34
+ subject: subject,
35
+ format: "text/html",
36
+ from: from,
37
+ # template_path: "dcidev_mailer/rails_mailer",
38
+ # template_name: 'a',
39
+ ) do |format|
40
+ format.html {
41
+ render locals: locals, html: ActionController::Base.new.render_to_string(template: template_path, locals: locals)
42
+
43
+ }
44
+ end
45
+ 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
+ end
80
+ 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.7
4
+ version: 0.0.11
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-01-11 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mimemagic
@@ -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/rails.rb
54
+ - lib/dcidev_mailer/rails_mailer.rb
52
55
  homepage:
53
56
  licenses: []
54
57
  metadata: {}
@@ -67,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
70
  - !ruby/object:Gem::Version
68
71
  version: '0'
69
72
  requirements: []
70
- rubygems_version: 3.2.3
73
+ rubygems_version: 3.0.6
71
74
  signing_key:
72
75
  specification_version: 4
73
76
  summary: Commonly used email codes
@@ -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