mailpy 0.1.2 → 0.1.7

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: b255678f953bd249a58a64993bce153b0a44e6052fc66f69b9fe8eb443dbdfc9
4
- data.tar.gz: cbc257a407c283efe7c4fb194ea20e1765b4a3a883b0f9e2a5f9a90269581e23
3
+ metadata.gz: e8a89f05d3dd0d6875dd4721e7fddeb47932ea72bca76b29dba13426867f6caa
4
+ data.tar.gz: 25c5965f5b4938b48351dd43371cd0341512735cd83f9cd0579ed2f37c0051ae
5
5
  SHA512:
6
- metadata.gz: 4b91847f34fc2c9e12204c96c4229e8d4742cea7bd491f7525acbd8e7c2722baad32cbc715dccc3c12b6aaee1e3b8bd9c0b9db6ba3c1c5eb0cea51cfd1f3b01f
7
- data.tar.gz: 75ab4dd001cc488b7c6e5888f870d6a017f8d7e71b4f27b432764937038a4d087ea4bdffb8555e2a83fe1be823b9d49b3cd0712b66bedd2b03741dca4f82246f
6
+ metadata.gz: 8d82d4ab3cdc74541b61357735c6c29f0cb7af8fa70aedfb745a4c4255ff2f935fb28879d9db3c0a4cebcd38dbf1f5d75a50149c1d401ea14ce00688aeeea6e7
7
+ data.tar.gz: 67b066c8a6d5587c88e50116c889f0bcf616c09673aa1a6ccb1e4fed52a02c30847885fbccf0485754ef0896b8eff5b57d7cc4054398220d9a94230c4ac8eeca
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Mailpy
2
- Short description and motivation.
2
+ Action Mailer Adapter for Sending Email Through HTTP APIs. Mailpy will automatically switch to SMTP method in case your mailer server encounter any problem.
3
3
 
4
4
  ## Usage
5
5
  How to use my plugin.
@@ -11,6 +11,30 @@ Add this line to your application's Gemfile:
11
11
  gem 'mailpy'
12
12
  ```
13
13
 
14
+ Add configuration to your application environments. development.rb, staging.rb, or production.rb
15
+
16
+ ```ruby
17
+ config.action_mailer.delivery_method = :mailpy
18
+ config.action_mailer.perform_deliveries = true
19
+ config.action_mailer.raise_delivery_errors = true
20
+ config.action_mailer.mailpy_settings = {
21
+ endpoint: ENV['MAILER_API_ENDPOINT'],
22
+ headers: {
23
+ Authorization: ENV['MAILER_API_KEY_OR_AUTH_TOKEN']
24
+ }
25
+ }
26
+ config.action_mailer.smtp_settings = {
27
+ address: ENV['SMTP_ADDRESS'],
28
+ port: ENV['SMTP_PORT'],
29
+ domain: ENV['SMTP_DOMAIN'],
30
+ authentication: ENV['SMTP_AUTHENTICATION_METHOD'],
31
+ user_name: ENV['SMTP_USERNAME'],
32
+ password: ENV['SMTP_PASSWORD'],
33
+ enable_starttls_auto: true,
34
+ openssl_verify_mode: 'none'
35
+ }
36
+ ```
37
+
14
38
  And then execute:
15
39
  ```bash
16
40
  $ bundle
data/lib/mailpy.rb CHANGED
@@ -3,6 +3,8 @@ require "mailpy/mailer_api"
3
3
 
4
4
  module Mailpy
5
5
  class DeliveryMethod
6
+ MailpyDeliveryError = Class.new(StandardError)
7
+
6
8
  attr_accessor :settings
7
9
 
8
10
  def initialize(params)
@@ -10,20 +12,21 @@ module Mailpy
10
12
  end
11
13
 
12
14
  def deliver!(mail)
13
- MailerApi.new(mail_options(mail), mail.body.to_s).send
15
+ perform_send_request(mail, settings)
14
16
  end
15
17
 
16
18
  private
