postmail_ruby 0.1.0
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 +7 -0
- data/README.md +95 -0
- data/lib/postmail_ruby/configuration.rb +97 -0
- data/lib/postmail_ruby/delivery_method/http.rb +127 -0
- data/lib/postmail_ruby/delivery_method/smtp.rb +38 -0
- data/lib/postmail_ruby/railtie.rb +43 -0
- data/lib/postmail_ruby/version.rb +6 -0
- data/lib/postmail_ruby.rb +63 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a30c976e7a59b089ec5ed4ce4e7efdf8b734433588e021d91f58f1b61c5e6588
|
4
|
+
data.tar.gz: b8e359dc9348ee1ee54af9a02cb692b68f1d57badcd131e646deab645e4344c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad6a4510ea30befbd59ab3704b46a3eada3becf11b34dc46ab3768a86f0e82b598b754c23d20f68c87993fab82a3c3a21b6772613f3ecdf89cd47bb4c273b199
|
7
|
+
data.tar.gz: 036a7cd3fa7c8e766a2ecc589de70e2e06839abce07057850ca6684ece45da21c4afbf9c2d7fc5ef5b5ca17cda21e5d5258043ffd2d35b59769c01553f0390f0
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Postmail
|
2
|
+
|
3
|
+
Postmail est une gem Ruby qui fournit un mécanisme flexible d’envoi d’e‑mails pour Ruby on Rails ou tout projet utilisant Action Mailer. La gem propose deux méthodes d’expédition : soit par **SMTP**, soit par une **API HTTP**. Tous les paramètres de connexion et le choix de la méthode d’envoi se pilotent via des variables d’environnement afin de simplifier la configuration lors du déploiement.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Dans votre `Gemfile`, ajoutez :
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "postmail", path: "path/to/postmail"
|
11
|
+
```
|
12
|
+
|
13
|
+
Puis exécutez `bundle install` pour installer la gem.
|
14
|
+
|
15
|
+
## Utilisation dans Rails
|
16
|
+
|
17
|
+
Postmail enregistre deux méthodes de livraison personnalisées pour Action Mailer : `:postmail_smtp` et `:postmail_api`. La gem est livrée avec un **Railtie** qui détecte Rails et configure automatiquement Action Mailer en fonction des variables d’environnement ci‑dessous. En pratique, il suffit de définir vos variables d’environnement et de vous assurer que la gem est chargée.
|
18
|
+
|
19
|
+
Si vous souhaitez configurer explicitement la gem (par exemple hors de Rails), vous pouvez appeler :
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require "postmail"
|
23
|
+
Postmail.configure
|
24
|
+
```
|
25
|
+
|
26
|
+
Cette méthode lit les variables d’environnement, enregistre les méthodes de livraison et définit la méthode d’envoi par défaut (`delivery_method`) sur Action Mailer.
|
27
|
+
|
28
|
+
### Variables d’environnement
|
29
|
+
|
30
|
+
Toutes les options de configuration se font via des variables d’environnement, ce qui permet de modifier le comportement sans changer le code. Voici les variables reconnues :
|
31
|
+
|
32
|
+
| Variable | Description | Valeur par défaut |
|
33
|
+
|---------|-------------|------------------|
|
34
|
+
| `POSTMAIL_DELIVERY_METHOD` | Choix de la méthode d’envoi : `smtp` (envoi via serveur SMTP) ou `api` (envoi via appel HTTP). | `smtp` |
|
35
|
+
| `POSTMAIL_DISABLE_RAILS_SMTP` | Lorsque défini à `true`, Postmail désactive les paramètres SMTP par défaut de Rails (`ActionMailer::Base.smtp_settings`) pour éviter des retours en arrière accidentels. Utiliser cette option est recommandé si vous envoyez exclusivement via l’API. | `false` |
|
36
|
+
| `POSTMAIL_API_ENDPOINT` | URL complète de l’endpoint HTTP pour l’envoi par API (par exemple `https://postal.exanora.com/api/v1/send/message`). Obligatoire si `POSTMAIL_DELIVERY_METHOD=api`. | – |
|
37
|
+
| `POSTMAIL_API_KEY` | Clé d’API utilisée pour authentifier les requêtes HTTP (envoyée dans l’en‑tête `X-Server-API-Key`). | – |
|
38
|
+
| `POSTMAIL_API_TIMEOUT` | Temps d’attente maximum en secondes lors de l’appel HTTP. | `10` |
|
39
|
+
| `POSTMAIL_SMTP_HOST` | Nom d’hôte du serveur SMTP (ex : `smtp.exemple.com`). Obligatoire si `POSTMAIL_DELIVERY_METHOD=smtp`. | – |
|
40
|
+
| `POSTMAIL_SMTP_PORT` | Port du serveur SMTP. | `587` |
|
41
|
+
| `POSTMAIL_SMTP_USERNAME` | Nom d’utilisateur pour l’authentification SMTP. Laisser vide pour une connexion sans authentification. | `nil` |
|
42
|
+
| `POSTMAIL_SMTP_PASSWORD` | Mot de passe pour l’authentification SMTP. | `nil` |
|
43
|
+
| `POSTMAIL_SMTP_AUTHENTICATION` | Type d’authentification SMTP : `plain`, `login` ou `cram_md5`. | `plain` |
|
44
|
+
| `POSTMAIL_SMTP_ENABLE_STARTTLS_AUTO` | Active STARTTLS. Mettre `false` pour désactiver. | `true` |
|
45
|
+
| `POSTMAIL_SMTP_SSL` | Lorsque défini à `true`, Postmail établit une connexion SSL/TLS implicite (comme un port 465). | `false` |
|
46
|
+
| `POSTMAIL_SMTP_OPEN_TIMEOUT` | Durée maximale (en secondes) pour établir la connexion SMTP. | `30` |
|
47
|
+
| `POSTMAIL_SMTP_READ_TIMEOUT` | Durée maximale (en secondes) pour la lecture des réponses SMTP. | `30` |
|
48
|
+
|
49
|
+
### Désactiver le SMTP par défaut de Rails
|
50
|
+
|
51
|
+
Rails utilise un mécanisme SMTP par défaut si aucune méthode d’envoi n’est explicitement configurée. Pour éviter toute utilisation accidentelle du SMTP intégré lorsque vous utilisez Postmail, définissez :
|
52
|
+
|
53
|
+
```sh
|
54
|
+
POSTMAIL_DISABLE_RAILS_SMTP=true
|
55
|
+
```
|
56
|
+
|
57
|
+
Lorsque cette variable est à `true`, Postmail réinitialise `ActionMailer::Base.smtp_settings` à un hash vide et définit `ActionMailer::Base.delivery_method` sur `:postmail_api` ou `:postmail_smtp` en fonction de `POSTMAIL_DELIVERY_METHOD`.
|
58
|
+
|
59
|
+
### Exemple de configuration
|
60
|
+
|
61
|
+
Pour envoyer via l’API HTTP :
|
62
|
+
|
63
|
+
```sh
|
64
|
+
POSTMAIL_DELIVERY_METHOD=api
|
65
|
+
POSTMAIL_API_ENDPOINT=https://postal.exanora.com/api/v1/send/message
|
66
|
+
POSTMAIL_API_KEY=your_api_key
|
67
|
+
POSTMAIL_DISABLE_RAILS_SMTP=true
|
68
|
+
```
|
69
|
+
|
70
|
+
Pour envoyer via SMTP :
|
71
|
+
|
72
|
+
```sh
|
73
|
+
POSTMAIL_DELIVERY_METHOD=smtp
|
74
|
+
POSTMAIL_SMTP_HOST=smtp.exemple.com
|
75
|
+
POSTMAIL_SMTP_PORT=2587
|
76
|
+
POSTMAIL_SMTP_USERNAME=user
|
77
|
+
POSTMAIL_SMTP_PASSWORD=secret
|
78
|
+
POSTMAIL_SMTP_AUTHENTICATION=login
|
79
|
+
POSTMAIL_SMTP_ENABLE_STARTTLS_AUTO=true
|
80
|
+
POSTMAIL_DISABLE_RAILS_SMTP=false
|
81
|
+
```
|
82
|
+
|
83
|
+
## Fonctionnement
|
84
|
+
|
85
|
+
Lorsque la méthode d’envoi est `api`, Postmail sérialise votre message et l’envoie en `POST` vers l’endpoint HTTP. Le corps de la requête est JSON et inclut les adresses, le sujet, les corps texte/HTML et les pièces jointes (encodées en Base64). La clé API est transmise via l’en‑tête `X-Server-API-Key`.
|
86
|
+
|
87
|
+
Lorsque la méthode d’envoi est `smtp`, Postmail utilise les paramètres SMTP renseignés pour établir la connexion avec le serveur de messagerie via la gem `mail`. Les options SSL/TLS, timeouts et authentification sont héritées des variables d’environnement.
|
88
|
+
|
89
|
+
## Contribuer
|
90
|
+
|
91
|
+
Les demandes d’amélioration et les rapports de bogue sont les bienvenus ! Ouvrez une issue ou une pull request sur GitHub à l’adresse <https://github.com/votrecompte/postmail>.
|
92
|
+
|
93
|
+
## Licence
|
94
|
+
|
95
|
+
Ce projet est distribué sous licence MIT. Voir le fichier `LICENSE` pour plus de détails.
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module PostmailRuby
|
6
|
+
# Configuration holds all settings loaded from environment variables or via configuration block.
|
7
|
+
# It provides helpers to build SMTP settings and determine if default Rails SMTP
|
8
|
+
# configuration should be disabled.
|
9
|
+
class Configuration
|
10
|
+
# Delivery method to use (:smtp or :api)
|
11
|
+
attr_accessor :delivery_method
|
12
|
+
# API endpoint for HTTP delivery
|
13
|
+
attr_accessor :api_endpoint
|
14
|
+
# API key for HTTP delivery (used in X-Server-API-Key header)
|
15
|
+
attr_accessor :api_key
|
16
|
+
# SMTP host
|
17
|
+
attr_accessor :smtp_host
|
18
|
+
# SMTP port (integer)
|
19
|
+
attr_accessor :smtp_port
|
20
|
+
# SMTP username
|
21
|
+
attr_accessor :smtp_username
|
22
|
+
# SMTP password
|
23
|
+
attr_accessor :smtp_password
|
24
|
+
# SMTP authentication type (:plain, :login, etc.)
|
25
|
+
attr_accessor :smtp_authentication
|
26
|
+
# Enable STARTTLS for SMTP (boolean)
|
27
|
+
attr_accessor :smtp_enable_starttls_auto
|
28
|
+
# Use SSL/TLS implicit connection for SMTP (boolean)
|
29
|
+
attr_accessor :smtp_ssl
|
30
|
+
# Domain used for SMTP
|
31
|
+
attr_accessor :smtp_domain
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
# set defaults from environment variables
|
35
|
+
@delivery_method = (ENV['POSTMAIL_DELIVERY_METHOD'] || 'smtp').downcase.to_sym
|
36
|
+
@disable_default_smtp = truthy?(ENV['POSTMAIL_DISABLE_RAILS_SMTP'])
|
37
|
+
|
38
|
+
@api_endpoint = ENV['POSTMAIL_API_ENDPOINT'] || 'https://postal.exanora.com/api/v1/send/message'
|
39
|
+
@api_key = ENV['POSTMAIL_API_KEY']
|
40
|
+
|
41
|
+
@smtp_host = ENV['POSTMAIL_SMTP_HOST'] || 'localhost'
|
42
|
+
@smtp_port = integer_or_nil(ENV['POSTMAIL_SMTP_PORT']) || 25
|
43
|
+
@smtp_username = ENV['POSTMAIL_SMTP_USERNAME']
|
44
|
+
@smtp_password = ENV['POSTMAIL_SMTP_PASSWORD']
|
45
|
+
@smtp_authentication = (ENV['POSTMAIL_SMTP_AUTH'] || 'login').downcase.to_sym
|
46
|
+
@smtp_enable_starttls_auto = truthy?(ENV['POSTMAIL_SMTP_ENABLE_STARTTLS_AUTO'])
|
47
|
+
# If not explicitly set, enable_starttls_auto defaults to true unless SSL is enabled
|
48
|
+
@smtp_enable_starttls_auto = !truthy?(ENV['POSTMAIL_SMTP_SSL']) if @smtp_enable_starttls_auto.nil?
|
49
|
+
@smtp_ssl = truthy?(ENV['POSTMAIL_SMTP_SSL'])
|
50
|
+
@smtp_domain = ENV['POSTMAIL_SMTP_DOMAIN']
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns true if the default Rails SMTP configuration should be disabled.
|
54
|
+
# When this is true, Postmail will clear `config.action_mailer.smtp_settings`.
|
55
|
+
def disable_default_smtp?
|
56
|
+
@disable_default_smtp
|
57
|
+
end
|
58
|
+
|
59
|
+
# Compose SMTP settings hash suitable for Mail::SMTP or ActionMailer.
|
60
|
+
def smtp_settings
|
61
|
+
settings = {
|
62
|
+
address: smtp_host, port: smtp_port,
|
63
|
+
user_name: smtp_username, password: smtp_password,
|
64
|
+
authentication: smtp_authentication,
|
65
|
+
enable_starttls_auto: smtp_enable_starttls_auto, ssl: smtp_ssl
|
66
|
+
}
|
67
|
+
settings[:domain] = smtp_domain if smtp_domain
|
68
|
+
# Remove nil values to avoid overriding defaults
|
69
|
+
settings.compact
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# Convert string to boolean if present, returns nil if value not provided.
|
75
|
+
def truthy?(value)
|
76
|
+
return nil if value.nil? || value.empty?
|
77
|
+
|
78
|
+
case value.downcase
|
79
|
+
when 'true', '1', 'yes', 'y'
|
80
|
+
true
|
81
|
+
when 'false', '0', 'no', 'n'
|
82
|
+
false
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Convert string to integer, returns nil if value is blank or not numeric.
|
87
|
+
def integer_or_nil(value)
|
88
|
+
return nil if value.nil? || value.to_s.strip.empty?
|
89
|
+
|
90
|
+
begin
|
91
|
+
Integer(value)
|
92
|
+
rescue StandardError
|
93
|
+
nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
require 'json'
|
6
|
+
require 'base64'
|
7
|
+
|
8
|
+
module PostmailRuby
|
9
|
+
module DeliveryMethod
|
10
|
+
# Implements a delivery method for Action Mailer that sends
|
11
|
+
# messages via the Postal HTTP API. Instead of speaking SMTP
|
12
|
+
# directly, this class packages the email into a JSON payload
|
13
|
+
# and posts it to the configured Postal endpoint. An API key
|
14
|
+
# must be provided via configuration for authentication.
|
15
|
+
class HTTP
|
16
|
+
attr_reader :settings
|
17
|
+
|
18
|
+
# Initialize the HTTP delivery method. Accepts a hash of
|
19
|
+
# options that may override configuration defaults. Options
|
20
|
+
# are stored but not used directly; configuration is read
|
21
|
+
# from Postmail.config for each delivery to ensure the most
|
22
|
+
# current environment variables are respected.
|
23
|
+
#
|
24
|
+
# @param [Hash] options delivery options (currently unused)
|
25
|
+
def initialize(options = {})
|
26
|
+
@settings = options
|
27
|
+
end
|
28
|
+
|
29
|
+
# Deliver a Mail::Message via the Postal API. Builds a JSON
|
30
|
+
# payload including recipients, subject, plain and HTML parts
|
31
|
+
# and attachments. Sends the payload as a POST request with
|
32
|
+
# the API key in the X-Server-API-Key header. Raises an
|
33
|
+
# exception if the request returns a non-success response.
|
34
|
+
#
|
35
|
+
# @param [Mail::Message] mail the message to send
|
36
|
+
def deliver!(mail)
|
37
|
+
config = Postmail.config
|
38
|
+
uri = URI.parse(config.api_endpoint)
|
39
|
+
|
40
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
41
|
+
http.use_ssl = (uri.scheme == 'https')
|
42
|
+
http.read_timeout = 15
|
43
|
+
http.open_timeout = 5
|
44
|
+
|
45
|
+
request = Net::HTTP::Post.new(uri.request_uri, {
|
46
|
+
'Content-Type' => 'application/json',
|
47
|
+
'X-Server-API-Key' => config.api_key.to_s
|
48
|
+
})
|
49
|
+
request.body = build_payload(mail, config).to_json
|
50
|
+
|
51
|
+
response = http.request(request)
|
52
|
+
unless response.is_a?(Net::HTTPSuccess)
|
53
|
+
raise "Postal API responded with status #{response.code}: #{response.body}"
|
54
|
+
end
|
55
|
+
|
56
|
+
response
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# Build a hash representing the JSON payload expected by
|
62
|
+
# Postal's API. Extracts addresses, subject, plain and HTML
|
63
|
+
# bodies and encodes attachments as base64. Null values are
|
64
|
+
# omitted from the final hash for brevity.
|
65
|
+
#
|
66
|
+
# @param [Mail::Message] mail
|
67
|
+
# @param [Postmail::Configuration] config
|
68
|
+
# @return [Hash]
|
69
|
+
def build_payload(mail, _config)
|
70
|
+
{
|
71
|
+
from: extract_sender(mail),
|
72
|
+
to: extract_addresses(mail.to),
|
73
|
+
cc: extract_addresses(mail.cc),
|
74
|
+
bcc: extract_addresses(mail.bcc),
|
75
|
+
subject: mail.subject.to_s,
|
76
|
+
plain_body: extract_part(mail, 'text/plain'),
|
77
|
+
html_body: extract_part(mail, 'text/html'),
|
78
|
+
attachments: build_attachments(mail)
|
79
|
+
}.compact
|
80
|
+
end
|
81
|
+
|
82
|
+
# Extract the sender address. The Mail object may store
|
83
|
+
# from address as an array; we use the first entry. Returns
|
84
|
+
# nil if no sender is specified.
|
85
|
+
def extract_sender(mail)
|
86
|
+
Array(mail.from).first
|
87
|
+
end
|
88
|
+
|
89
|
+
# Convert an array of addresses to a comma-separated string.
|
90
|
+
# Returns nil if the array is blank.
|
91
|
+
def extract_addresses(value)
|
92
|
+
return nil if value.nil? || value.empty?
|
93
|
+
|
94
|
+
Array(value).join(',')
|
95
|
+
end
|
96
|
+
|
97
|
+
# Extract a specific MIME part "../../postmail/delivery_method""."from the message. For
|
98
|
+
# multipart messages, the first matching part "../../postmail/delivery_method""."is returned.
|
99
|
+
# For non-multipart, the body is returned if the MIME type
|
100
|
+
# matches. Returns nil if no matching part "../../postmail/delivery_method""."exists.
|
101
|
+
def extract_part(mail, mime_type)
|
102
|
+
if mail.multipart?
|
103
|
+
mail.parts.find { |p| p.mime_type&.start_with?(mime_type) }
|
104
|
+
part "../../postmail_ruby/delivery_method#{'.'.decoded}"
|
105
|
+
else
|
106
|
+
mail.mime_type&.start_with?(mime_type) ? mail.body.decoded : nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Build an array of attachment hashes. Each attachment
|
111
|
+
# includes name, content_type and base64 encoded data. If
|
112
|
+
# there are no attachments the method returns nil to omit
|
113
|
+
# the key from the payload.
|
114
|
+
def build_attachments(mail)
|
115
|
+
return nil if mail.attachments.empty?
|
116
|
+
|
117
|
+
mail.attachments.map do |att|
|
118
|
+
{
|
119
|
+
name: att.filename.to_s,
|
120
|
+
content_type: att.mime_type.to_s,
|
121
|
+
data: Base64.strict_encode64(att.body.decoded)
|
122
|
+
}
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mail'
|
4
|
+
|
5
|
+
module PostmailRuby
|
6
|
+
module DeliveryMethod
|
7
|
+
# Provides a custom SMTP delivery method that reads its
|
8
|
+
# configuration from Postmail::Configuration. This class is
|
9
|
+
# essentially a thin wrapper around Mail::SMTP, passing in
|
10
|
+
# settings derived from environment variables. It allows
|
11
|
+
# Action Mailer to use Postmail configuration seamlessly.
|
12
|
+
class SMTP
|
13
|
+
attr_reader :settings
|
14
|
+
|
15
|
+
# Initialize the SMTP delivery method. Accepts an options
|
16
|
+
# hash which can override the configuration. Options are
|
17
|
+
# merged with configuration on each delivery.
|
18
|
+
#
|
19
|
+
# @param [Hash] options options overriding configuration
|
20
|
+
def initialize(options = {})
|
21
|
+
@settings = options
|
22
|
+
end
|
23
|
+
|
24
|
+
# Delivers a Mail::Message via SMTP. Merges any options
|
25
|
+
# provided during initialization with the configuration's
|
26
|
+
# smtp_settings. Uses Mail::SMTP#deliver! to send the
|
27
|
+
# message.
|
28
|
+
#
|
29
|
+
# @param [Mail::Message] mail the message to send
|
30
|
+
def deliver!(mail)
|
31
|
+
config_settings = Postmail.config.smtp_settings
|
32
|
+
smtp_settings = config_settings.merge(settings)
|
33
|
+
smtp = ::Mail::SMTP.new(smtp_settings)
|
34
|
+
smtp.deliver!(mail)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/railtie'
|
4
|
+
|
5
|
+
module PostmailRuby
|
6
|
+
# Railtie for integrating Postmail with Rails. This railtie
|
7
|
+
# registers custom delivery methods with Action Mailer and
|
8
|
+
# configures Action Mailer based on Postmail configuration. It
|
9
|
+
# also offers an option to disable the default SMTP settings
|
10
|
+
# configured by Rails, allowing Postmail to be the sole mail
|
11
|
+
# provider in a Rails application.
|
12
|
+
class Railtie < ::Rails::Railtie
|
13
|
+
initializer 'postmail_ruby.initialize' do
|
14
|
+
ActiveSupport.on_load(:action_mailer) do
|
15
|
+
# Register our delivery methods
|
16
|
+
ActionMailer::Base.add_delivery_method :postmail_smtp, Postmail::DeliveryMethod::SMTP
|
17
|
+
ActionMailer::Base.add_delivery_method :postmail_api, Postmail::DeliveryMethod::HTTP
|
18
|
+
|
19
|
+
# Determine which delivery method to use based on configuration
|
20
|
+
delivery_method = Postmail.config.delivery_method
|
21
|
+
ActionMailer::Base.delivery_method = case delivery_method
|
22
|
+
when :api
|
23
|
+
:postmail_api
|
24
|
+
else
|
25
|
+
:postmail_smtp
|
26
|
+
end
|
27
|
+
|
28
|
+
# If using SMTP, apply our SMTP settings and optionally
|
29
|
+
# clear any default Rails SMTP settings. The
|
30
|
+
# disable_default_smtp? flag removes Rails SMTP settings
|
31
|
+
# before applying Postmail settings.
|
32
|
+
if delivery_method == :smtp
|
33
|
+
if PostmailRuby.config.disable_default_smtp?
|
34
|
+
# Clear any pre-existing SMTP settings so they do not
|
35
|
+
# interfere with Postmail's configuration
|
36
|
+
ActionMailer::Base.smtp_settings = {}
|
37
|
+
end
|
38
|
+
ActionMailer::Base.smtp_settings = PostmailRuby.config.smtp_settings
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'postmail_ruby/version'
|
4
|
+
require_relative 'postmail_ruby/configuration'
|
5
|
+
|
6
|
+
# Main entry point for the Postmail gem. This module exposes
|
7
|
+
# configuration and helper methods to integrate the gem into a
|
8
|
+
# Ruby on Rails application. When loaded in a Rails environment,
|
9
|
+
# a Railtie is required automatically to register custom delivery
|
10
|
+
# methods with Action Mailer.
|
11
|
+
module PostmailRuby
|
12
|
+
class << self
|
13
|
+
# Accessor for the configuration instance. When first called
|
14
|
+
# a new Configuration object is created and memoized. This
|
15
|
+
# object reads environment variables to determine how Postmail
|
16
|
+
# should behave (API vs SMTP, credentials, endpoints, etc.).
|
17
|
+
#
|
18
|
+
# @return [Postmail::Configuration]
|
19
|
+
attr_accessor :configuration
|
20
|
+
|
21
|
+
# Yields the configuration instance to a block so that callers
|
22
|
+
# can override defaults at runtime. If no block is given the
|
23
|
+
# current configuration is returned. This is the primary API
|
24
|
+
# used to change settings in an initializer.
|
25
|
+
#
|
26
|
+
# @example Override the API endpoint
|
27
|
+
# Postmail.configure do |config|
|
28
|
+
# config.api_endpoint = "https://my-postal.example/api/v1/send/message"
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# @yieldparam [Postmail::Configuration] configuration
|
32
|
+
# @return [Postmail::Configuration]
|
33
|
+
def configure
|
34
|
+
self.configuration ||= Configuration.new
|
35
|
+
return configuration unless block_given?
|
36
|
+
|
37
|
+
yield(configuration)
|
38
|
+
configuration
|
39
|
+
end
|
40
|
+
|
41
|
+
# Ensures that a configuration instance exists. If the
|
42
|
+
# configuration has not been initialized then a new one is
|
43
|
+
# created. This method should be used internally when a
|
44
|
+
# configuration is required.
|
45
|
+
#
|
46
|
+
# @return [Postmail::Configuration]
|
47
|
+
def config
|
48
|
+
self.configuration ||= Configuration.new
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Require delivery method classes. These require statements are
|
54
|
+
# placed outside of the Postmail module definition so that they
|
55
|
+
# are loaded when the gem is required. They rely on Postmail
|
56
|
+
# configuration, so configuration must be loaded first.
|
57
|
+
require_relative 'postmail_ruby/delivery_method/http'
|
58
|
+
require_relative 'postmail_ruby/delivery_method/smtp'
|
59
|
+
|
60
|
+
# Load the Railtie only if Rails is defined. The Railtie is
|
61
|
+
# responsible for registering delivery methods and setting up
|
62
|
+
# Action Mailer configuration based on environment variables.
|
63
|
+
require_relative 'postmail_ruby/railtie' if defined?(Rails)
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postmail_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- DAKIN Judicaël
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-09-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mail
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.7'
|
27
|
+
description: Postmail is a simple gem that adds custom Action Mailer delivery methods
|
28
|
+
allowing you to send email via either SMTP or an HTTP API. The delivery method and
|
29
|
+
all settings are configurable via environment variables so it can be easily switched
|
30
|
+
at runtime without code changes.
|
31
|
+
email:
|
32
|
+
- d.j.bidossessi@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- README.md
|
38
|
+
- lib/postmail_ruby.rb
|
39
|
+
- lib/postmail_ruby/configuration.rb
|
40
|
+
- lib/postmail_ruby/delivery_method/http.rb
|
41
|
+
- lib/postmail_ruby/delivery_method/smtp.rb
|
42
|
+
- lib/postmail_ruby/railtie.rb
|
43
|
+
- lib/postmail_ruby/version.rb
|
44
|
+
homepage: https://postmail.exanora.com
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '2.5'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.3.27
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Delivery methods for sending mail via SMTP or HTTP using environment variables
|
67
|
+
test_files: []
|