thecore_backend_commons 3.2.9 → 3.2.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 +4 -4
- data/README.md +73 -0
- data/app/mailers/concerns/smtp_deliverable.rb +32 -0
- data/lib/tasks/thecore_backend_commons_tasks.rake +11 -4
- data/lib/thecore_backend_commons/smtp_config.rb +42 -0
- data/lib/thecore_backend_commons/smtp_tester.rb +79 -0
- data/lib/thecore_backend_commons/version.rb +1 -1
- data/lib/thecore_backend_commons.rb +2 -1
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fe73ef2835e94090bd037724805658c43bb87820a68d2fc86eadd35747ef8e94
|
|
4
|
+
data.tar.gz: 707da7e03c6f3bad7499c95fa8616e0f43ff75feed46ebe0159ec05a0594f06f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 047fff2d4b57e67c7320df0e8d38874603084e91cab99d8e5802807bcf903f75c503360fa5e4845692876906db4118f720457f631c9a51cfbaf65099ed0a8a39
|
|
7
|
+
data.tar.gz: 10d926f3d871d4adb4ce2cc7a721b930d32a608ceb2505fc37e07f1408c0a96fe7f7e1d23c065a55e35d344d3433e2dc4fc1c7cb2778e5550079eeea56031ce4
|
data/README.md
CHANGED
|
@@ -1 +1,74 @@
|
|
|
1
1
|
This is part of Thecore framework: https://github.com/gabrieletassoni/thecore/tree/release/3
|
|
2
|
+
|
|
3
|
+
## Invio email e configurazione SMTP
|
|
4
|
+
|
|
5
|
+
### Configurazione
|
|
6
|
+
|
|
7
|
+
Le impostazioni SMTP sono salvate in `ThecoreSettings` (namespace `:smtp`) e vengono caricate automaticamente dai seeds del gem:
|
|
8
|
+
|
|
9
|
+
| Chiave | Descrizione |
|
|
10
|
+
| ------------------- | --------------------------------------------------------------------------- |
|
|
11
|
+
| `address` | Indirizzo del server SMTP (obbligatorio per abilitare la consegna) |
|
|
12
|
+
| `port` | Porta (default `587`); impostare `465` per SMTPS (SSL implicito) |
|
|
13
|
+
| `domain` | Dominio HELO |
|
|
14
|
+
| `user_name` | Username per l'autenticazione |
|
|
15
|
+
| `password` | Password per l'autenticazione |
|
|
16
|
+
| `authentication` | `plain`, `login`, `cram_md5`, oppure `none`/vuoto per disabilitare l'auth |
|
|
17
|
+
| `enable_starttls_auto` | Impostare a `false`, `f`, `0` o `no` per disabilitare STARTTLS. Ignorato se `port` è `465` (SMTPS usa TLS implicito). |
|
|
18
|
+
| `from` | Indirizzo mittente |
|
|
19
|
+
|
|
20
|
+
**Nota porta 465 (SMTPS):** la connessione viene wrappata in SSL dal primo byte. Impostare `enable_starttls_auto` a `false` non è sufficiente: occorre usare la porta `465` affinché `SmtpConfig` abiliti automaticamente `tls: true`. Le due modalità (SMTPS e STARTTLS) sono mutuamente esclusive — mescolarle causa un `Net::ReadTimeout`.
|
|
21
|
+
|
|
22
|
+
### Usare SmtpDeliverable nei mailer
|
|
23
|
+
|
|
24
|
+
Includere il concern `SmtpDeliverable` in qualsiasi `ActionMailer::Base` per leggere le impostazioni SMTP da `ThecoreSettings` al momento dell'invio (nessun riavvio necessario quando le impostazioni cambiano):
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
class MyMailer < ApplicationMailer
|
|
28
|
+
include SmtpDeliverable
|
|
29
|
+
|
|
30
|
+
def my_email
|
|
31
|
+
mail(
|
|
32
|
+
from: smtp_setting(:from),
|
|
33
|
+
to: "recipient@example.com",
|
|
34
|
+
subject: "Hello"
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Il concern registra automaticamente un `after_action :configure_smtp_delivery` che sovrascrive il delivery method con le impostazioni lette da `ThecoreSettings`. Se `smtp.address` è vuoto, il mailer usa il delivery method configurato nell'ambiente Rails (es. `:test` o `:letter_opener` in sviluppo).
|
|
41
|
+
|
|
42
|
+
Il metodo helper `smtp_setting(key)` è disponibile in tutta la classe mailer.
|
|
43
|
+
|
|
44
|
+
### Usare SmtpConfig direttamente
|
|
45
|
+
|
|
46
|
+
`ThecoreBackendCommons::SmtpConfig` è un modulo Ruby puro utilizzabile anche al di fuori dei mailer (es. script, job, rake task):
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
opts = ThecoreBackendCommons::SmtpConfig.delivery_options
|
|
50
|
+
# => { address: "smtp.example.com", port: 465, tls: true, ... }
|
|
51
|
+
|
|
52
|
+
ThecoreBackendCommons::SmtpConfig.setting(:from)
|
|
53
|
+
# => "noreply@example.com"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Testare la configurazione SMTP
|
|
57
|
+
|
|
58
|
+
Il gem espone `ThecoreBackendCommons::SmtpTester` e un rake task, disponibili automaticamente in qualsiasi app che include questo gem.
|
|
59
|
+
|
|
60
|
+
**Da rake task (shell):**
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
rails thecore_backend_commons:smtp:test[destinatario@example.com]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**Da Rails console:**
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
ThecoreBackendCommons::SmtpTester.call("destinatario@example.com")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Se non si passa un indirizzo, in entrambi i casi viene usato `ThecoreSettings mytask.default_email`.
|
|
73
|
+
|
|
74
|
+
Lo tester stampa i parametri SMTP effettivamente usati (inclusi `tls` e `enable_starttls_auto`) prima di tentare la consegna. Restituisce `true`/`false` dal console e exit code `1` dal rake task in caso di errore.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Include in any ActionMailer subclass to wire up SMTP delivery from
|
|
4
|
+
# ThecoreSettings at send time (no application restart needed).
|
|
5
|
+
#
|
|
6
|
+
# class MyMailer < ApplicationMailer
|
|
7
|
+
# include SmtpDeliverable
|
|
8
|
+
# ...
|
|
9
|
+
# end
|
|
10
|
+
#
|
|
11
|
+
# The concern registers an after_action that overwrites the delivery method
|
|
12
|
+
# with settings read from ThecoreSettings (ns: :smtp). If smtp.address is
|
|
13
|
+
# blank the mailer falls back to whatever delivery_method is configured in
|
|
14
|
+
# the Rails environment (e.g. :test or :letter_opener in development).
|
|
15
|
+
module SmtpDeliverable
|
|
16
|
+
extend ActiveSupport::Concern
|
|
17
|
+
|
|
18
|
+
included do
|
|
19
|
+
after_action :configure_smtp_delivery
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def configure_smtp_delivery
|
|
25
|
+
opts = ThecoreBackendCommons::SmtpConfig.delivery_options
|
|
26
|
+
message.delivery_method(:smtp, opts) if opts[:address].present?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def smtp_setting(key)
|
|
30
|
+
ThecoreBackendCommons::SmtpConfig.setting(key)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
namespace :thecore_backend_commons do
|
|
2
|
+
namespace :smtp do
|
|
3
|
+
desc "Send a test email using ThecoreSettings SMTP config. " \
|
|
4
|
+
"Usage: rails thecore_backend_commons:smtp:test[recipient@example.com] " \
|
|
5
|
+
"(omit argument to use mytask.default_email)"
|
|
6
|
+
task :test, [:recipient] => :environment do |_t, args|
|
|
7
|
+
success = ThecoreBackendCommons::SmtpTester.call(args[:recipient])
|
|
8
|
+
exit 1 unless success
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThecoreBackendCommons
|
|
4
|
+
# Reads SMTP settings from ThecoreSettings (ns: :smtp) and builds the
|
|
5
|
+
# delivery_options hash for ActionMailer / Net::SMTP.
|
|
6
|
+
#
|
|
7
|
+
# Port 465 → SMTPS (implicit TLS from the first byte): tls: true.
|
|
8
|
+
# Port 587 → STARTTLS (plain connection upgraded after banner): enable_starttls_auto.
|
|
9
|
+
# The two modes are mutually exclusive; mixing them causes a ReadTimeout.
|
|
10
|
+
module SmtpConfig
|
|
11
|
+
FALSY_STRINGS = %w[false f 0 no].freeze
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def setting(key)
|
|
16
|
+
ThecoreSettings::Setting.find_by(ns: :smtp, key: key)&.raw
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def delivery_options
|
|
20
|
+
auth = setting(:authentication)
|
|
21
|
+
auth = nil if auth.blank? || auth == 'none'
|
|
22
|
+
port = setting(:port)&.to_i || 587
|
|
23
|
+
smtps = port == 465
|
|
24
|
+
opts = {
|
|
25
|
+
address: setting(:address),
|
|
26
|
+
port: port,
|
|
27
|
+
domain: setting(:domain),
|
|
28
|
+
user_name: setting(:user_name),
|
|
29
|
+
password: setting(:password),
|
|
30
|
+
authentication: auth&.to_sym,
|
|
31
|
+
tls: smtps,
|
|
32
|
+
enable_starttls_auto: !smtps && FALSY_STRINGS.exclude?(setting(:enable_starttls_auto).to_s.downcase.strip)
|
|
33
|
+
}
|
|
34
|
+
opts.transform_values! { |v| v.is_a?(String) ? v.strip.presence : v }
|
|
35
|
+
unless opts[:authentication]
|
|
36
|
+
opts.delete(:user_name)
|
|
37
|
+
opts.delete(:password)
|
|
38
|
+
end
|
|
39
|
+
opts.compact
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThecoreBackendCommons
|
|
4
|
+
# Sends a test email using the SMTP settings from ThecoreSettings.
|
|
5
|
+
# Usable from the Rails console or via the rake task.
|
|
6
|
+
#
|
|
7
|
+
# From rails console:
|
|
8
|
+
# ThecoreBackendCommons::SmtpTester.call("you@example.com")
|
|
9
|
+
#
|
|
10
|
+
# From the shell:
|
|
11
|
+
# rails thecore_backend_commons:smtp:test[you@example.com]
|
|
12
|
+
class SmtpTester
|
|
13
|
+
def self.call(recipient = nil)
|
|
14
|
+
new(recipient).call
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(recipient = nil)
|
|
18
|
+
@recipient = recipient.presence ||
|
|
19
|
+
ThecoreSettings::Setting.find_by(ns: :mytask, key: :default_email)&.raw
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call
|
|
23
|
+
validate!
|
|
24
|
+
print_settings
|
|
25
|
+
send_mail
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def validate!
|
|
31
|
+
raise ArgumentError, "No recipient given and mytask.default_email is not configured." if @recipient.blank?
|
|
32
|
+
raise ArgumentError, "smtp.address is not configured in ThecoreSettings." if opts[:address].blank?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def print_settings
|
|
36
|
+
puts "SMTP settings:"
|
|
37
|
+
puts " address: #{opts[:address]}"
|
|
38
|
+
puts " port: #{opts[:port]}"
|
|
39
|
+
puts " domain: #{opts[:domain].inspect}"
|
|
40
|
+
puts " user_name: #{opts[:user_name].inspect}"
|
|
41
|
+
puts " authentication: #{opts[:authentication].inspect}"
|
|
42
|
+
puts " tls: #{opts[:tls]}"
|
|
43
|
+
puts " enable_starttls_auto:#{opts[:enable_starttls_auto]}"
|
|
44
|
+
puts " from: #{SmtpConfig.setting(:from).inspect}"
|
|
45
|
+
puts ""
|
|
46
|
+
puts "Sending test email to: #{@recipient}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def send_mail
|
|
50
|
+
from_address = SmtpConfig.setting(:from).presence || "noreply@mytask.local"
|
|
51
|
+
delivery_opts = opts
|
|
52
|
+
|
|
53
|
+
mail = Mail.new do
|
|
54
|
+
from from_address
|
|
55
|
+
to delivery_opts[:address] # placeholder; overridden below
|
|
56
|
+
subject "[MyTask] SMTP test — #{Time.current.strftime('%Y-%m-%d %H:%M:%S %Z')}"
|
|
57
|
+
body "This is an automated SMTP connectivity test sent from MyTask.\n\n" \
|
|
58
|
+
"If you received this message, the SMTP configuration is working correctly.\n\n" \
|
|
59
|
+
"Settings used:\n" \
|
|
60
|
+
" address: #{delivery_opts[:address]}\n" \
|
|
61
|
+
" port: #{delivery_opts[:port]}\n" \
|
|
62
|
+
" tls: #{delivery_opts[:tls]}\n" \
|
|
63
|
+
" auth: #{delivery_opts[:authentication].inspect}"
|
|
64
|
+
end
|
|
65
|
+
mail.to = @recipient
|
|
66
|
+
mail.delivery_method(:smtp, delivery_opts)
|
|
67
|
+
mail.deliver!
|
|
68
|
+
puts "OK: email delivered successfully."
|
|
69
|
+
true
|
|
70
|
+
rescue StandardError => e
|
|
71
|
+
puts "ERROR: #{e.class}: #{e.message}"
|
|
72
|
+
false
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def opts
|
|
76
|
+
@opts ||= SmtpConfig.delivery_options
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -13,7 +13,8 @@ require "seed_dump"
|
|
|
13
13
|
|
|
14
14
|
require "thecore_backend_commons/version"
|
|
15
15
|
require "thecore_backend_commons/engine"
|
|
16
|
+
require "thecore_backend_commons/smtp_config"
|
|
17
|
+
require "thecore_backend_commons/smtp_tester"
|
|
16
18
|
|
|
17
19
|
module ThecoreBackendCommons
|
|
18
|
-
# Your code goes here...
|
|
19
20
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: thecore_backend_commons
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.2.
|
|
4
|
+
version: 3.2.11
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriele Tassoni
|
|
@@ -188,6 +188,7 @@ files:
|
|
|
188
188
|
- README.md
|
|
189
189
|
- Rakefile
|
|
190
190
|
- app/channels/activity_log_channel.rb
|
|
191
|
+
- app/mailers/concerns/smtp_deliverable.rb
|
|
191
192
|
- config/initializers/abilities.rb
|
|
192
193
|
- config/initializers/add_to_db_migrations.rb
|
|
193
194
|
- config/initializers/after_initialize.rb
|
|
@@ -206,6 +207,8 @@ files:
|
|
|
206
207
|
- lib/tasks/thecore_backend_commons_tasks.rake
|
|
207
208
|
- lib/thecore_backend_commons.rb
|
|
208
209
|
- lib/thecore_backend_commons/engine.rb
|
|
210
|
+
- lib/thecore_backend_commons/smtp_config.rb
|
|
211
|
+
- lib/thecore_backend_commons/smtp_tester.rb
|
|
209
212
|
- lib/thecore_backend_commons/version.rb
|
|
210
213
|
homepage: https://github.com/gabrieletassoni/thecore_backend_commons
|
|
211
214
|
licenses: []
|