17
- def mail_options(mail)
18
- {
19
- to: mail.to.try(:join, ', '),
20
- cc: mail.cc.try(:join, ', '),
21
- bcc: mail.bcc.try(:join, ', '),
22
- sender: mail.from.try(:join, ', '),
23
- subject: mail.subject,
24
- endpoint: settings[:endpoint],
25
- token: settings[:token]
26
- }
19
+ def perform_send_request(mail, settings)
20
+ begin
21
+ result = MailerApi.new(mail, settings).send
22
+ raise(MailpyDeliveryError, JSON.parse(result.body)['message']) unless result.code === 200
23
+ result
24
+ rescue Errno::ECONNREFUSED
25
+ smtp_settings = Rails.application.config.action_mailer.smtp_settings
26
+ raise(MailpyDeliveryError, "SMTP miss configured. Please add SMTP configuration in your environment config") if smtp_settings.blank?
27
+ result = SMTPApi.new(mail, smtp_settings).send
28
+ result
27
29
  end
30
+ end
28
31
  end
29
32
  end
@@ -1,34 +1,45 @@
1
1
  require 'httparty'
2
2
 
3
3
  class MailerApi
4
- attr_reader :options, :message
5
-
6
- def initialize(options, message)
4
+ def initialize(mail, options = {})
5
+ @mail = mail
7
6
  @options = options
8
- @message = message
9
7
  end
10
8
 
11
9
  def send
12
- send_email
10
+ result = HTTParty.post(
11
+ options[:endpoint],
12
+ body: form_data,
13
+ headers: headers
14
+ )
13
15
  end
14
16
 
15
17
  private
16
- def send_email
17
- result = HTTParty.post(
18
- options[:endpoint],
19
- body: {
20
- sender_email: options[:sender],
21
- recipient_email: options[:to],
22
- cc_email: options[:cc],
23
- bcc_email: options[:bcc],
24
- subject_email: options[:subject],
25
- message_email: message
26
- }.to_json,
27
-
28
- headers: {
29
- 'Content-Type': 'application/json',
30
- 'Authorization': options[:token].to_s
18
+ attr_reader :mail, :options
19
+
20
+ def form_data
21
+ {
22
+ to: mail.to.try(:join, ', '),
23
+ cc: mail.cc.try(:join, ', '),
24
+ bcc: mail.bcc.try(:join, ', '),
25
+ from: mail.from.try(:join, ', '),
26
+ subject: mail.subject,
27
+ body: body_data,
28
+ attachment: attachment_data
31
29
  }
32
- )
33
- end
30
+ end
31
+
32
+ def body_data
33
+ mail.body.parts.present? ? mail.body.parts[0].body.to_s : mail.body.to_s
34
+ end
35
+
36
+ def attachment_data
37
+ attachments = {}
38
+ mail.attachments.each { |attachment| attachments[attachment.filename] = attachment.body }
39
+ attachments
40
+ end
41
+
42
+ def headers
43
+ options[:headers]
44
+ end
34
45
  end
@@ -0,0 +1,14 @@
1
+ class SMTPApi < ActionMailer::Base
2
+ def initialize(mail, options = {})
3
+ @mail = mail
4
+ @options = options
5
+ end
6
+
7
+ def send
8
+ mail.delivery_method :smtp, options
9
+ mail.deliver!
10
+ end
11
+
12
+ private
13
+ attr_reader :mail, :options
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Mailpy
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.7'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailpy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nasrul Gunawan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-25 00:00:00.000000000 Z
11
+ date: 2021-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Description of Mailpy.
41
+ description: Action Mailer Adapter for Send Email Through HTTP APIs.
42
42
  email:
43
43
  - nasrul.remaza@gmail.com
44
44
  executables: []
@@ -51,6 +51,7 @@ files:
51
51
  - lib/mailpy.rb
52
52
  - lib/mailpy/mailer_api.rb
53
53
  - lib/mailpy/railtie.rb
54
+ - lib/mailpy/smtp_api.rb
54
55
  - lib/mailpy/version.rb
55
56
  homepage: https://github.com/nasrulgunawan/mailpy
56
57
  licenses: