2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
class ApplicationMailer < ActionMailer::Base
|
2017-09-10 17:25:29 +05:30
|
|
|
around_action :render_with_default_locale
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
helper ApplicationHelper
|
|
|
|
helper MarkupHelper
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
attr_accessor :current_user
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
helper_method :current_user, :can?
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
default from: proc { default_sender_address.format }
|
|
|
|
default reply_to: proc { default_reply_to_address.format }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
def can?
|
2016-09-29 09:46:39 +05:30
|
|
|
Ability.allowed?(current_user, action, subject)
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def render_with_default_locale(&block)
|
|
|
|
Gitlab::I18n.with_default_locale(&block)
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
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-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
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-09-25 12:07:36 +05:30
|
|
|
end
|