dcidev_mailer 0.0.5 → 0.0.9

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: 63527600458fafe442b04ad42cc9afe6ce814587d6c7d3ca1df0a9c8ee2cfbf3
4
- data.tar.gz: 134af01794ea7b23d6075959f87b09e6952362059ddec4f3f5b64dabdd690771
3
+ metadata.gz: ed94880843b2769154af5f080cd85ecedbe73889f50da7d2f8d93745140264df
4
+ data.tar.gz: fe43144786157d05b25b80a1bfe2a6efd7b5c5396846a5c466018fd57e6a4a06
5
5
  SHA512:
6
- metadata.gz: 2359b8a5f941a4b2ee802386b2b27c8e1167a7543d7fadfd3615826368601c8e6e36a0f39264c180ff01e548f04b0582fd3852843ba3e134d5f641203f142a81
7
- data.tar.gz: b29ffd3b8554b5cd8767edff42e5cea95fc11c14f0e684f30957f8743c05019957954a01f3b9a994c35561a5782bbb8f06907c41bacd7bd7354228bad1bec791
6
+ metadata.gz: d022a4bfd340978d3a1b652a33cb9153b477eb98ad85a449599c87578f32b64cf5079b3ce4aed2ff06e6dd13158b32357e68c5f992424275b6819da61f60b3ab
7
+ data.tar.gz: 83d280862d6434e7c46151fcb17eaf90cec0ae77a630092c3ab785f7ec9f8f1f4afba346448db557a678fdbf684dd5a1789b2d500748d4dd275448e8e79d5cf4
data/README.md CHANGED
@@ -1,40 +1,160 @@
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
  ```
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
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
17
71
  ```
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
- )
72
+
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,
95
+ bcc: nil,
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
28
109
  ```
29
110
 
30
- action mailer
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,
133
+ bcc: nil,
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
31
147
  ```
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
- )
148
+
149
+ ### Helpers
150
+ ```ruby
151
+ # convert all image URL in <img src="url"> to <img src="cid:xxxx">
152
+ DcidevMailer::format_image_from_html(html)
153
+
154
+ # format array of attachment files
155
+ DcidevMailer::format_attachments(attachments)
156
+
157
+ # format email header & footer url to be embedded to html body
158
+ # refer to class DcidevMailer::Mandrill or DcidevMailer::Rails to understand more of this method
159
+ DcidevMailer::format_header_footer(header_url: "", footer_url: "", locals: {}, images: {})
40
160
  ```
@@ -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,25 +1,27 @@
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, 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, 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
- def send_mail(subject, to, html, attachments = nil, images = nil, from = nil)
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
+ cc: cc,
24
+ bcc: bcc,
23
25
  # to: { email: invitation.email, name: 'Honored Guest' },
24
26
  html: html,
25
27
  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,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.5
4
+ version: 0.0.9
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-10 00:00:00.000000000 Z
11
+ date: 2022-01-11 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/mail.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
51
53
  - lib/dcidev_mailer/mandrill.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.0.6
73
+ rubygems_version: 3.2.3
71
74
  signing_key:
72
75
  specification_version: 4
73
76
  summary: Commonly used email codes
@@ -1,48 +0,0 @@
1
-
2
-
3
- # require 'mail'
4
- require 'action_mailer/base'
5
- require 'action_view'
6
- require 'mail'
7
- module DcidevMailer
8
- class Mail < ActionMailer::Base
9
- default from: ENV['DEFAULT_EMAIL_SENDER']
10
- class << self
11
-
12
- def email(html_body: "", header_url: "", footer_url: "", file_attachments: nil, to: "", from: nil, subject: "", template_path: "", template_name: "")
13
- wording, images = DcidevMailer.format_image_from_html(html_body)
14
-
15
- @wording = "wording"
16
- locals = { wording: wording, header: nil, footer: nil }
17
- 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?
18
-
19
- att = ActionMailer::Base.new
20
- if file_attachments.present?
21
- file_attachments.each do |a|
22
- att.attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
23
- end
24
- end
25
-
26
- # self.attachments.inline['header'] = File.read(Utility.download_to_file(header_url)) rescue nil if header_url.present?
27
- # self.attachments.inline['footer'] = File.read(Utility.download_to_file(footer_url)) rescue nil if footer_url.present?
28
-
29
- att.attachments.inline['header'] = File.read(Utility.download_to_file(header_url)) rescue nil if header_url.present?
30
- att.attachments.inline['footer'] = File.read(Utility.download_to_file(footer_url)) rescue nil if footer_url.present?
31
-
32
- ActionMailer::Base.mail(
33
- to: to,
34
- subject: subject,
35
- format: "text/html",
36
- from: from,
37
- template_path: template_path,
38
- template_name: template_name
39
- ) do |format|
40
- format.html {
41
- ActionView::Base.new.render locals: locals
42
- }
43
- end
44
-
45
- end
46
- end
47
- end
48
- end