thecore_backend_commons 3.2.9 → 3.2.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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8c3ab89916ec1ac0d08617605d32e5ad30b2bc8232df838a29a9b75966f0d653
|
|
4
|
+
data.tar.gz: 89ea95480895118d1641d964fae91339e3714cb126e3d0ae3343b83e9c18c289
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5e11ad2f433fd264b679cd7f77ad77363f75d0c5f4245027ab4b39e85190f45bb78af8624a5621334833a73615846084c5478375f0d7dae4622319c6225ddf9e
|
|
7
|
+
data.tar.gz: 7fa46bd7592d9d85db5778db94d2130d7c70771aea9118f089dd05822646e9602515b1d1ede8080280be79cf168939fc594a1daf60cec04ff370c501c38829f0
|
data/README.md
CHANGED
|
@@ -1 +1,66 @@
|
|
|
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
|
+
Dall'applicazione Rails che include questo gem, eseguire:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
bundle exec rails runner script/test_smtp.rb destinatario@example.com
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Se non si passa un indirizzo, l'email viene inviata all'indirizzo configurato in `ThecoreSettings mytask.default_email`.
|
|
65
|
+
|
|
66
|
+
Lo script stampa i parametri SMTP effettivamente usati (inclusi `tls` e `enable_starttls_auto`) prima di tentare la consegna, e restituisce exit code `1` in caso di errore con il messaggio dell'eccezione.
|
|
@@ -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
|
|
@@ -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
|
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.10
|
|
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,7 @@ 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
|
|
209
211
|
- lib/thecore_backend_commons/version.rb
|
|
210
212
|
homepage: https://github.com/gabrieletassoni/thecore_backend_commons
|
|
211
213
|
licenses: []
|