dcidev_mailer 0.0.6 → 0.0.10

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: 99da00135ff089f563f8843ac8eed71aff180563e8257c0c3ad50a5fea4627e1
4
- data.tar.gz: 9c07e164bde039359ef9ec2ab2102d0277d733970218f64ac6033e0639f838b2
3
+ metadata.gz: 1d5db8d1b62f6e33eb11d13d17387303403cdc9fbf3db7cfa19fd1df952be63b
4
+ data.tar.gz: e4bc5416ecc528cb1296937bacc7a0beb60cefed862b53a9291d490b8be6ac99
5
5
  SHA512:
6
- metadata.gz: defa57723a34010ec7fb48f680fd331dfa063c15b3790d976e8612718b9ee9550a260400d729a410bd79fad7bcc16de7aa41d4f326c3fa340d1a939eb04ddb51
7
- data.tar.gz: 60066e1f871cf6b8d56ea407ca01472e3fd8ee9848f34bd063852cca20e2d1ecdb99e8d2d964bd18eb67f9bfc1db724a241736bc4a7c10ad96f812993eb2d092
6
+ metadata.gz: bed99b742d0ab530425775cae53704fa6f3599a00b894a6fd51d1e45d43b936c56eeadd4c13c513c3bf22e2b32a64ad118d37ae32b12f9f915e97e916b257d5c
7
+ data.tar.gz: 6c69261bc24fabd8b53ed829045e44ff9ced03f9b1ef909ba6a30809a89e386d4477121c6f7a478dc7cde467844969c4e3c0a82977c5d810a98eee7706f334d8
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, # 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
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, # 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
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
  ```
@@ -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,
@@ -16,10 +16,11 @@ module DcidevMailer
16
16
 
17
17
  locals = { wording: wording, header: nil, footer: nil }
18
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
-
19
+
20
+ file_attachments = DcidevMailer.format_attachments(file_attachments) if file_attachments.present?
20
21
  if file_attachments.present?
21
22
  file_attachments.each do |a|
22
- am.attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
23
+ attachments[a[:name].to_s] = a[:content] unless a[:content].nil?
23
24
  end
24
25
  end
25
26
 
@@ -76,4 +77,4 @@ module DcidevMailer
76
77
  # end
77
78
 
78
79
  end
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.6
4
+ version: 0.0.10
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-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mimemagic
@@ -50,8 +50,8 @@ files:
50
50
  - lib/dcidev_mailer/errors/invalid_body.rb
51
51
  - lib/dcidev_mailer/errors/invalid_recipients.rb
52
52
  - lib/dcidev_mailer/errors/invalid_template.rb
53
- - lib/dcidev_mailer/mail.rb
54
53
  - lib/dcidev_mailer/mandrill.rb
54
+ - lib/dcidev_mailer/rails_mailer.rb
55
55
  homepage:
56
56
  licenses: []
57
57
  metadata: {}