Chatwoot/app/mailers/application_mailer.rb
Sojan Jose 31c07771e8
feat: Notification on new messages in conversation (#1204)
fixes: #895
fixes: #1118
fixes: #1075

Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
2020-09-10 19:19:15 +05:30

62 lines
1.9 KiB
Ruby

class ApplicationMailer < ActionMailer::Base
include ActionView::Helpers::SanitizeHelper
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
before_action { ensure_current_account(params.try(:[], :account)) }
layout 'mailer/base'
# Fetch template from Database if available
# Order: Account Specific > Installation Specific > Fallback to file
prepend_view_path ::EmailTemplate.resolver
append_view_path Rails.root.join('app/views/mailers')
helper :frontend_urls
helper do
def global_config
@global_config ||= GlobalConfig.get('BRAND_NAME', 'BRAND_URL')
end
end
def smtp_config_set_or_development?
ENV.fetch('SMTP_ADDRESS', nil).present? || Rails.env.development?
end
private
def send_mail_with_liquid(*args)
mail(*args) do |format|
# explored sending a multipart email containg both text type and html
# parsing the html with nokogiri will remove the links as well
# might also remove tags like b,li etc. so lets rethink about this later
# format.text { Nokogiri::HTML(render(layout: false)).text }
format.html { render }
end
end
def liquid_droppables
# Merge additional objects into this in your mailer
# liquid template handler converts these objects into drop objects
{
account: Current.account,
user: @agent,
conversation: @conversation,
inbox: @conversation&.inbox
}
end
def liquid_locals
# expose variables you want to be exposed in liquid
{
global_config: GlobalConfig.get('BRAND_NAME', 'BRAND_URL'),
action_url: @action_url
}
end
def locale_from_account(account)
I18n.available_locales.map(&:to_s).include?(account.locale) ? account.locale : nil
end
def ensure_current_account(account)
Current.account = account if account.present?
locale ||= locale_from_account(account) if account.present?
I18n.locale = locale || I18n.default_locale
end
end