fix(mailer): apply delivery_method directly to ActionMailer::Base
The ActionMailer railtie's on_load hook applies config.action_mailer.* settings to ActionMailer::Base when ActionMailer::Base is first referenced. In our initializer load order, devise.rb runs before mailer.rb (alphabetical) and triggers ActionMailer::Base loading via Devise::Mailer. By the time mailer.rb runs, the on_load hook has already fired with the default :smtp delivery method, and subsequent writes to config.action_mailer.delivery_method are silently ignored. Result: production transactional emails (Devise confirmation, password reset, member invitations) were being sent via Mail's default :smtp delivery method to localhost:25, raising Errno::ECONNREFUSED on every attempt — silently in deliver_later. LeadMail was never actually used despite LEADMAIL_API_TOKEN being set. Fix is to set ActionMailer::Base.delivery_method (and the matching *_settings) directly in addition to config.action_mailer.*. Same pattern applied to the SMTP and sendmail fallback branches for consistency. Verified locally: rails runner now reports delivery_method=:leadmail when LEADMAIL_API_TOKEN is set.
This commit is contained in:
@@ -32,17 +32,32 @@ Rails.application.configure do
|
||||
# Use LeadMail API if configured
|
||||
if ENV['LEADMAIL_API_TOKEN'].present?
|
||||
ActionMailer::Base.add_delivery_method :leadmail, LeadmailDelivery
|
||||
config.action_mailer.delivery_method = :leadmail
|
||||
config.action_mailer.leadmail_settings = {
|
||||
leadmail_settings = {
|
||||
api_url: ENV.fetch('LEADMAIL_API_URL', 'https://mail.leadmagnet.dev/api/v1'),
|
||||
token: ENV['LEADMAIL_API_TOKEN']
|
||||
}
|
||||
config.action_mailer.delivery_method = :leadmail
|
||||
config.action_mailer.leadmail_settings = leadmail_settings
|
||||
# === LeadChat: leadmail-delivery-fix (start) ===
|
||||
# Devise (loaded earlier alphabetically) references ActionMailer::Base before
|
||||
# this initializer runs, so the ActionMailer railtie's on_load hook has
|
||||
# already finalised delivery_method to the default :smtp. Setting it via
|
||||
# config.action_mailer.* alone is silently ignored. Apply directly to
|
||||
# ActionMailer::Base so the config takes effect.
|
||||
ActionMailer::Base.delivery_method = :leadmail
|
||||
ActionMailer::Base.leadmail_settings = leadmail_settings
|
||||
# === LeadChat: leadmail-delivery-fix (end) ===
|
||||
elsif !Rails.env.test?
|
||||
config.action_mailer.delivery_method = :smtp
|
||||
config.action_mailer.smtp_settings = smtp_settings
|
||||
ActionMailer::Base.delivery_method = :smtp
|
||||
ActionMailer::Base.smtp_settings = smtp_settings
|
||||
|
||||
# Use sendmail if using postfix for email
|
||||
config.action_mailer.delivery_method = :sendmail if ENV['SMTP_ADDRESS'].blank?
|
||||
if ENV['SMTP_ADDRESS'].blank?
|
||||
config.action_mailer.delivery_method = :sendmail
|
||||
ActionMailer::Base.delivery_method = :sendmail
|
||||
end
|
||||
end
|
||||
|
||||
# You can use letter opener for your local development by setting the environment variable
|
||||
|
||||
Reference in New Issue
Block a user