2018-08-15 17:45:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-27 16:08:47 -05:00
|
|
|
class ApplicationMailer < ActionMailer::Base
|
2017-05-25 11:22:45 -04:00
|
|
|
around_action :render_with_default_locale
|
|
|
|
|
2016-11-06 14:27:51 -05:00
|
|
|
helper ApplicationHelper
|
2017-04-03 05:55:15 -04:00
|
|
|
helper MarkupHelper
|
2015-08-19 15:35:08 -04:00
|
|
|
|
|
|
|
attr_accessor :current_user
|
2022-02-28 13:14:03 -05:00
|
|
|
|
2018-02-02 13:39:55 -05:00
|
|
|
helper_method :current_user, :can?
|
2015-08-19 15:35:08 -04:00
|
|
|
|
2017-03-31 11:11:28 -04:00
|
|
|
default from: proc { default_sender_address.format }
|
|
|
|
default reply_to: proc { default_reply_to_address.format }
|
2015-08-19 15:35:08 -04:00
|
|
|
|
|
|
|
def can?
|
2016-08-08 14:55:13 -04:00
|
|
|
Ability.allowed?(current_user, action, subject)
|
2015-08-19 15:35:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-25 11:22:45 -04:00
|
|
|
def render_with_default_locale(&block)
|
|
|
|
Gitlab::I18n.with_default_locale(&block)
|
|
|
|
end
|
|
|
|
|
2015-08-19 15:35:08 -04:00
|
|
|
def default_sender_address
|
|
|
|
address = Mail::Address.new(Gitlab.config.gitlab.email_from)
|
|
|
|
address.display_name = Gitlab.config.gitlab.email_display_name
|
|
|
|
address
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_reply_to_address
|
|
|
|
address = Mail::Address.new(Gitlab.config.gitlab.email_reply_to)
|
|
|
|
address.display_name = Gitlab.config.gitlab.email_display_name
|
|
|
|
address
|
|
|
|
end
|
2022-09-08 14:10:47 -04:00
|
|
|
|
|
|
|
def mail_with_locale(headers = {}, &block)
|
|
|
|
locale = recipient_locale headers
|
|
|
|
|
|
|
|
Gitlab::I18n.with_locale(locale) do
|
|
|
|
mail(headers, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def recipient_locale(headers = {})
|
|
|
|
to = Array(headers[:to])
|
|
|
|
locale = I18n.locale
|
|
|
|
locale = preferred_language_by_email(to.first) if to.one?
|
|
|
|
locale
|
|
|
|
end
|
|
|
|
|
|
|
|
def preferred_language_by_email(email)
|
|
|
|
User.find_by_any_email(email)&.preferred_language || I18n.locale
|
|
|
|
end
|
2015-08-19 15:35:08 -04:00
|
|
|
end
|