docbrown/app/mailers/application_mailer.rb
Mac Siri e38196df6f
Create Settings::SMTP (#13943)
* Fix typo

* Add SMTP configs to Settings::General

* Expand Settings's show page

* Expand Settings::General's constants

* Move smtp_enabled? logic to ApplicationMailer

* Apply suggestions from code review

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

* Add missing descriptions and placeholders

* Remove production guard clause

* Change delivery_method to a callback

* Create Settings::SMTP

* Run migration

* Move constants

* Remove SMTP from Settings::General

* Create SMTPSettingsController

* Add back guard clause

* Change which perform_deliveries configuration to use from

* Update config/environments/production.rb

* Rename migration to singular

* Run migration again

* Fix name

* Alphabetize and add validation for authentication

* Move settings and enabled? logic to Settings::SMTP

* Change after_action to before_action

* Fix broken spec

* Fix broken spec

* Create SMTP specs

* Provide default for port

* Create spec for ApplicationMailer

* Fix broken spec

* Move smtp_enabled?

* Search and replace

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>
2021-06-14 10:29:43 -04:00

51 lines
1.7 KiB
Ruby

class ApplicationMailer < ActionMailer::Base
layout "mailer"
# the order of importing the helpers here is important
# we want the application helpers to override the Rails route helpers should there be a name conflict
# an example is the user_url
helper Rails.application.routes.url_helpers
helper ApplicationHelper
helper AuthenticationHelper
before_action :use_custom_host
before_action :set_delivery_options
before_action :set_perform_deliveries
default(
from: -> { email_from },
template_path: ->(mailer) { "mailers/#{mailer.class.name.underscore}" },
reply_to: -> { ForemInstance.email },
)
def email_from(topic = "")
community_name = if topic.present?
"#{Settings::Community.community_name} #{topic}"
else
Settings::Community.community_name
end
"#{community_name} <#{ForemInstance.email}>"
end
def generate_unsubscribe_token(id, email_type)
Rails.application.message_verifier(:unsubscribe).generate({
user_id: id,
email_type: email_type.to_sym,
expires_at: 31.days.from_now
})
end
def use_custom_host
ActionMailer::Base.default_url_options[:host] = Settings::General.app_domain
end
def set_perform_deliveries
self.perform_deliveries = ForemInstance.smtp_enabled?
end
protected
def set_delivery_options
self.smtp_settings = Settings::SMTP.settings
end
end